|
5 | 5 | "net/http" |
6 | 6 | "strings" |
7 | 7 |
|
| 8 | + "github.com/stackvista/stackstate-cli/generated/stackstate_api" |
8 | 9 | "github.com/stackvista/stackstate-cli/internal/util" |
9 | 10 | ) |
10 | 11 |
|
@@ -39,9 +40,30 @@ func NewConnectError(err error, apiURL string, serverResponse *http.Response) CL |
39 | 40 | } |
40 | 41 |
|
41 | 42 | if statusCode == HTTPStatusUnauthorized { |
| 43 | + var errMessage string |
| 44 | + |
| 45 | + if apiErr, ok := err.(*stackstate_api.GenericOpenAPIError); ok { |
| 46 | + if detailedErr, ok := apiErr.Model().(stackstate_api.GenericErrorsResponse); ok { |
| 47 | + // Now you can access your 401 error details |
| 48 | + var messages []string |
| 49 | + for _, apiErr := range detailedErr.Errors { |
| 50 | + messages = append(messages, apiErr.Message) |
| 51 | + } |
| 52 | + errMessage = strings.Join(messages, ", ") |
| 53 | + // Some responses do not yield more than the 401, others give in the body the error message as the RefreshToken failed or so |
| 54 | + if errMessage == "" { |
| 55 | + errMessage = "invalid api-token" |
| 56 | + } |
| 57 | + } else { |
| 58 | + errMessage = err.Error() |
| 59 | + } |
| 60 | + } else { |
| 61 | + errMessage = err.Error() |
| 62 | + } |
| 63 | + |
42 | 64 | return StdCLIError{ |
43 | | - Err: fmt.Errorf("could not connect to %s: invalid api-token\n"+ |
44 | | - "For more information: https://l.stackstate.com/cli-invalid-api-token", apiURL), |
| 65 | + Err: fmt.Errorf("could not connect to %s: %s\n"+ |
| 66 | + "For more information: https://l.stackstate.com/cli-invalid-api-token", apiURL, errMessage), |
45 | 67 | exitCode: ConnectErrorExitCode, |
46 | 68 | } |
47 | 69 | } else { |
|
0 commit comments