Skip to content

Show correct URL upon failed HTTP request from CLI #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions cli/cmd/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"fmt"
"net/url"

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

const (
ErrUnknown ErrorKind = iota
ErrCliAlreadyInAppDir
ErrCLIAlreadyInAppDir
ErrAPINotReady
ErrAPINotFound
ErrFailedToConnect
ErrCliNotInAppDir
ErrFailedToConnectURL
ErrFailedToConnectOperator
ErrCLINotInAppDir
)

var errorKinds = []string{
"err_unknown",
"err_cli_already_in_app_dir",
"err_api_not_ready",
"err_api_not_found",
"err_failed_to_connect",
"err_failed_to_connect_url",
"err_failed_to_connect_operator",
"err_cli_not_in_app_dir",
}

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

func (t ErrorKind) String() string {
return errorKinds[t]
Expand Down Expand Up @@ -94,7 +97,7 @@ func (e Error) Error() string {

func ErrorCliAlreadyInAppDir(dirPath string) error {
return Error{
Kind: ErrCliAlreadyInAppDir,
Kind: ErrCLIAlreadyInAppDir,
message: fmt.Sprintf("your current working directory is already in a cortex directory (%s)", dirPath),
}
}
Expand All @@ -113,16 +116,24 @@ func ErrorAPINotFound(apiName string) error {
}
}

func ErrorFailedToConnect(urlStr string) error {
func ErrorFailedConnectURL(url url.URL) error {
url.RawQuery = ""
return Error{
Kind: ErrFailedToConnect,
Kind: ErrFailedToConnectURL,
message: "failed to connect to " + url.String(),
}
}

func ErrorFailedToConnectOperator(urlStr string) error {
return Error{
Kind: ErrFailedToConnectOperator,
message: fmt.Sprintf("failed to connect to the operator (%s), run `cortex configure` if you need to update the operator URL", urlStr),
}
}

func ErrorCliNotInAppDir() error {
return Error{
Kind: ErrCliNotInAppDir,
Kind: ErrCLINotInAppDir,
message: "your current working directory is not in or under a cortex directory (identified via a top-level cortex.yaml file)",
}
}
9 changes: 6 additions & 3 deletions cli/cmd/lib_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ func StreamLogs(appName string, resourceName string, resourceType string) error
connection, response, err := dialer.Dial(wsURL, header)
if response == nil {
cliConfig := getValidCLIConfig()
return ErrorFailedToConnect(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
return ErrorFailedToConnectOperator(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
}
defer response.Body.Close()

if err != nil {
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil || bodyBytes == nil || string(bodyBytes) == "" {
cliConfig := getValidCLIConfig()
return ErrorFailedToConnect(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
return ErrorFailedToConnectOperator(strings.Replace(cliConfig.CortexURL, "http", "ws", 1))
}
var output schema.ErrorResponse
err = json.Unmarshal(bodyBytes, &output)
Expand Down Expand Up @@ -269,7 +269,10 @@ func (client *cortexClient) makeRequest(request *http.Request) ([]byte, error) {
response, err := client.Do(request)
if err != nil {
cliConfig := getValidCLIConfig()
return nil, ErrorFailedToConnect(cliConfig.CortexURL)
if strings.HasPrefix(request.URL.String(), cliConfig.CortexURL) {
return nil, ErrorFailedToConnectOperator(cliConfig.CortexURL)
}
return nil, ErrorFailedConnectURL(*request.URL)
}
defer response.Body.Close()

Expand Down