Skip to content

Commit fced493

Browse files
authored
Show correct URL upon failed HTTP request from CLI (#504)
1 parent d559de1 commit fced493

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

cli/cmd/errors.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
"net/url"
2122

2223
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2324
)
@@ -31,23 +32,25 @@ type ErrorKind int
3132

3233
const (
3334
ErrUnknown ErrorKind = iota
34-
ErrCliAlreadyInAppDir
35+
ErrCLIAlreadyInAppDir
3536
ErrAPINotReady
3637
ErrAPINotFound
37-
ErrFailedToConnect
38-
ErrCliNotInAppDir
38+
ErrFailedToConnectURL
39+
ErrFailedToConnectOperator
40+
ErrCLINotInAppDir
3941
)
4042

4143
var errorKinds = []string{
4244
"err_unknown",
4345
"err_cli_already_in_app_dir",
4446
"err_api_not_ready",
4547
"err_api_not_found",
46-
"err_failed_to_connect",
48+
"err_failed_to_connect_url",
49+
"err_failed_to_connect_operator",
4750
"err_cli_not_in_app_dir",
4851
}
4952

50-
var _ = [1]int{}[int(ErrCliNotInAppDir)-(len(errorKinds)-1)] // Ensure list length matches
53+
var _ = [1]int{}[int(ErrCLINotInAppDir)-(len(errorKinds)-1)] // Ensure list length matches
5154

5255
func (t ErrorKind) String() string {
5356
return errorKinds[t]
@@ -94,7 +97,7 @@ func (e Error) Error() string {
9497

9598
func ErrorCliAlreadyInAppDir(dirPath string) error {
9699
return Error{
97-
Kind: ErrCliAlreadyInAppDir,
100+
Kind: ErrCLIAlreadyInAppDir,
98101
message: fmt.Sprintf("your current working directory is already in a cortex directory (%s)", dirPath),
99102
}
100103
}
@@ -113,16 +116,24 @@ func ErrorAPINotFound(apiName string) error {
113116
}
114117
}
115118

116-
func ErrorFailedToConnect(urlStr string) error {
119+
func ErrorFailedConnectURL(url url.URL) error {
120+
url.RawQuery = ""
117121
return Error{
118-
Kind: ErrFailedToConnect,
122+
Kind: ErrFailedToConnectURL,
123+
message: "failed to connect to " + url.String(),
124+
}
125+
}
126+
127+
func ErrorFailedToConnectOperator(urlStr string) error {
128+
return Error{
129+
Kind: ErrFailedToConnectOperator,
119130
message: fmt.Sprintf("failed to connect to the operator (%s), run `cortex configure` if you need to update the operator URL", urlStr),
120131
}
121132
}
122133

123134
func ErrorCliNotInAppDir() error {
124135
return Error{
125-
Kind: ErrCliNotInAppDir,
136+
Kind: ErrCLINotInAppDir,
126137
message: "your current working directory is not in or under a cortex directory (identified via a top-level cortex.yaml file)",
127138
}
128139
}

cli/cmd/lib_client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ func StreamLogs(appName string, resourceName string, resourceType string) error
180180
connection, response, err := dialer.Dial(wsURL, header)
181181
if response == nil {
182182
cliConfig := getValidCLIConfig()
183-
return ErrorFailedToConnect(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
183+
return ErrorFailedToConnectOperator(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
184184
}
185185
defer response.Body.Close()
186186

187187
if err != nil {
188188
bodyBytes, err := ioutil.ReadAll(response.Body)
189189
if err != nil || bodyBytes == nil || string(bodyBytes) == "" {
190190
cliConfig := getValidCLIConfig()
191-
return ErrorFailedToConnect(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
191+
return ErrorFailedToConnectOperator(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
192192
}
193193
var output schema.ErrorResponse
194194
err = json.Unmarshal(bodyBytes, &output)
@@ -269,7 +269,10 @@ func (client *cortexClient) makeRequest(request *http.Request) ([]byte, error) {
269269
response, err := client.Do(request)
270270
if err != nil {
271271
cliConfig := getValidCLIConfig()
272-
return nil, ErrorFailedToConnect(cliConfig.CortexURL)
272+
if strings.HasPrefix(request.URL.String(), cliConfig.CortexURL) {
273+
return nil, ErrorFailedToConnectOperator(cliConfig.CortexURL)
274+
}
275+
return nil, ErrorFailedConnectURL(*request.URL)
273276
}
274277
defer response.Body.Close()
275278

0 commit comments

Comments
 (0)