Skip to content

Commit

Permalink
feat(cli): improve error messages from servers
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhao committed Mar 6, 2018
1 parent b8a2912 commit 87a3429
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cmd/fossa/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ func makeAPIRequest(method, endpoint, apiKey string, payload []byte) ([]byte, er
return nil, fmt.Errorf("could not read API HTTP response: %s", err.Error())
}

responseStr := string(body)

if resp.StatusCode == http.StatusForbidden {
commonLogger.Debugf("Response body: %s", string(body))
commonLogger.Debugf("Response body: %s", responseStr)
return nil, fmt.Errorf("invalid API key %#v (try setting $FOSSA_API_KEY); get one at https://fossa.io", apiKey)
} else if resp.StatusCode != http.StatusOK {
commonLogger.Debugf("Response body: %s", string(body))
return nil, fmt.Errorf("bad server response: %d", resp.StatusCode)
commonLogger.Debugf("Response body: %s", responseStr)
maxResLength := 250
if len(responseStr) < maxResLength {
maxResLength = len(responseStr)
}
return nil, fmt.Errorf("bad server response (%d): %s", resp.StatusCode, responseStr[0:maxResLength])
}

commonLogger.Debugf("Got API response: %#v", string(body))
Expand Down

0 comments on commit 87a3429

Please sign in to comment.