-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
98 lines (89 loc) · 2.54 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
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
package main
import (
"encoding/json"
"net/http"
"strings"
)
const (
// Unavailable unavailable
Unavailable uint8 = iota
// BadRequest Bad Request
BadRequest
// NotAllowed not allow request
NotAllowed
// Unsupported not support
Unsupported
// Unauthorized not auth
Unauthorized
// InternalError internal error
InternalError
// NotFound not found
NotFound
// NotImplemented not implement
NotImplemented
// Forbidden Forbidden
Forbidden
)
var (
ErrNotFound = NewError("Not found", NotFound)
ErrInvalidApiKey = NewError("Invalid or missing API key", Unauthorized)
ErrMethodNotAllowed = NewError("Method not allowed", NotAllowed)
ErrUnsupportedMedia = NewError("Unsupported media type", Unsupported)
ErrOutputFormat = NewError("Unsupported output image format", BadRequest)
ErrEmptyBody = NewError("Empty image", BadRequest)
ErrMissingParamFile = NewError("Missing required param: file", BadRequest)
ErrInvalidFilePath = NewError("Invalid file path", BadRequest)
ErrInvalidImageURL = NewError("Invalid image URL", BadRequest)
ErrMissingImageSource = NewError("Cannot process the image due to missing or invalid params", BadRequest)
ErrNotImplemented = NewError("Not implemented endpoint", NotImplemented)
ErrInvalidURLSignature = NewError("Invalid URL signature", BadRequest)
ErrURLSignatureMismatch = NewError("URL signature mismatch", Forbidden)
)
type Error struct {
Message string `json:"message,omitempty"`
Code uint8 `json:"code"`
}
func (e Error) JSON() []byte {
buf, _ := json.Marshal(e)
return buf
}
func (e Error) Error() string {
return e.Message
}
func (e Error) HTTPCode() int {
if e.Code == BadRequest {
return http.StatusBadRequest
}
if e.Code == NotAllowed {
return http.StatusMethodNotAllowed
}
if e.Code == Unsupported {
return http.StatusUnsupportedMediaType
}
if e.Code == InternalError {
return http.StatusInternalServerError
}
if e.Code == Unauthorized {
return http.StatusUnauthorized
}
if e.Code == NotFound {
return http.StatusNotFound
}
if e.Code == NotImplemented {
return http.StatusNotImplemented
}
if e.Code == Forbidden {
return http.StatusForbidden
}
return http.StatusServiceUnavailable
}
func NewError(err string, code uint8) Error {
err = strings.Replace(err, "\n", "", -1)
return Error{err, code}
}
func ErrorReply(req *http.Request, w http.ResponseWriter, err Error, o ServerOptions) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(err.HTTPCode())
w.Write(err.JSON())
return err
}