-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
59 lines (51 loc) · 1.4 KB
/
error.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
package gofantasy
import (
"fmt"
)
// Known errors
var (
ErrNotImplemented = fmt.Errorf("method not implemented")
ErrBadRequest = fmt.Errorf("the API doesn’t understand the request. Something is missing")
ErrUnauthorized = fmt.Errorf("the API key is missing or misspelled")
ErrForbidden = fmt.Errorf("the API key doesn’t have the roles required to perform the request")
ErrNotFound = fmt.Errorf("the API understands the request but a parameter is missing or misspelled")
ErrInternalServer = fmt.Errorf("something went wrong on the server’s side")
ErrUnknown = fmt.Errorf("an unknown error was returned")
)
type HTTPError struct {
StatusCode int `json:"statusCode"`
Status string `json:"status"`
Message string `json:"message"`
ReportErrorURL string `json:"reportErrorUrl"`
}
type RequestError struct {
StatusCode int
Err error
}
func (e *RequestError) Error() string {
if e.Err != nil {
return e.Err.Error()
}
return fmt.Sprintf("status code %d", e.StatusCode)
}
type ErrorResponse struct {
Error *HTTPError `json:"error,omitempty"`
}
func (m *HTTPError) Error() string {
return m.Message
}
func (m *HTTPError) Cause() error {
switch m.StatusCode {
case 400:
return ErrBadRequest
case 401:
return ErrUnauthorized
case 403:
return ErrForbidden
case 404:
return ErrNotFound
case 500:
return ErrInternalServer
}
return ErrUnknown
}