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

Better errormessage #266

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion cmd/harbor/root/registry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/errors"
"github.com/goharbor/harbor-cli/pkg/views/registry/create"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,7 +38,7 @@ func CreateRegistryCommand() *cobra.Command {
}

if err != nil {
log.Errorf("failed to create registry: %v", err)
log.Errorf("failed to create registry: %v", errors.ErrorCreateRegistry(err))
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/harbor/root/registry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/errors"
"github.com/goharbor/harbor-cli/pkg/prompt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -25,7 +26,7 @@ func DeleteRegistryCommand() *cobra.Command {
err = api.DeleteRegistry(registryId)
}
if err != nil {
log.Errorf("failed to delete registry: %v", err)
log.Errorf("failed to delete registry: %v", errors.ErrorDeleteRegistry(err))
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/harbor/root/registry/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/errors"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/registry/list"
log "github.com/sirupsen/logrus"
Expand All @@ -20,7 +21,7 @@ func ListRegistryCommand() *cobra.Command {
registry, err := api.ListRegistries(opts)

if err != nil {
log.Fatalf("failed to get projects list: %v", err)
log.Fatalf("failed to get projects list: %v", errors.ErrorListRegistry(err))
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
Expand Down
3 changes: 2 additions & 1 deletion cmd/harbor/root/registry/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/errors"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/registry/create"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -51,7 +52,7 @@ func UpdateRegistryCommand() *cobra.Command {
}

if err != nil {
log.Errorf("failed to update registry: %v", err)
log.Errorf("failed to update registry: %v", errors.ErrorUpdateRegistry(err))
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/harbor/root/registry/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package registry
import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/errors"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/registry/view"
Expand Down Expand Up @@ -35,7 +36,7 @@ func ViewRegistryCommand() *cobra.Command {
registry, err = api.ViewRegistry(registryId)

if err != nil {
log.Errorf("failed to get registry info: %v", err)
log.Errorf("failed to get registry info: %v", errors.ErrorViewRegistry(err))
return
}

Expand Down
50 changes: 50 additions & 0 deletions pkg/errors/registry_error.go
Althaf66 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package errors

import (
"github.com/goharbor/harbor-cli/pkg/utils"
)

func getErrorMessage(statusCode int64, action string) string {
switch statusCode {
case 400:
if action == "create" {
return "Give adequate information to create a registry."
}
case 401:
return "Registry Unauthorized"
case 403:
return "Registry Forbidden"
case 404:
return "Registry is not found"
case 409:
if action == "create" {
return "Registries with the same name exist"
}
return "Conflict"
case 412:
return "Registry is not found"
case 500:
return "Internal Server Error"
}
return "cannot identify the status code"
}

func ErrorCreateRegistry(err error) string {
return getErrorMessage(utils.ErrorStatusCode(err), "create")
}

func ErrorDeleteRegistry(err error) string {
return getErrorMessage(utils.ErrorStatusCode(err), "delete")
}

func ErrorViewRegistry(err error) string {
return getErrorMessage(utils.ErrorStatusCode(err), "view")
}

func ErrorListRegistry(err error) string {
return getErrorMessage(utils.ErrorStatusCode(err), "list")
}

func ErrorUpdateRegistry(err error) string {
return getErrorMessage(utils.ErrorStatusCode(err), "update")
}
22 changes: 22 additions & 0 deletions pkg/utils/error.go
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
package utils

import (
"regexp"
"strconv"

log "github.com/sirupsen/logrus"
)

func ErrorStatusCode(err error) int64 {
re := regexp.MustCompile(`\[(\d{3})\]`)
matches := re.FindStringSubmatch(err.Error())

var statusCode int64
if len(matches) > 1 {
stringCode := matches[1]
statusCode, err = strconv.ParseInt(stringCode, 10, 64)
if err != nil {
log.Fatal(err)
}
}
return statusCode
}
Loading