Skip to content

Commit

Permalink
fix: added support to unauthorized error (#3292)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbdias authored Oct 25, 2023
1 parent 271be34 commit 2296535
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
18 changes: 17 additions & 1 deletion cli/cmd/middleware.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"errors"
"fmt"
"os"

"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"

"github.com/spf13/cobra"
)

Expand All @@ -27,17 +30,30 @@ func WithResultHandler(runFn RunFn) CobraRunFn {
}

func OnError(err error) {
errorMessage := handleErrorMessage(err)

fmt.Fprintf(os.Stderr, `
Version
%s
An error ocurred when executing the command
%s
`, versionText, err.Error())
`, versionText, errorMessage)
ExitCLI(1)
}

func handleErrorMessage(err error) string {
var requestError resourcemanager.RequestError
hasRequestError := errors.As(err, &requestError)

if !hasRequestError || requestError.Code != 401 {
return err.Error()
}

return fmt.Sprintf("user is not authenticated on %s", cliConfig.Endpoint)
}

func WithParamsHandler(validators ...Validator) MiddlewareWrapper {
return func(runFn RunFn) RunFn {
return func(cmd *cobra.Command, args []string) (string, error) {
Expand Down
38 changes: 30 additions & 8 deletions cli/pkg/resourcemanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,27 @@ func (c Client) resourceType() string {
return caser.String(c.resourceName)
}

var ErrNotFound = requestError{
var ErrNotFound = RequestError{
Code: http.StatusNotFound,
Message: "Resource not found",
}

type requestError struct {
type RequestError struct {
Code int `json:"code"`
Message string `json:"error"`
}

func (e requestError) Error() string {
type alternateRequestError struct {
Status int `json:"status"`
Detail string `json:"detail"`
}

func (e RequestError) Error() string {
return e.Message
}

func (e requestError) Is(target error) bool {
t, ok := target.(requestError)
func (e RequestError) Is(target error) bool {
t, ok := target.(RequestError)
return ok && t.Code == e.Code
}

Expand All @@ -159,16 +164,33 @@ func parseRequestError(resp *http.Response, format Format) error {
}

if len(body) == 0 {
return requestError{
return RequestError{
Code: resp.StatusCode,
Message: resp.Status,
}
}
var reqErr requestError
var reqErr RequestError
err = format.Unmarshal(body, &reqErr)
if err != nil {
return fmt.Errorf("cannot parse response body: %w", err)
}

return reqErr
emptyRequestError := reqErr.Code == 0 && reqErr.Message == ""
if !emptyRequestError {
// Success, parsed error message
return reqErr
}

// Fallback, try to parse message in other format

var alternateReqError alternateRequestError
err = format.Unmarshal(body, &alternateReqError)
if err != nil {
return fmt.Errorf("cannot parse response body: %w", err)
}

return RequestError{
Code: alternateReqError.Status,
Message: alternateReqError.Detail,
}
}

0 comments on commit 2296535

Please sign in to comment.