Skip to content

Commit b3c3d6f

Browse files
committed
Improve Internal Server Error response message.
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
1 parent dd9f46b commit b3c3d6f

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

api/client.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"strings"
89

910
"github.com/exercism/cli/config"
1011
)
1112

13+
const (
14+
apiIssueTracker = "https://github.com/exercism/exercism.io/issues"
15+
xapiIssueTracker = "https://github.com/exercism/x-api/issues"
16+
)
17+
1218
var (
1319
// UserAgent lets the API know where the call is being made from.
1420
// It's set from main() so that we have access to the version.
@@ -53,14 +59,21 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
5359
return nil, err
5460
}
5561

56-
if res.StatusCode == http.StatusNoContent {
62+
switch res.StatusCode {
63+
case http.StatusNoContent:
5764
return res, nil
58-
}
59-
60-
if v != nil {
61-
defer res.Body.Close()
62-
if err := json.NewDecoder(res.Body).Decode(v); err != nil {
63-
return nil, fmt.Errorf("error parsing API response - %s", err)
65+
case http.StatusInternalServerError:
66+
issueTracker := apiIssueTracker
67+
if strings.Contains(req.URL.Host, "x.exercism.io") {
68+
issueTracker = xapiIssueTracker
69+
}
70+
return nil, fmt.Errorf("an internal server error was received.\nPlease file a bug report with the contents of 'exercism debug' at: %s ", issueTracker)
71+
default:
72+
if v != nil {
73+
defer res.Body.Close()
74+
if err := json.NewDecoder(res.Body).Decode(v); err != nil {
75+
return nil, fmt.Errorf("error parsing API response - %s", err)
76+
}
6477
}
6578
}
6679

0 commit comments

Comments
 (0)