Skip to content

Commit caf654a

Browse files
Merge pull request #102 from StackVista/stac-23051
STAC-23051: Make error when trying out connection to platform more in…
2 parents f491b7b + 38b5535 commit caf654a

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

generated/stackstate_api/api/openapi.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,6 +3541,12 @@ paths:
35413541
schema:
35423542
$ref: '#/components/schemas/ServerInfo'
35433543
description: Server information
3544+
"401":
3545+
content:
3546+
application/json:
3547+
schema:
3548+
$ref: '#/components/schemas/GenericErrorsResponse'
3549+
description: Error when handling the request on the server side.
35443550
"500":
35453551
content:
35463552
application/json:

generated/stackstate_api/api_server.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/common/common_cli_errors.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strings"
77

8+
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
89
"github.com/stackvista/stackstate-cli/internal/util"
910
)
1011

@@ -39,9 +40,30 @@ func NewConnectError(err error, apiURL string, serverResponse *http.Response) CL
3940
}
4041

4142
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+
4264
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),
4567
exitCode: ConnectErrorExitCode,
4668
}
4769
} else {

stackstate_openapi/openapi_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3e7ca840dfb06920cbffa0b7f70130d6b17e0721
1+
eb3e3df341ccfd167ed8c2e2c14b63a38be686b3

0 commit comments

Comments
 (0)