-
Notifications
You must be signed in to change notification settings - Fork 2
/
responseerror.go
133 lines (111 loc) · 3.29 KB
/
responseerror.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package files_sdk
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/Files-com/files-sdk-go/v2/lib"
)
type ResponseError struct {
Type string `json:"type"`
Title string `json:"title"`
ErrorMessage string `json:"error"`
HttpCode int `json:"http-code"`
Errors []ResponseError `json:"errors"`
Data Data `json:"data"`
}
const (
DestinationExists = "processing-failure/destination-exists"
)
func IsDestinationExistsError(err error) bool {
re, ok := err.(ResponseError)
return ok && re.Type == DestinationExists
}
type SignRequest struct {
Version string `json:"version"`
KeyHandle string `json:"keyHandle"`
}
type U2fSignRequests struct {
AppId string `json:"app_id"`
Challenge string `json:"challenge"`
SignRequest SignRequest `json:"sign_request"`
}
type Data struct {
U2fSIgnRequests []U2fSignRequests `json:"u2f_sign_requests,omitempty"`
PartialSessionId string `json:"partial_session_id,omitempty"`
TwoFactorAuthenticationMethod []string `json:"two_factor_authentication_methods,omitempty"`
// Download Request Status
BytesTransferred int64 `json:"bytes_transferred"`
Status string `json:"status"`
StartedAt time.Time `json:"started_at"`
CompletedAt time.Time `json:"completed_at"`
TouchedAt time.Time `json:"touched_at"`
}
func (e ResponseError) Error() string {
return fmt.Sprintf("%v - `%v`", e.Title, e.ErrorMessage)
}
func (e ResponseError) IsNil() bool {
return e.ErrorMessage == ""
}
func (e ResponseError) Is(err error) bool {
_, ok := err.(ResponseError)
return ok
}
func (e *ResponseError) UnmarshalJSON(data []byte) error {
type re ResponseError
var v re
err := json.Unmarshal(data, &v)
if err != nil {
jsonError, ok := err.(*json.UnmarshalTypeError)
if ok && jsonError.Field == "" {
if jsonError.Value == "string" {
var str string
json.Unmarshal(data, &str)
v.ErrorMessage = str
} else if jsonError.Value != "array" {
return err
}
} else if ok && jsonError.Field == "http-code" {
tmp := make(map[string]interface{})
json.Unmarshal(data, &tmp)
intVar, _ := strconv.Atoi(tmp["http-code"].(string))
v.HttpCode = intVar
} else {
return err
}
jsonSyntaxErr, ok := err.(*json.SyntaxError)
if ok && jsonSyntaxErr.Error() == "invalid character '<' looking for beginning of value" {
return fmt.Errorf(string(data))
}
}
*e = ResponseError(v)
return nil
}
func APIError(callbacks ...func(ResponseError) ResponseError) func(res *http.Response) error {
return func(res *http.Response) error {
if lib.IsNonOkStatus(res) && lib.IsHTML(res) && res.Header.Get("X-Request-Id") != "" && res.Header.Get("Server") == "nginx" {
return fmt.Errorf("files.com Server error - request id: %v", res.Header.Get("X-Request-Id"))
}
if lib.IsNonOkStatus(res) && lib.IsJSON(res) {
data, err := io.ReadAll(res.Body)
if err != nil {
return lib.NonOkError(res)
}
re := ResponseError{}
err = re.UnmarshalJSON(data)
if err != nil {
return lib.NonOkError(res)
}
if re.IsNil() {
return lib.NonOkError(res)
}
for _, callback := range callbacks {
re = callback(re)
}
return re
}
return nil
}
}