-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy patherrors.go
190 lines (171 loc) · 4.13 KB
/
errors.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package controller
import (
"errors"
"fmt"
"net/http"
)
var (
ErrMultipartFormFileNotFound = &APIError{
http.StatusBadRequest,
"file[] not found in Multipart form",
errors.New("file[] not found in Multipart form"), //nolint
nil,
}
ErrMultipartFileWrong = &APIError{
http.StatusBadRequest,
"wrong file data in multipart form, one needs to be specified",
errors.New("wrong file data in multipart form, one needs to be specified"), //nolint
nil,
}
ErrWrongDate = &APIError{
http.StatusBadRequest,
"couldn't parse date",
errors.New("couldn't parse date"), //nolint
nil,
}
ErrMetadataLength = &APIError{
http.StatusBadRequest,
"file metadata length doesn't match number of files in request",
errors.New("file metadata length doesn't match number of files in request"), //nolint
nil,
}
ErrBucketNotFound = &APIError{
http.StatusNotFound,
"bucket not found",
errors.New("bucket not found"), //nolint
nil,
}
ErrFileNotFound = &APIError{
http.StatusNotFound,
"file not found",
errors.New("file not found"), //nolint
nil,
}
ErrFileNotUploaded = &APIError{
http.StatusForbidden,
"file not uploaded",
errors.New("file not uploaded"), //nolint
nil,
}
)
// Used to standardized the output of the handers' response.
type ErrorResponse struct {
Message string `json:"message"`
Data map[string]interface{} `json:"data,omitempty"`
}
type APIError struct {
statusCode int
publicMessage string
err error
data map[string]interface{}
}
func InternalServerError(err error) *APIError {
return &APIError{
statusCode: http.StatusInternalServerError,
publicMessage: "an internal server error occurred",
err: err,
data: nil,
}
}
func ForbiddenError(err error, publicMessage string) *APIError {
return &APIError{
statusCode: http.StatusForbidden,
publicMessage: publicMessage,
err: err,
data: nil,
}
}
func FailedToInitializeMetadataError(err error) *APIError {
return &APIError{
statusCode: http.StatusForbidden,
publicMessage: "you are not authorized",
err: err,
data: nil,
}
}
func FileTooBigError(filename string, size, maxSize int) *APIError {
return &APIError{
statusCode: http.StatusBadRequest,
publicMessage: "file too big",
err: fmt.Errorf("file %s too big: %d > %d", filename, size, maxSize), //nolint
data: map[string]interface{}{
"filename": filename,
"size": size,
"maxSize": maxSize,
},
}
}
func FileTooSmallError(filename string, size, minSize int) *APIError {
return &APIError{
statusCode: http.StatusBadRequest,
publicMessage: "file too small",
err: fmt.Errorf("file %s too small: %d < %d", filename, size, minSize), //nolint
data: map[string]interface{}{
"filename": filename,
"size": size,
"minSize": minSize,
},
}
}
func WrongMetadataFormatError(err error) *APIError {
return &APIError{
statusCode: http.StatusBadRequest,
publicMessage: "couldn't decode metadata",
err: err,
}
}
func BadDataError(err error, publicMessage string) *APIError {
return &APIError{
statusCode: http.StatusBadRequest,
publicMessage: publicMessage,
err: err,
}
}
func NewAPIError(
statusCode int,
publicMessage string,
err error,
data map[string]interface{},
) *APIError {
return &APIError{
statusCode: statusCode,
publicMessage: publicMessage,
err: err,
data: data,
}
}
func (a *APIError) StatusCode() int {
if a == nil {
return 0
}
return a.statusCode
}
func (a *APIError) PublicResponse() *ErrorResponse {
return &ErrorResponse{
Message: a.publicMessage,
Data: a.data,
}
}
func (a *APIError) Error() string {
return a.err.Error()
}
func (a *APIError) ExtendError(msg string) *APIError {
a.err = fmt.Errorf(fmt.Sprintf("%s: %s", msg, a.err.Error())) //nolint
return a
}
func (a *APIError) GetDataString(k string) string {
if a.data == nil {
return ""
}
s, ok := a.data[k].(string)
if !ok {
return ""
}
return s
}
func (a *APIError) SetData(k string, v any) {
if a.data == nil {
a.data = make(map[string]interface{})
}
a.data[k] = v
}