Skip to content

Commit dd3c8f5

Browse files
author
Diwaker Gupta
committed
Support proper handling of Auth and Access errors.
1 parent 56e5f65 commit dd3c8f5

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

dropbox/auth/sdk.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,42 @@ import (
99
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
1010
)
1111

12+
// AuthAPIError wraps AuthError
13+
type AuthAPIError struct {
14+
dropbox.APIError
15+
AuthError *AuthError `json:"error"`
16+
}
17+
18+
// AccessAPIError wraps AccessError
19+
type AccessAPIError struct {
20+
dropbox.APIError
21+
AccessError *AccessError `json:"error"`
22+
}
23+
24+
// RateLimitAPIError wraps RateLimitError
1225
type RateLimitAPIError struct {
1326
dropbox.APIError
1427
RateLimitError *RateLimitError `json:"error"`
1528
}
1629

1730
// HandleCommonAuthErrors handles common authentication errors
1831
func HandleCommonAuthErrors(c dropbox.Config, resp *http.Response, body []byte) error {
19-
if resp.StatusCode == http.StatusTooManyRequests {
32+
switch resp.StatusCode {
33+
case http.StatusUnauthorized:
34+
var apiError AuthAPIError
35+
if err := json.Unmarshal(body, &apiError); err != nil {
36+
c.LogDebug("Error unmarshaling '%s' into JSON", body)
37+
return err
38+
}
39+
return apiError
40+
case http.StatusForbidden:
41+
var apiError AccessAPIError
42+
if err := json.Unmarshal(body, &apiError); err != nil {
43+
c.LogDebug("Error unmarshaling '%s' into JSON", body)
44+
return err
45+
}
46+
return apiError
47+
case http.StatusTooManyRequests:
2048
var apiError RateLimitAPIError
2149
// Check content-type
2250
contentType, _, _ := mime.ParseMediaType(resp.Header.Get("content-type"))
@@ -33,6 +61,7 @@ func HandleCommonAuthErrors(c dropbox.Config, resp *http.Response, body []byte)
3361
apiError.RateLimitError.RetryAfter = uint64(timeout)
3462
}
3563
return apiError
64+
default:
65+
return nil
3666
}
37-
return nil
3867
}

dropbox/sdk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
hostAPI = "api"
3838
hostContent = "content"
3939
hostNotify = "notify"
40-
sdkVersion = "5.2.0"
40+
sdkVersion = "5.3.0"
4141
specVersion = "097e9ba"
4242
)
4343

dropbox/sdk_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,61 @@ func TestRateLimitJSON(t *testing.T) {
111111
t.Errorf("Unexpected reason: %v\n", re.RateLimitError.Reason)
112112
}
113113
}
114+
115+
func TestAuthError(t *testing.T) {
116+
eString := `{"error_summary": "user_suspended/...", "error": {".tag": "user_suspended"}}`
117+
ts := httptest.NewServer(http.HandlerFunc(
118+
func(w http.ResponseWriter, r *http.Request) {
119+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
120+
w.WriteHeader(http.StatusUnauthorized)
121+
w.Write([]byte(eString))
122+
}))
123+
defer ts.Close()
124+
125+
config := dropbox.Config{Client: ts.Client(), LogLevel: dropbox.LogDebug,
126+
URLGenerator: func(hostType string, style string, namespace string, route string) string {
127+
return generateURL(ts.URL, namespace, route)
128+
}}
129+
client := users.New(config)
130+
_, e := client.GetCurrentAccount()
131+
re, ok := e.(auth.AuthAPIError)
132+
if !ok {
133+
t.Errorf("Unexpected error type: %T\n", e)
134+
}
135+
fmt.Printf("ERROR is %v\n", re)
136+
if re.AuthError.Tag != auth.AuthErrorUserSuspended {
137+
t.Errorf("Unexpected tag: %s\n", re.AuthError.Tag)
138+
}
139+
}
140+
141+
func TestAccessError(t *testing.T) {
142+
eString := `{"error_summary": "access_error/...",
143+
"error": {
144+
".tag": "paper_access_denied",
145+
"paper_access_denied": {".tag": "not_paper_user"}
146+
}}`
147+
ts := httptest.NewServer(http.HandlerFunc(
148+
func(w http.ResponseWriter, r *http.Request) {
149+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
150+
w.WriteHeader(http.StatusForbidden)
151+
w.Write([]byte(eString))
152+
}))
153+
defer ts.Close()
154+
155+
config := dropbox.Config{Client: ts.Client(), LogLevel: dropbox.LogDebug,
156+
URLGenerator: func(hostType string, style string, namespace string, route string) string {
157+
return generateURL(ts.URL, namespace, route)
158+
}}
159+
client := users.New(config)
160+
_, e := client.GetCurrentAccount()
161+
re, ok := e.(auth.AccessAPIError)
162+
if !ok {
163+
t.Errorf("Unexpected error type: %T\n", e)
164+
}
165+
if re.AccessError.Tag != auth.AccessErrorPaperAccessDenied {
166+
t.Errorf("Unexpected tag: %s\n", re.AccessError.Tag)
167+
}
168+
if re.AccessError.PaperAccessDenied.Tag != auth.PaperAccessErrorNotPaperUser {
169+
t.Errorf("Unexpected tag: %s\n", re.AccessError.PaperAccessDenied.Tag)
170+
}
171+
}

0 commit comments

Comments
 (0)