Skip to content

Commit 47b25c8

Browse files
STAC-23051: Make error when trying out connection to platform more informative
1 parent f491b7b commit 47b25c8

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

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: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package common
22

33
import (
44
"fmt"
5+
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
6+
"github.com/stackvista/stackstate-cli/internal/util"
57
"net/http"
68
"strings"
7-
8-
"github.com/stackvista/stackstate-cli/internal/util"
99
)
1010

1111
const (
@@ -39,9 +39,28 @@ func NewConnectError(err error, apiURL string, serverResponse *http.Response) CL
3939
}
4040

4141
if statusCode == HTTPStatusUnauthorized {
42+
var errMessage string
43+
44+
if apiErr, ok := err.(*stackstate_api.GenericOpenAPIError); ok {
45+
if detailedErr, ok := apiErr.Model().(stackstate_api.GenericErrorsResponse); ok {
46+
// Now you can access your 401 error details
47+
var messages []string
48+
for _, apiErr := range detailedErr.Errors {
49+
messages = append(messages, apiErr.Message)
50+
}
51+
errMessage = strings.Join(messages, ", ")
52+
//fmt.Println("401 error details:", strings.Join(messages, ", "))
53+
} else {
54+
errMessage = err.Error()
55+
//fmt.Println("401 error, but failed to cast model to GenericErrorsResponse")
56+
}
57+
} else {
58+
errMessage = err.Error()
59+
}
60+
4261
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),
62+
Err: fmt.Errorf("could not connect to %s: %s\n"+
63+
"For more information: https://l.stackstate.com/cli-invalid-api-token", apiURL, errMessage),
4564
exitCode: ConnectErrorExitCode,
4665
}
4766
} 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)