Skip to content

Commit

Permalink
Support proper handling of Auth and Access errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Diwaker Gupta committed Dec 7, 2018
1 parent 56e5f65 commit dd3c8f5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
33 changes: 31 additions & 2 deletions dropbox/auth/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,42 @@ import (
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
)

// AuthAPIError wraps AuthError
type AuthAPIError struct {
dropbox.APIError
AuthError *AuthError `json:"error"`
}

// AccessAPIError wraps AccessError
type AccessAPIError struct {
dropbox.APIError
AccessError *AccessError `json:"error"`
}

// RateLimitAPIError wraps RateLimitError
type RateLimitAPIError struct {
dropbox.APIError
RateLimitError *RateLimitError `json:"error"`
}

// HandleCommonAuthErrors handles common authentication errors
func HandleCommonAuthErrors(c dropbox.Config, resp *http.Response, body []byte) error {
if resp.StatusCode == http.StatusTooManyRequests {
switch resp.StatusCode {
case http.StatusUnauthorized:
var apiError AuthAPIError
if err := json.Unmarshal(body, &apiError); err != nil {
c.LogDebug("Error unmarshaling '%s' into JSON", body)
return err
}
return apiError
case http.StatusForbidden:
var apiError AccessAPIError
if err := json.Unmarshal(body, &apiError); err != nil {
c.LogDebug("Error unmarshaling '%s' into JSON", body)
return err
}
return apiError
case http.StatusTooManyRequests:
var apiError RateLimitAPIError
// Check content-type
contentType, _, _ := mime.ParseMediaType(resp.Header.Get("content-type"))
Expand All @@ -33,6 +61,7 @@ func HandleCommonAuthErrors(c dropbox.Config, resp *http.Response, body []byte)
apiError.RateLimitError.RetryAfter = uint64(timeout)
}
return apiError
default:
return nil
}
return nil
}
2 changes: 1 addition & 1 deletion dropbox/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
hostAPI = "api"
hostContent = "content"
hostNotify = "notify"
sdkVersion = "5.2.0"
sdkVersion = "5.3.0"
specVersion = "097e9ba"
)

Expand Down
58 changes: 58 additions & 0 deletions dropbox/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,61 @@ func TestRateLimitJSON(t *testing.T) {
t.Errorf("Unexpected reason: %v\n", re.RateLimitError.Reason)
}
}

func TestAuthError(t *testing.T) {
eString := `{"error_summary": "user_suspended/...", "error": {".tag": "user_suspended"}}`
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(eString))
}))
defer ts.Close()

config := dropbox.Config{Client: ts.Client(), LogLevel: dropbox.LogDebug,
URLGenerator: func(hostType string, style string, namespace string, route string) string {
return generateURL(ts.URL, namespace, route)
}}
client := users.New(config)
_, e := client.GetCurrentAccount()
re, ok := e.(auth.AuthAPIError)
if !ok {
t.Errorf("Unexpected error type: %T\n", e)
}
fmt.Printf("ERROR is %v\n", re)
if re.AuthError.Tag != auth.AuthErrorUserSuspended {
t.Errorf("Unexpected tag: %s\n", re.AuthError.Tag)
}
}

func TestAccessError(t *testing.T) {
eString := `{"error_summary": "access_error/...",
"error": {
".tag": "paper_access_denied",
"paper_access_denied": {".tag": "not_paper_user"}
}}`
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(eString))
}))
defer ts.Close()

config := dropbox.Config{Client: ts.Client(), LogLevel: dropbox.LogDebug,
URLGenerator: func(hostType string, style string, namespace string, route string) string {
return generateURL(ts.URL, namespace, route)
}}
client := users.New(config)
_, e := client.GetCurrentAccount()
re, ok := e.(auth.AccessAPIError)
if !ok {
t.Errorf("Unexpected error type: %T\n", e)
}
if re.AccessError.Tag != auth.AccessErrorPaperAccessDenied {
t.Errorf("Unexpected tag: %s\n", re.AccessError.Tag)
}
if re.AccessError.PaperAccessDenied.Tag != auth.PaperAccessErrorNotPaperUser {
t.Errorf("Unexpected tag: %s\n", re.AccessError.PaperAccessDenied.Tag)
}
}

0 comments on commit dd3c8f5

Please sign in to comment.