forked from viliproject/vili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
117 lines (105 loc) · 2.68 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
package errors
import (
"errors"
"net/http"
)
// ErrorResponse is the json wrapper for API error responses
type ErrorResponse struct {
Status int `json:"-"`
ErrorType string `json:"type"`
Message string `json:"message"`
Params map[string][]string `json:"params,omitempty"`
}
func (e *ErrorResponse) Error() string {
return e.Message
}
// New returns a new error
func New(err string) error {
return errors.New(err)
}
// InternalServerError returns an internal server error response
func InternalServerError() *ErrorResponse {
return &ErrorResponse{
Status: http.StatusInternalServerError,
ErrorType: "internal_server_error",
Message: "Internal server error",
}
}
// UnauthorizedError returns an unauthorized error response
func UnauthorizedError(message string) *ErrorResponse {
if message == "" {
message = "Unauthorized"
}
return &ErrorResponse{
Status: http.StatusUnauthorized,
ErrorType: "unauthorized",
Message: message,
}
}
// ForbiddenError returns a forbidden error response
func ForbiddenError(message string) *ErrorResponse {
if message == "" {
message = "Forbidden"
}
return &ErrorResponse{
Status: http.StatusForbidden,
ErrorType: "forbidden",
Message: message,
}
}
// BadRequestError returns a bad request error response
func BadRequestError(message string) *ErrorResponse {
if message == "" {
message = "Bad request"
}
return &ErrorResponse{
Status: http.StatusBadRequest,
ErrorType: "bad_request",
Message: message,
}
}
// NotFoundError returns a not found error response
func NotFoundError(message string) *ErrorResponse {
if message == "" {
message = "Not found"
}
return &ErrorResponse{
Status: http.StatusNotFound,
ErrorType: "not_found",
Message: message,
}
}
// ValidationError returns a validation error response
func ValidationError(message string, params map[string][]string) *ErrorResponse {
if message == "" {
message = "Validation error"
}
return &ErrorResponse{
Status: http.StatusBadRequest,
ErrorType: "validation_error",
Message: message,
Params: params,
}
}
// ConflictError returns a not found error response
func ConflictError(message string) *ErrorResponse {
if message == "" {
message = "Conflict"
}
return &ErrorResponse{
Status: http.StatusConflict,
ErrorType: "conflict",
Message: message,
}
}
// RequestTooLargeError returns a request too large error response
func RequestTooLargeError(message string) *ErrorResponse {
if message == "" {
message = "Request entity too large"
}
return &ErrorResponse{
Status: http.StatusRequestEntityTooLarge,
ErrorType: "request_entity_too_large",
Message: message,
}
}