Skip to content

Commit 70dde67

Browse files
committed
Improve error handling
1 parent 8433352 commit 70dde67

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

api/api.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (c *APIClient) doRequest(params requestParams) (*http.Response, error) {
8888
}
8989

9090
req.Header.Add("Accept", "application/json")
91+
req.Header.Add("trakt-api-version", "2")
9192

9293
if params.body != nil {
9394
req.Header.Add("Content-Type", "application/json")
@@ -235,7 +236,6 @@ type Pagination struct {
235236
}
236237

237238
func (c *APIClient) GetUserHistory(user string, params PaginationsParams) (UserHistory, Pagination, error) {
238-
var resp UserHistory
239239
httpResp, err := c.doRequest(requestParams{
240240
method: http.MethodGet,
241241
path: fmt.Sprintf("/users/%s/history", user),
@@ -248,19 +248,21 @@ func (c *APIClient) GetUserHistory(user string, params PaginationsParams) (UserH
248248
}
249249
defer httpResp.Body.Close()
250250

251-
var pagination Pagination
252-
if httpResp.StatusCode == 200 {
253-
err = json.NewDecoder(httpResp.Body).Decode(&resp)
254-
if err != nil {
255-
return nil, Pagination{}, err
256-
}
251+
if httpResp.StatusCode != 200 {
252+
return nil, Pagination{}, fmt.Errorf("failed to get user history: %s", httpResp.Status)
253+
}
257254

258-
pagination = Pagination{
259-
Page: httpResp.Header.Get("X-Pagination-Page"),
260-
Limit: httpResp.Header.Get("X-Pagination-Limit"),
261-
PageCount: httpResp.Header.Get("X-Pagination-Page-Count"),
262-
ItemCount: httpResp.Header.Get("X-Pagination-Item-Count"),
263-
}
255+
var resp UserHistory
256+
err = json.NewDecoder(httpResp.Body).Decode(&resp)
257+
if err != nil {
258+
return nil, Pagination{}, err
259+
}
260+
261+
pagination := Pagination{
262+
Page: httpResp.Header.Get("X-Pagination-Page"),
263+
Limit: httpResp.Header.Get("X-Pagination-Limit"),
264+
PageCount: httpResp.Header.Get("X-Pagination-Page-Count"),
265+
ItemCount: httpResp.Header.Get("X-Pagination-Item-Count"),
264266
}
265267

266268
return resp, pagination, nil
@@ -324,12 +326,14 @@ func (c *APIClient) GetUserSettings() (UserSettings, error) {
324326
}
325327
defer httpResp.Body.Close()
326328

329+
if httpResp.StatusCode != 200 {
330+
return UserSettings{}, fmt.Errorf("failed to get user settings: %s", httpResp.Status)
331+
}
332+
327333
var resp UserSettings
328-
if httpResp.StatusCode == 200 {
329-
err = json.NewDecoder(httpResp.Body).Decode(&resp)
330-
if err != nil {
331-
return UserSettings{}, err
332-
}
334+
err = json.NewDecoder(httpResp.Body).Decode(&resp)
335+
if err != nil {
336+
return UserSettings{}, err
333337
}
334338

335339
return resp, nil

0 commit comments

Comments
 (0)