Skip to content

Commit f221b80

Browse files
committed
Merge pull request #62 from Tonkpils/show-error-message
Display error message received from fetch current endpoint
2 parents 69d9699 + aacc9a8 commit f221b80

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

api.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ func FetchAssignments(config configuration.Config, path string) (as []Assignment
4848
return
4949
}
5050

51-
if resp.StatusCode != http.StatusOK {
52-
err = fmt.Errorf("Error fetching assignments. HTTP Status Code: %d", resp.StatusCode)
53-
return
54-
}
55-
5651
body, err := ioutil.ReadAll(resp.Body)
5752
resp.Body.Close()
5853

@@ -61,6 +56,18 @@ func FetchAssignments(config configuration.Config, path string) (as []Assignment
6156
return
6257
}
6358

59+
if resp.StatusCode != http.StatusOK {
60+
var apiError struct { Error string `json:"error"` }
61+
err = json.Unmarshal(body, &apiError)
62+
if err != nil {
63+
err = fmt.Errorf("Error parsing API response: [%v]", err)
64+
return
65+
}
66+
67+
err = fmt.Errorf("Error fetching assignments. HTTP Status Code: %d\n%s", resp.StatusCode, apiError.Error)
68+
return
69+
}
70+
6471
var fr struct {
6572
Assignments []Assignment
6673
}

api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var fetchHandler = func(rw http.ResponseWriter, r *http.Request) {
3535
return
3636
}
3737
if apiKey != "myApiKey" {
38-
rw.WriteHeader(http.StatusForbidden)
38+
rw.WriteHeader(http.StatusUnauthorized)
3939
fmt.Fprintf(rw, `{"error": "Unable to identify user"}`)
4040
return
4141
}
@@ -80,6 +80,7 @@ func TestFetchWithIncorrectKey(t *testing.T) {
8080

8181
assert.Error(t, err)
8282
assert.Equal(t, len(assignments), 0)
83+
assert.Contains(t, fmt.Sprintf("%s", err), "Unable to identify user")
8384

8485
server.Close()
8586
}

0 commit comments

Comments
 (0)