From 99b1af527154f662210ec742b49d3194a82a3778 Mon Sep 17 00:00:00 2001 From: bupd Date: Mon, 30 Sep 2024 11:47:02 +0530 Subject: [PATCH] improve err handling Signed-off-by: bupd --- ground-control/internal/server/handlers.go | 10 +++++++++- ground-control/internal/utils/helper.go | 10 ++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ground-control/internal/server/handlers.go b/ground-control/internal/server/handlers.go index effcb13..50a5cb3 100644 --- a/ground-control/internal/server/handlers.go +++ b/ground-control/internal/server/handlers.go @@ -2,6 +2,7 @@ package server import ( "crypto/rand" + "database/sql" "encoding/hex" "encoding/json" "fmt" @@ -199,8 +200,15 @@ func (s *Server) removeImageHandler(w http.ResponseWriter, r *http.Request) { err = s.dbQueries.DeleteImage(r.Context(), int32(id)) if err != nil { - err = fmt.Errorf("error: delete image failed: %v", err) log.Println(err) + if err == sql.ErrNoRows { + err = &AppError{ + Message: fmt.Sprintf("Image with ID %d not found", id), + Code: http.StatusNotFound, + } + } else { + err = fmt.Errorf("error: delete image failed: %v", err) + } HandleAppError(w, err) return } diff --git a/ground-control/internal/utils/helper.go b/ground-control/internal/utils/helper.go index 9fe2e89..f32839c 100644 --- a/ground-control/internal/utils/helper.go +++ b/ground-control/internal/utils/helper.go @@ -34,13 +34,11 @@ func ParseArtifactURL(rawURL string) (reg.Images, error) { // Validate that repository and registry exist if repo == "" || registry == "" { - log.Println("Error: Missing repository or registry.") return reg.Images{}, fmt.Errorf("error: missing repository or registry in URL: %s", rawURL) } // Validate that either tag or digest exists if tag == "" && digest == "" { - log.Println("Error: Missing tag or digest.") return reg.Images{}, fmt.Errorf("error: missing tag or digest in artifact URL: %s", rawURL) } @@ -81,9 +79,7 @@ func CreateRobotAccForSatellite(ctx context.Context, repos []string, name string // get harbor client harborClient, err := harbor.GetClient() if err != nil { - log.Println("error in getting client") - log.Println(err) - return nil, err + return nil, fmt.Errorf("error getting Harbor client: %w", err) } var projects []string @@ -97,9 +93,7 @@ func CreateRobotAccForSatellite(ctx context.Context, repos []string, name string robot, err := harbor.CreateRobotAccount(ctx, robotTemp, harborClient) if err != nil { - log.Println("error: creating robot account") - log.Println(err) - return nil, err + return nil, fmt.Errorf("error creating robot account: %w", err) } return robot.Payload, nil