Skip to content

Commit

Permalink
add more clierror wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed May 14, 2024
1 parent 9161cfb commit 574c835
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 122 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
sigs.k8s.io/controller-runtime v0.18.2
)

require (
Expand All @@ -27,7 +26,6 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -53,6 +51,8 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/onsi/ginkgo/v2 v2.17.1 // indirect
github.com/onsi/gomega v1.32.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxER
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gboddin/go-www-authenticate-parser v0.0.0-20230926203616-ec0b649bb077 h1:JvEO7eltd2aCHF+ABLquTUziO7hzC6G7H3tgENYkDBc=
Expand All @@ -46,8 +44,6 @@ github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE=
Expand Down Expand Up @@ -172,15 +168,9 @@ go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
go.opentelemetry.io/proto/otlp v1.1.0 h1:2Di21piLrCqJ3U3eXGCTPHE9R8Nh+0uglSnOyxikMeI=
go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7ej/RNTae6MdY=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
Expand Down Expand Up @@ -250,8 +240,6 @@ gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
k8s.io/api v0.30.0 h1:siWhRq7cNjy2iHssOB9SCGNCl2spiF1dO3dABqZ8niA=
k8s.io/api v0.30.0/go.mod h1:OPlaYhoHs8EQ1ql0R/TsUgaRPhpKNxIMrKQfWUp8QSE=
k8s.io/apiextensions-apiserver v0.30.0 h1:jcZFKMqnICJfRxTgnC4E+Hpcq8UEhT8B2lhBcQ+6uAs=
k8s.io/apiextensions-apiserver v0.30.0/go.mod h1:N9ogQFGcrbWqAY9p2mUAL5mGxsLqwgtUce127VtRX5Y=
k8s.io/apimachinery v0.30.0 h1:qxVPsyDM5XS96NIh9Oj6LavoVFYff/Pon9cZeDIkHHA=
k8s.io/apimachinery v0.30.0/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/client-go v0.30.0 h1:sB1AGGlhY/o7KCyCEQ0bPWzYDL0pwOZO4vAtTSh/gJQ=
Expand All @@ -262,8 +250,6 @@ k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7F
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/controller-runtime v0.18.2 h1:RqVW6Kpeaji67CY5nPEfRz6ZfFMk0lWQlNrLqlNpx+Q=
sigs.k8s.io/controller-runtime v0.18.2/go.mod h1:tuAt1+wbVsXIT8lPtk5RURxqAnq7xkpv2Mhttslg7Hw=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
Expand Down
4 changes: 2 additions & 2 deletions internal/btp/auth/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ type UAA struct {
func LoadCISCredentials(path string) (*CISCredentials, error) {
credentialsBytes, err := os.ReadFile(path)
if err != nil {
return nil, &clierror.Error{Message: "failed to read credentials file", Details: err.Error(), Hints: []string{"Make sure the path to the credentials file is correct."}}
return nil, clierror.Wrap(err, &clierror.Error{Message: "failed to read credentials file", Hints: []string{"Make sure the path to the credentials file is correct."}})
}

credentials := CISCredentials{}
err = json.Unmarshal(credentialsBytes, &credentials)
if err != nil {
return nil, &clierror.Error{Message: "failed to unmarshal file data", Details: err.Error(), Hints: []string{"Make sure the credentials file is in the correct format."}}
return nil, clierror.Wrap(err, &clierror.Error{Message: "failed to unmarshal file data", Hints: []string{"Make sure the credentials file is in the correct format."}})
}

return &credentials, nil
Expand Down
9 changes: 5 additions & 4 deletions internal/btp/auth/xsuaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetOAuthToken(grantType, serverURL, username, password string) (*XSUAAToken
strings.NewReader(urlBody.Encode()),
)
if err != nil {
return nil, &clierror.Error{Message: "failed to build request", Details: err.Error(), Hints: []string{"Make sure the server URL in the config is correct."}}
return nil, clierror.Wrap(err, &clierror.Error{Message: "failed to build request", Hints: []string{"Make sure the server URL in the config is correct."}})
}
defer request.Body.Close()

Expand All @@ -44,7 +44,7 @@ func GetOAuthToken(grantType, serverURL, username, password string) (*XSUAAToken

response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, &clierror.Error{Message: "failed to get token from server", Details: err.Error()}
return nil, clierror.Wrap(err, &clierror.Error{Message: "failed to get token from server"})
}
defer response.Body.Close()

Expand All @@ -59,7 +59,7 @@ func decodeAuthSuccessResponse(response *http.Response) (*XSUAAToken, error) {
token := XSUAAToken{}
err := json.NewDecoder(response.Body).Decode(&token)
if err != nil {
return nil, &clierror.Error{Message: fmt.Sprintf("failed to decode response with Status %s", response.Status), Details: err.Error()}
return nil, clierror.Wrap(err, &clierror.Error{Message: fmt.Sprintf("failed to decode response with Status %s", response.Status)})
}

return &token, nil
Expand All @@ -69,7 +69,8 @@ func decodeAuthErrorResponse(response *http.Response) error {
errorData := xsuaaErrorResponse{}
err := json.NewDecoder(response.Body).Decode(&errorData)
if err != nil {
return &clierror.Error{Message: "failed to decode error response", Details: err.Error()}
return clierror.Wrap(err, &clierror.Error{Message: "failed to decode error response"})
}
// TODO: replace it with New func
return &clierror.Error{Message: fmt.Sprintf("error response: %s", response.Status), Details: errorData.ErrorDescription}
}
5 changes: 1 addition & 4 deletions internal/btp/cis/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ func decodeProvisionSuccessResponse(response *http.Response) (*ProvisionResponse
provisionResponse := ProvisionResponse{}
err := json.NewDecoder(response.Body).Decode(&provisionResponse)
if err != nil {
return nil, &clierror.Error{
Message: "failed to decode response",
Details: err.Error(),
}
return nil, clierror.Wrap(err, &clierror.Error{Message: "failed to decode response"})
}

return &provisionResponse, nil
Expand Down
42 changes: 15 additions & 27 deletions internal/clierror/cli_error.go → internal/clierror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ type Error struct {
Hints []string
}

// Error returns the error string, compatible with the error interface
func (e Error) Error() string {
output := fmt.Sprintf("Error:\n %s\n\n", e.Message)
if e.Details != "" {
output += fmt.Sprintf("Error Details:\n %s\n\n", e.Details)
}
if len(e.Hints) > 0 {
output += "Hints:\n"
for _, hint := range e.Hints {
output += fmt.Sprintf(" - %s\n", hint)
}
}
return output
}

// Wrap adds a new message and hints to the error
func (inside *Error) wrap(outside *Error) *Error {
newError := &Error{
Expand Down Expand Up @@ -46,30 +61,3 @@ func wrapDetails(outside, inside string) string {
}
return fmt.Sprintf("%s: %s", outside, inside)
}

func Wrap(inside error, outside *Error) error {
if err, ok := inside.(*Error); ok {
return err.wrap(outside)
} else {
return &Error{
Message: outside.Message,
Details: wrapDetails(outside.Details, inside.Error()),
Hints: outside.Hints,
}
}
}

// Error returns the error string, compatible with the error interface
func (e Error) Error() string {
output := fmt.Sprintf("Error:\n %s\n\n", e.Message)
if e.Details != "" {
output += fmt.Sprintf("Error Details:\n %s\n\n", e.Details)
}
if len(e.Hints) > 0 {
output += "Hints:\n"
for _, hint := range e.Hints {
output += fmt.Sprintf(" - %s\n", hint)
}
}
return output
}
File renamed without changes.
13 changes: 13 additions & 0 deletions internal/clierror/wrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package clierror

func Wrap(inside error, outside *Error) error {
if err, ok := inside.(*Error); ok {
return err.wrap(outside)
} else {
return &Error{
Message: outside.Message,
Details: wrapDetails(outside.Details, inside.Error()),
Hints: outside.Hints,
}
}
}
22 changes: 12 additions & 10 deletions internal/cmd/hana/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,23 @@ func checkHanaBindingUrl(config *hanaCheckConfig) error {

func handleCheckResponse(u *unstructured.Unstructured, err error, printedName, namespace, name string) error {
if err != nil {
return &clierror.Error{
Message: "failed to get resource data",
Details: err.Error(),
Hints: []string{
"Make sure that Hana was provisioned.",
return clierror.Wrap(err,
&clierror.Error{
Message: "failed to get resource data",
Hints: []string{
"Make sure that Hana was provisioned.",
},
},
}
)
}

ready, error := kube.IsReady(u)
if error != nil {
return &clierror.Error{
Message: "failed to check readiness of Hana resources",
Details: error.Error(),
}
return clierror.Wrap(err,
&clierror.Error{
Message: "failed to check readiness of Hana resources",
},
)
}
if !ready {
fmt.Printf("%s is not ready (%s/%s).\n", printedName, namespace, name)
Expand Down
23 changes: 12 additions & 11 deletions internal/cmd/hana/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ func getHanaCredentials(config *hanaCredentialsConfig) (credentials, error) {
}

func handleGetHanaCredentialsError(err error) (credentials, error) {
error := &clierror.Error{
Message: "failed to get Hana credentails",
Details: err.Error(),
Hints: []string{
"Make sure that Hana is run and ready to use. You can use command 'kyma hana check'.",
},
hints := []string{
"Make sure that Hana is run and ready to use. You can use command 'kyma hana check'.",
}
if error.Details == "Unauthorized" {
error.Hints = []string{
"Make sure that your kubeconfig has access to kubernetes.",
}

if err.Error() == "Unauthorized" {
hints = append(hints, "Make sure that your kubeconfig has access to kubernetes.")
}
return credentials{}, error

credErr := clierror.Wrap(err, &clierror.Error{
Message: "failed to get Hana credentails",
Hints: hints,
})

return credentials{}, credErr
}
5 changes: 2 additions & 3 deletions internal/cmd/hana/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ func handleDeleteResponse(err error, printedName, namespace, name string) error
fmt.Printf("%s (%s/%s) not found.\n", printedName, namespace, name)
return nil
}
return &clierror.Error{
return clierror.Wrap(err, &clierror.Error{
Message: "failed to delete Hana resource.",
Details: err.Error(),
}
})
}
Loading

0 comments on commit 574c835

Please sign in to comment.