File: sdk.go

package info (click to toggle)
golang-github-dropbox-dropbox-sdk-go-unofficial 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,324 kB
  • sloc: python: 481; sh: 22; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 1,884 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package auth

import (
	"encoding/json"
	"net/http"

	"github.com/dropbox/dropbox-sdk-go-unofficial/v6/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"`
}

// Bad input parameter.
type BadRequest struct {
	dropbox.APIError
}

// An error occurred on the Dropbox servers. Check status.dropbox.com for announcements about
// Dropbox service issues.
type ServerError struct {
	dropbox.APIError
	StatusCode int
}

func ParseError(err error, appError error) error {
	sdkErr, ok := err.(dropbox.SDKInternalError)
	if !ok {
		return err
	}

	if sdkErr.StatusCode >= 500 && sdkErr.StatusCode <= 599 {
		return ServerError{
			APIError: dropbox.APIError{
				ErrorSummary: sdkErr.Content,
			},
		}
	}

	switch sdkErr.StatusCode {
	case http.StatusBadRequest:
		return BadRequest{
			APIError: dropbox.APIError{
				ErrorSummary: sdkErr.Content,
			},
		}
	case http.StatusUnauthorized:
		var apiError AuthAPIError
		if pErr := json.Unmarshal([]byte(sdkErr.Content), &apiError); pErr != nil {
			return pErr
		}

		return apiError
	case http.StatusForbidden:
		var apiError AccessAPIError
		if pErr := json.Unmarshal([]byte(sdkErr.Content), &apiError); pErr != nil {
			return pErr
		}

		return apiError
	case http.StatusTooManyRequests:
		var apiError RateLimitAPIError
		if pErr := json.Unmarshal([]byte(sdkErr.Content), &apiError); pErr != nil {
			return pErr
		}

		return apiError
	case http.StatusConflict:
		if pErr := json.Unmarshal([]byte(sdkErr.Content), appError); pErr != nil {
			return pErr
		}

		return appError
	}

	return err
}