Skip to content

Commit

Permalink
improve err handling
Browse files Browse the repository at this point in the history
Signed-off-by: bupd <[email protected]>
  • Loading branch information
bupd committed Sep 30, 2024
1 parent b2f758b commit 99b1af5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 9 additions & 1 deletion ground-control/internal/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"crypto/rand"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 2 additions & 8 deletions ground-control/internal/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 99b1af5

Please sign in to comment.