Skip to content

Commit

Permalink
Improve Internal Server Error response message.
Browse files Browse the repository at this point in the history
This does two things:
- Doesn't attempt to parse the response body on 500 responses
- Adds a message depending on which API was attempted to file an issue with the corresponding GitHub issue tracker
  • Loading branch information
Tonkpils committed Jun 26, 2015
1 parent dd9f46b commit b3c3d6f
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/exercism/cli/config"
)

const (
apiIssueTracker = "https://github.com/exercism/exercism.io/issues"
xapiIssueTracker = "https://github.com/exercism/x-api/issues"
)

var (
// UserAgent lets the API know where the call is being made from.
// It's set from main() so that we have access to the version.
Expand Down Expand Up @@ -53,14 +59,21 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
return nil, err
}

if res.StatusCode == http.StatusNoContent {
switch res.StatusCode {
case http.StatusNoContent:
return res, nil
}

if v != nil {
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(v); err != nil {
return nil, fmt.Errorf("error parsing API response - %s", err)
case http.StatusInternalServerError:
issueTracker := apiIssueTracker
if strings.Contains(req.URL.Host, "x.exercism.io") {
issueTracker = xapiIssueTracker
}
return nil, fmt.Errorf("an internal server error was received.\nPlease file a bug report with the contents of 'exercism debug' at: %s ", issueTracker)
default:
if v != nil {
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(v); err != nil {
return nil, fmt.Errorf("error parsing API response - %s", err)
}
}
}

Expand Down

0 comments on commit b3c3d6f

Please sign in to comment.