diff --git a/generated/stackstate_api/api/openapi.yaml b/generated/stackstate_api/api/openapi.yaml index 132d34fb..b5164792 100644 --- a/generated/stackstate_api/api/openapi.yaml +++ b/generated/stackstate_api/api/openapi.yaml @@ -3541,6 +3541,12 @@ paths: schema: $ref: '#/components/schemas/ServerInfo' description: Server information + "401": + content: + application/json: + schema: + $ref: '#/components/schemas/GenericErrorsResponse' + description: Error when handling the request on the server side. "500": content: application/json: diff --git a/generated/stackstate_api/api_server.go b/generated/stackstate_api/api_server.go index f9e84b68..bde36c39 100644 --- a/generated/stackstate_api/api_server.go +++ b/generated/stackstate_api/api_server.go @@ -165,6 +165,16 @@ func (a *ServerApiService) ServerInfoExecute(r ApiServerInfoRequest) (*ServerInf body: localVarBody, error: localVarHTTPResponse.Status, } + if localVarHTTPResponse.StatusCode == 401 { + var v GenericErrorsResponse + err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHTTPResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHTTPResponse, newErr + } if localVarHTTPResponse.StatusCode == 500 { var v GenericErrorsResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) diff --git a/internal/common/common_cli_errors.go b/internal/common/common_cli_errors.go index e1b7e0c2..73f045d1 100644 --- a/internal/common/common_cli_errors.go +++ b/internal/common/common_cli_errors.go @@ -5,6 +5,7 @@ import ( "net/http" "strings" + "github.com/stackvista/stackstate-cli/generated/stackstate_api" "github.com/stackvista/stackstate-cli/internal/util" ) @@ -39,9 +40,30 @@ func NewConnectError(err error, apiURL string, serverResponse *http.Response) CL } if statusCode == HTTPStatusUnauthorized { + var errMessage string + + if apiErr, ok := err.(*stackstate_api.GenericOpenAPIError); ok { + if detailedErr, ok := apiErr.Model().(stackstate_api.GenericErrorsResponse); ok { + // Now you can access your 401 error details + var messages []string + for _, apiErr := range detailedErr.Errors { + messages = append(messages, apiErr.Message) + } + errMessage = strings.Join(messages, ", ") + // Some responses do not yield more than the 401, others give in the body the error message as the RefreshToken failed or so + if errMessage == "" { + errMessage = "invalid api-token" + } + } else { + errMessage = err.Error() + } + } else { + errMessage = err.Error() + } + return StdCLIError{ - Err: fmt.Errorf("could not connect to %s: invalid api-token\n"+ - "For more information: https://l.stackstate.com/cli-invalid-api-token", apiURL), + Err: fmt.Errorf("could not connect to %s: %s\n"+ + "For more information: https://l.stackstate.com/cli-invalid-api-token", apiURL, errMessage), exitCode: ConnectErrorExitCode, } } else { diff --git a/stackstate_openapi/openapi_version b/stackstate_openapi/openapi_version index 4f66fe79..4a076531 100644 --- a/stackstate_openapi/openapi_version +++ b/stackstate_openapi/openapi_version @@ -1 +1 @@ -3e7ca840dfb06920cbffa0b7f70130d6b17e0721 +eb3e3df341ccfd167ed8c2e2c14b63a38be686b3