Skip to content
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
17 changes: 12 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ func FetchAssignments(config configuration.Config, path string) (as []Assignment
return
}

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Error fetching assignments. HTTP Status Code: %d", resp.StatusCode)
return
}

body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()

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

if resp.StatusCode != http.StatusOK {
var apiError struct { Error string `json:"error"` }
err = json.Unmarshal(body, &apiError)
if err != nil {
err = fmt.Errorf("Error parsing API response: [%v]", err)
return
}

err = fmt.Errorf("Error fetching assignments. HTTP Status Code: %d\n%s", resp.StatusCode, apiError.Error)
return
}

var fr struct {
Assignments []Assignment
}
Expand Down
3 changes: 2 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var fetchHandler = func(rw http.ResponseWriter, r *http.Request) {
return
}
if apiKey != "myApiKey" {
rw.WriteHeader(http.StatusForbidden)
rw.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(rw, `{"error": "Unable to identify user"}`)
return
}
Expand Down Expand Up @@ -80,6 +80,7 @@ func TestFetchWithIncorrectKey(t *testing.T) {

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

server.Close()
}
Expand Down