Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Error Messages #282

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 126 additions & 23 deletions pkg/api/artifact_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"fmt"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/scan"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
Expand All @@ -12,7 +14,7 @@ import (
func DeleteArtifact(projectName, repoName, reference string) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
return fmt.Errorf("Failed to initialize client context")
}

_, err = client.Artifact.DeleteArtifact(ctx, &artifact.DeleteArtifactParams{
Expand All @@ -21,8 +23,18 @@ func DeleteArtifact(projectName, repoName, reference string) error {
Reference: reference,
})
if err != nil {
log.Errorf("Failed to delete artifact: %v", err)
return err
switch err.(type) {
case *artifact.DeleteArtifactForbidden:
return fmt.Errorf("Forbidden to delete artifact: %s/%s@%s", projectName, repoName, reference)
case *artifact.DeleteArtifactInternalServerError:
return fmt.Errorf("Internal server error occurred while deleting artifact: %s/%s@%s", projectName, repoName, reference)
case *artifact.DeleteArtifactNotFound:
return fmt.Errorf("Artifact not found: %s/%s@%s", projectName, repoName, reference)
case *artifact.DeleteArtifactUnauthorized:
return fmt.Errorf("Unauthorized to delete artifact: %s/%s@%s", projectName, repoName, reference)
default:
return fmt.Errorf("Unknown error occurred while deleting artifact: %v", err)
}
}

log.Infof("Artifact deleted successfully: %s/%s@%s", projectName, repoName, reference)
Expand All @@ -34,7 +46,7 @@ func ViewArtifact(projectName, repoName, reference string) (*artifact.GetArtifac
ctx, client, err := utils.ContextWithClient()
var response = &artifact.GetArtifactOK{}
if err != nil {
return response, err
return response, fmt.Errorf("Failed to initialize client context")
}

response, err = client.Artifact.GetArtifact(ctx, &artifact.GetArtifactParams{
Expand All @@ -44,8 +56,20 @@ func ViewArtifact(projectName, repoName, reference string) (*artifact.GetArtifac
})

if err != nil {
log.Errorf("Failed to get artifact info: %v", err)
return response, err
log.Errorf("Failed to get artifact info: ")

switch err.(type) {
case *artifact.GetArtifactForbidden:
return response, fmt.Errorf("Forbidden to retrieve artifact: %s/%s@%s", projectName, repoName, reference)
case *artifact.GetArtifactInternalServerError:
return response, fmt.Errorf("Internal server error occurred while retrieving artifact: %s/%s@%s", projectName, repoName, reference)
case *artifact.GetArtifactNotFound:
return response, fmt.Errorf("Artifact not found: %s/%s@%s", projectName, repoName, reference)
case *artifact.GetArtifactUnauthorized:
return response, fmt.Errorf("Unauthorized to retrieve artifact: %s/%s@%s", projectName, repoName, reference)
default:
return response, fmt.Errorf("Unknown error occurred while retrieving artifact info: %v", err)
}
}

return response, nil
Expand All @@ -55,7 +79,7 @@ func ViewArtifact(projectName, repoName, reference string) (*artifact.GetArtifac
func ListArtifact(projectName, repoName string, opts ...ListFlags) (artifact.ListArtifactsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return artifact.ListArtifactsOK{}, err
return artifact.ListArtifactsOK{}, fmt.Errorf("Failed to initialize client context")
}

var listFlags ListFlags
Expand All @@ -71,7 +95,18 @@ func ListArtifact(projectName, repoName string, opts ...ListFlags) (artifact.Lis
Sort: &listFlags.Sort,
})
if err != nil {
return artifact.ListArtifactsOK{}, err
switch err.(type) {
case *artifact.ListArtifactsBadRequest:
return artifact.ListArtifactsOK{}, fmt.Errorf("Bad request for listing artifacts: %s/%s", projectName, repoName)
case *artifact.ListArtifactsInternalServerError:
return artifact.ListArtifactsOK{}, fmt.Errorf("Internal server error occurred while listing artifacts: %s/%s", projectName, repoName)
case *artifact.ListArtifactsNotFound:
return artifact.ListArtifactsOK{}, fmt.Errorf("Artifacts not found: %s/%s", projectName, repoName)
case *artifact.ListArtifactsUnauthorized:
return artifact.ListArtifactsOK{}, fmt.Errorf("Unauthorized to list artifacts: %s/%s", projectName, repoName)
default:
return artifact.ListArtifactsOK{}, fmt.Errorf("Unknown error occurred while listing artifacts: %v", err)
}
}

return *response, nil
Expand All @@ -81,7 +116,7 @@ func ListArtifact(projectName, repoName string, opts ...ListFlags) (artifact.Lis
func StartScanArtifact(projectName, repoName, reference string) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
return fmt.Errorf("Failed to initialize client context")
}

_, err = client.Scan.ScanArtifact(ctx, &scan.ScanArtifactParams{
Expand All @@ -90,8 +125,22 @@ func StartScanArtifact(projectName, repoName, reference string) error {
Reference: reference,
})
if err != nil {
log.Errorf("Failed to start scan: %v", err)
return err
log.Errorf("Failed to start scan: ")

switch err.(type) {
case *scan.ScanArtifactBadRequest:
return fmt.Errorf("Bad request for starting scan: %s and %s", repoName, projectName)
case *scan.ScanArtifactForbidden:
return fmt.Errorf("Scan already in progress for artifact: %s and %s", repoName, projectName)
case *scan.ScanArtifactInternalServerError:
return fmt.Errorf("Internal server error occurred while starting scan: %s and %s", repoName, projectName)
case *scan.ScanArtifactNotFound:
return fmt.Errorf("Artifacts %s/%s not found", projectName, repoName)
case *scan.ScanArtifactUnauthorized:
return fmt.Errorf("Unauthorized to start scan: %s and %s", repoName, projectName)
default:
return fmt.Errorf("Unknown error occurred while starting scan: %s and %s: %v", repoName, projectName, err)
}
}

log.Infof("Scan started successfully: %s/%s@%s", projectName, repoName, reference)
Expand All @@ -102,7 +151,7 @@ func StartScanArtifact(projectName, repoName, reference string) error {
func StopScanArtifact(projectName, repoName, reference string) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
return fmt.Errorf("Failed to initialize client context")
}

_, err = client.Scan.StopScanArtifact(ctx, &scan.StopScanArtifactParams{
Expand All @@ -111,8 +160,22 @@ func StopScanArtifact(projectName, repoName, reference string) error {
Reference: reference,
})
if err != nil {
log.Errorf("Failed to stop scan: %v", err)
return err
log.Errorf("Failed to stop scan: ")

switch err.(type) {
case *scan.StopScanArtifactBadRequest:
return fmt.Errorf("Bad request for stopping scan: %s/%s@%s", projectName, repoName, reference)
case *scan.StopScanArtifactForbidden:
return fmt.Errorf("Scan already stopped for artifact: %s/%s@%s", projectName, repoName, reference)
case *scan.StopScanArtifactInternalServerError:
return fmt.Errorf("Internal server error occurred while stopping scan: %s/%s@%s", projectName, repoName, reference)
case *scan.StopScanArtifactNotFound:
return fmt.Errorf("Artifact not found: %s/%s@%s", projectName, repoName, reference)
case *scan.StopScanArtifactUnauthorized:
return fmt.Errorf("Unauthorized to stop scan: %s/%s@%s", projectName, repoName, reference)
default:
return fmt.Errorf("Unknown error occurred while stopping scan: %v", err)
}
}

log.Infof("Scan stopped successfully: %s/%s@%s", projectName, repoName, reference)
Expand All @@ -123,7 +186,7 @@ func StopScanArtifact(projectName, repoName, reference string) error {
func DeleteTag(projectName, repoName, reference, tag string) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
return fmt.Errorf("Failed to initialize client context")
}

_, err = client.Artifact.DeleteTag(ctx, &artifact.DeleteTagParams{
Expand All @@ -133,8 +196,20 @@ func DeleteTag(projectName, repoName, reference, tag string) error {
TagName: tag,
})
if err != nil {
log.Errorf("Failed to delete tag: %v", err)
return err
log.Errorf("Failed to delete tag: ")

switch err.(type) {
case *artifact.DeleteTagForbidden:
return fmt.Errorf("Forbidden to delete tag: %s/%s@%s:%s", projectName, repoName, reference, tag)
case *artifact.DeleteTagInternalServerError:
return fmt.Errorf("Internal server error occurred while deleting tag: %s/%s@%s:%s", projectName, repoName, reference, tag)
case *artifact.DeleteTagNotFound:
return fmt.Errorf("Tag not found: %s/%s@%s:%s", projectName, repoName, reference, tag)
case *artifact.DeleteTagUnauthorized:
return fmt.Errorf("Unauthorized to delete tag: %s/%s@%s:%s", projectName, repoName, reference, tag)
default:
return fmt.Errorf("Unknown error occurred while deleting tag: %v", err)
}
}

log.Infof("Tag deleted successfully: %s/%s@%s:%s", projectName, repoName, reference, tag)
Expand All @@ -145,7 +220,7 @@ func DeleteTag(projectName, repoName, reference, tag string) error {
func ListTags(projectName, repoName, reference string) (*artifact.ListTagsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return &artifact.ListTagsOK{}, err
return &artifact.ListTagsOK{}, fmt.Errorf("Failed to initialize client context")
}

resp, err := client.Artifact.ListTags(ctx, &artifact.ListTagsParams{
Expand All @@ -155,8 +230,23 @@ func ListTags(projectName, repoName, reference string) (*artifact.ListTagsOK, er
})

if err != nil {
log.Errorf("Failed to list tags: %v", err)
return &artifact.ListTagsOK{}, err

log.Errorf("Failed to list tags: ")
switch err.(type) {
case *artifact.ListTagsBadRequest:
return &artifact.ListTagsOK{}, fmt.Errorf("Bad request for listing tags: %s/%s", projectName, repoName)
case *artifact.ListTagsForbidden:
return &artifact.ListTagsOK{}, fmt.Errorf("Forbidden to list tags: %s/%s", projectName, repoName)
case *artifact.ListTagsInternalServerError:
return &artifact.ListTagsOK{}, fmt.Errorf("Internal server error occurred while listing tags: %s/%s", projectName, repoName)
case *artifact.ListTagsNotFound:
return &artifact.ListTagsOK{}, fmt.Errorf("Tags not found for artifact: %s/%s@%s", projectName, repoName, reference)
case *artifact.ListTagsUnauthorized:
return &artifact.ListTagsOK{}, fmt.Errorf("Unauthorized to list tags: %s/%s", projectName, repoName)
default:
return &artifact.ListTagsOK{}, fmt.Errorf("Unknown error occurred while listing tags: %s/%s: %v", projectName, repoName, err)
}

}

return resp, nil
Expand All @@ -166,7 +256,7 @@ func ListTags(projectName, repoName, reference string) (*artifact.ListTagsOK, er
func CreateTag(projectName, repoName, reference, tagName string) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
return fmt.Errorf("Failed to initialize client context")
}
_, err = client.Artifact.CreateTag(ctx, &artifact.CreateTagParams{
ProjectName: projectName,
Expand All @@ -177,8 +267,21 @@ func CreateTag(projectName, repoName, reference, tagName string) error {
},
})
if err != nil {
log.Errorf("Failed to create tag: %v", err)
return err
log.Errorf("Failed to create tag: ")
switch err.(type) {
case *artifact.CreateTagBadRequest:
return fmt.Errorf("Bad request for creating tag: %s/%s:%s", repoName, reference, tagName)
case *artifact.CreateTagForbidden:
return fmt.Errorf("Forbidden to create tag: %s/%s:%s", repoName, reference, tagName)
case *artifact.CreateTagInternalServerError:
return fmt.Errorf("Internal server error occurred while creating tag: %s/%s:%s", repoName, reference, tagName)
case *artifact.CreateTagNotFound:
return fmt.Errorf("Artifact not found: %s/%s@%s", projectName, repoName, reference)
case *artifact.CreateTagUnauthorized:
return fmt.Errorf("Unauthorized to create tag: %s/%s:%s", repoName, reference, tagName)
default:
return fmt.Errorf("Unknown error occurred while creating tag: %s/%s:%s: %v", repoName, reference, tagName, err)
}
}
log.Infof("Tag created successfully: %s/%s@%s:%s", projectName, repoName, reference, tagName)
return nil
Expand Down
9 changes: 7 additions & 2 deletions pkg/api/health_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import (
func GetHealth() (*health.GetHealthOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to initialize client context: ")
}

response, err := client.Health.GetHealth(ctx, &health.GetHealthParams{})
if err != nil {
return nil, fmt.Errorf("error getting health status: %w", err)
switch err.(type) {
case *health.GetHealthInternalServerError:
return nil, fmt.Errorf("internal server error occurred while getting health status")
default:
return nil, fmt.Errorf("unknown error occurred while getting health status: %w", err)
}
}

return response, nil
Expand Down
Loading
Loading