-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_errors.go
57 lines (48 loc) · 1.14 KB
/
server_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
package main
import (
"fmt"
"net/http"
)
type ServerError struct {
Message string `json:"message"`
Code string `json:"code,omitempty"`
Hint string `json:"hint,omitempty"`
StatusCode int `json:"-"`
}
func (se *ServerError) Error() string {
if se.Hint != "" {
return fmt.Sprintf("%s - %s", se.Message, se.Hint)
}
return se.Message
}
func (se *ServerError) WithHint(hint string) *ServerError {
rv := new(ServerError)
*rv = *se
rv.Hint = hint
return rv
}
var (
ErrUnsupportedMediaType = &ServerError{
Message: "Unsupported Media Type",
StatusCode: http.StatusUnsupportedMediaType,
}
ErrBadRequest = &ServerError{
Message: "Bad Request",
StatusCode: http.StatusBadRequest,
}
ErrUnauthorized = &ServerError{
Message: "Unauthorized",
StatusCode: http.StatusUnauthorized,
}
ErrAccessRestricted = &ServerError{
Message: "Access Restricted",
StatusCode: http.StatusForbidden,
}
)
func ErrUnsupportedOperator(op string) *ServerError {
return &ServerError{
Message: "Unsupported Operator",
Hint: fmt.Sprintf("operator %q is unsupported", op),
StatusCode: http.StatusBadRequest,
}
}