Skip to content

Commit d6b0ac3

Browse files
committed
Correctly handle API errors when downloading Release Assets
If `RepositoriesService.DownloadReleaseAsset` encounters an error from the GitHub API (e.g., 404 Not Found), The error would not be returned by the method and instead the error payload would be delivered as the `io.ReadCloser`. This patch resolves this with `CheckResponse`. Fixes google#343
1 parent f2d7729 commit d6b0ac3

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎github/repos_releases.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ func (s *RepositoriesService) DownloadReleaseAsset(owner, repo string, id int) (
251251
return nil, loc, nil
252252
}
253253

254+
if err := CheckResponse(resp); err != nil {
255+
resp.Body.Close()
256+
return nil, "", err
257+
}
258+
254259
return resp.Body, "", nil
255260
}
256261

‎github/repos_releases_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,32 @@ func TestRepositoriesService_DownloadReleaseAsset_Redirect(t *testing.T) {
253253
}
254254
}
255255

256+
func TestRepositoriesService_DownloadReleaseAsset_APIError(t *testing.T) {
257+
setup()
258+
defer teardown()
259+
260+
mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
261+
testMethod(t, r, "GET")
262+
testHeader(t, r, "Accept", defaultMediaType)
263+
w.WriteHeader(http.StatusNotFound)
264+
fmt.Fprint(w, `{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}`)
265+
})
266+
267+
resp, loc, err := client.Repositories.DownloadReleaseAsset("o", "r", 1)
268+
if err == nil {
269+
t.Error("Repositories.DownloadReleaseAsset did not return an error")
270+
}
271+
272+
if resp != nil {
273+
resp.Close()
274+
t.Error("Repositories.DownloadReleaseAsset returned stream, want nil")
275+
}
276+
277+
if loc != "" {
278+
t.Error(`Repositories.DownloadReleaseAsset returned "%s", want empty ""`, loc)
279+
}
280+
}
281+
256282
func TestRepositoriesService_EditReleaseAsset(t *testing.T) {
257283
setup()
258284
defer teardown()

0 commit comments

Comments
 (0)