Skip to content

Commit 8b48024

Browse files
author
Diwaker Gupta
committed
Abstract out common API error handling.
1 parent b0e551e commit 8b48024

File tree

12 files changed

+237
-2266
lines changed

12 files changed

+237
-2266
lines changed

dropbox/auth/client.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,7 @@ func (dbx *apiImpl) TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAut
9898
err = apiError
9999
return
100100
}
101-
var apiError dropbox.APIError
102-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
103-
apiError.ErrorSummary = string(body)
104-
err = apiError
105-
return
106-
}
107-
err = json.Unmarshal(body, &apiError)
108-
if err != nil {
109-
return
110-
}
111-
err = apiError
101+
err = dropbox.HandleCommonAPIErrors(resp, body)
112102
return
113103
}
114104

@@ -157,17 +147,7 @@ func (dbx *apiImpl) TokenRevoke() (err error) {
157147
err = apiError
158148
return
159149
}
160-
var apiError dropbox.APIError
161-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
162-
apiError.ErrorSummary = string(body)
163-
err = apiError
164-
return
165-
}
166-
err = json.Unmarshal(body, &apiError)
167-
if err != nil {
168-
return
169-
}
170-
err = apiError
150+
err = dropbox.HandleCommonAPIErrors(resp, body)
171151
return
172152
}
173153

dropbox/file_properties/client.go

Lines changed: 16 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,7 @@ func (dbx *apiImpl) PropertiesAdd(arg *AddPropertiesArg) (err error) {
155155
err = apiError
156156
return
157157
}
158-
var apiError dropbox.APIError
159-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
160-
apiError.ErrorSummary = string(body)
161-
err = apiError
162-
return
163-
}
164-
err = json.Unmarshal(body, &apiError)
165-
if err != nil {
166-
return
167-
}
168-
err = apiError
158+
err = dropbox.HandleCommonAPIErrors(resp, body)
169159
return
170160
}
171161

@@ -222,17 +212,7 @@ func (dbx *apiImpl) PropertiesOverwrite(arg *OverwritePropertyGroupArg) (err err
222212
err = apiError
223213
return
224214
}
225-
var apiError dropbox.APIError
226-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
227-
apiError.ErrorSummary = string(body)
228-
err = apiError
229-
return
230-
}
231-
err = json.Unmarshal(body, &apiError)
232-
if err != nil {
233-
return
234-
}
235-
err = apiError
215+
err = dropbox.HandleCommonAPIErrors(resp, body)
236216
return
237217
}
238218

@@ -289,17 +269,7 @@ func (dbx *apiImpl) PropertiesRemove(arg *RemovePropertiesArg) (err error) {
289269
err = apiError
290270
return
291271
}
292-
var apiError dropbox.APIError
293-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
294-
apiError.ErrorSummary = string(body)
295-
err = apiError
296-
return
297-
}
298-
err = json.Unmarshal(body, &apiError)
299-
if err != nil {
300-
return
301-
}
302-
err = apiError
272+
err = dropbox.HandleCommonAPIErrors(resp, body)
303273
return
304274
}
305275

@@ -361,17 +331,7 @@ func (dbx *apiImpl) PropertiesSearch(arg *PropertiesSearchArg) (res *PropertiesS
361331
err = apiError
362332
return
363333
}
364-
var apiError dropbox.APIError
365-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
366-
apiError.ErrorSummary = string(body)
367-
err = apiError
368-
return
369-
}
370-
err = json.Unmarshal(body, &apiError)
371-
if err != nil {
372-
return
373-
}
374-
err = apiError
334+
err = dropbox.HandleCommonAPIErrors(resp, body)
375335
return
376336
}
377337

@@ -433,17 +393,7 @@ func (dbx *apiImpl) PropertiesSearchContinue(arg *PropertiesSearchContinueArg) (
433393
err = apiError
434394
return
435395
}
436-
var apiError dropbox.APIError
437-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
438-
apiError.ErrorSummary = string(body)
439-
err = apiError
440-
return
441-
}
442-
err = json.Unmarshal(body, &apiError)
443-
if err != nil {
444-
return
445-
}
446-
err = apiError
396+
err = dropbox.HandleCommonAPIErrors(resp, body)
447397
return
448398
}
449399

@@ -500,17 +450,7 @@ func (dbx *apiImpl) PropertiesUpdate(arg *UpdatePropertiesArg) (err error) {
500450
err = apiError
501451
return
502452
}
503-
var apiError dropbox.APIError
504-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
505-
apiError.ErrorSummary = string(body)
506-
err = apiError
507-
return
508-
}
509-
err = json.Unmarshal(body, &apiError)
510-
if err != nil {
511-
return
512-
}
513-
err = apiError
453+
err = dropbox.HandleCommonAPIErrors(resp, body)
514454
return
515455
}
516456

@@ -569,17 +509,7 @@ func (dbx *apiImpl) TemplatesAddForTeam(arg *AddTemplateArg) (res *AddTemplateRe
569509
err = apiError
570510
return
571511
}
572-
var apiError dropbox.APIError
573-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
574-
apiError.ErrorSummary = string(body)
575-
err = apiError
576-
return
577-
}
578-
err = json.Unmarshal(body, &apiError)
579-
if err != nil {
580-
return
581-
}
582-
err = apiError
512+
err = dropbox.HandleCommonAPIErrors(resp, body)
583513
return
584514
}
585515

@@ -641,17 +571,7 @@ func (dbx *apiImpl) TemplatesAddForUser(arg *AddTemplateArg) (res *AddTemplateRe
641571
err = apiError
642572
return
643573
}
644-
var apiError dropbox.APIError
645-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
646-
apiError.ErrorSummary = string(body)
647-
err = apiError
648-
return
649-
}
650-
err = json.Unmarshal(body, &apiError)
651-
if err != nil {
652-
return
653-
}
654-
err = apiError
574+
err = dropbox.HandleCommonAPIErrors(resp, body)
655575
return
656576
}
657577

@@ -710,17 +630,7 @@ func (dbx *apiImpl) TemplatesGetForTeam(arg *GetTemplateArg) (res *GetTemplateRe
710630
err = apiError
711631
return
712632
}
713-
var apiError dropbox.APIError
714-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
715-
apiError.ErrorSummary = string(body)
716-
err = apiError
717-
return
718-
}
719-
err = json.Unmarshal(body, &apiError)
720-
if err != nil {
721-
return
722-
}
723-
err = apiError
633+
err = dropbox.HandleCommonAPIErrors(resp, body)
724634
return
725635
}
726636

@@ -782,17 +692,7 @@ func (dbx *apiImpl) TemplatesGetForUser(arg *GetTemplateArg) (res *GetTemplateRe
782692
err = apiError
783693
return
784694
}
785-
var apiError dropbox.APIError
786-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
787-
apiError.ErrorSummary = string(body)
788-
err = apiError
789-
return
790-
}
791-
err = json.Unmarshal(body, &apiError)
792-
if err != nil {
793-
return
794-
}
795-
err = apiError
695+
err = dropbox.HandleCommonAPIErrors(resp, body)
796696
return
797697
}
798698

@@ -843,17 +743,7 @@ func (dbx *apiImpl) TemplatesListForTeam() (res *ListTemplateResult, err error)
843743
err = apiError
844744
return
845745
}
846-
var apiError dropbox.APIError
847-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
848-
apiError.ErrorSummary = string(body)
849-
err = apiError
850-
return
851-
}
852-
err = json.Unmarshal(body, &apiError)
853-
if err != nil {
854-
return
855-
}
856-
err = apiError
746+
err = dropbox.HandleCommonAPIErrors(resp, body)
857747
return
858748
}
859749

@@ -907,17 +797,7 @@ func (dbx *apiImpl) TemplatesListForUser() (res *ListTemplateResult, err error)
907797
err = apiError
908798
return
909799
}
910-
var apiError dropbox.APIError
911-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
912-
apiError.ErrorSummary = string(body)
913-
err = apiError
914-
return
915-
}
916-
err = json.Unmarshal(body, &apiError)
917-
if err != nil {
918-
return
919-
}
920-
err = apiError
800+
err = dropbox.HandleCommonAPIErrors(resp, body)
921801
return
922802
}
923803

@@ -971,17 +851,7 @@ func (dbx *apiImpl) TemplatesRemoveForTeam(arg *RemoveTemplateArg) (err error) {
971851
err = apiError
972852
return
973853
}
974-
var apiError dropbox.APIError
975-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
976-
apiError.ErrorSummary = string(body)
977-
err = apiError
978-
return
979-
}
980-
err = json.Unmarshal(body, &apiError)
981-
if err != nil {
982-
return
983-
}
984-
err = apiError
854+
err = dropbox.HandleCommonAPIErrors(resp, body)
985855
return
986856
}
987857

@@ -1038,17 +908,7 @@ func (dbx *apiImpl) TemplatesRemoveForUser(arg *RemoveTemplateArg) (err error) {
1038908
err = apiError
1039909
return
1040910
}
1041-
var apiError dropbox.APIError
1042-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
1043-
apiError.ErrorSummary = string(body)
1044-
err = apiError
1045-
return
1046-
}
1047-
err = json.Unmarshal(body, &apiError)
1048-
if err != nil {
1049-
return
1050-
}
1051-
err = apiError
911+
err = dropbox.HandleCommonAPIErrors(resp, body)
1052912
return
1053913
}
1054914

@@ -1107,17 +967,7 @@ func (dbx *apiImpl) TemplatesUpdateForTeam(arg *UpdateTemplateArg) (res *UpdateT
1107967
err = apiError
1108968
return
1109969
}
1110-
var apiError dropbox.APIError
1111-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
1112-
apiError.ErrorSummary = string(body)
1113-
err = apiError
1114-
return
1115-
}
1116-
err = json.Unmarshal(body, &apiError)
1117-
if err != nil {
1118-
return
1119-
}
1120-
err = apiError
970+
err = dropbox.HandleCommonAPIErrors(resp, body)
1121971
return
1122972
}
1123973

@@ -1179,17 +1029,7 @@ func (dbx *apiImpl) TemplatesUpdateForUser(arg *UpdateTemplateArg) (res *UpdateT
11791029
err = apiError
11801030
return
11811031
}
1182-
var apiError dropbox.APIError
1183-
if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError {
1184-
apiError.ErrorSummary = string(body)
1185-
err = apiError
1186-
return
1187-
}
1188-
err = json.Unmarshal(body, &apiError)
1189-
if err != nil {
1190-
return
1191-
}
1192-
err = apiError
1032+
err = dropbox.HandleCommonAPIErrors(resp, body)
11931033
return
11941034
}
11951035

0 commit comments

Comments
 (0)