-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
52 lines (45 loc) · 1.31 KB
/
errors.go
File metadata and controls
52 lines (45 loc) · 1.31 KB
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
package arc
import (
"net/http"
)
// ErrorDetail describes one validation/binding problem.
type ErrorDetail struct {
Path string `json:"path,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
// APIError is standard error payload.
type APIError struct {
Status int `json:"-"`
Code string `json:"code"`
Message string `json:"message"`
Details []ErrorDetail `json:"details,omitempty"`
RequestID string `json:"requestId,omitempty"`
}
func (e *APIError) Error() string {
if e == nil {
return ""
}
if e.Message != "" {
return e.Message
}
return e.Code
}
// BadRequest creates 400 API error.
func BadRequest(code, message string) *APIError {
return &APIError{Status: http.StatusBadRequest, Code: code, Message: message}
}
// Validation creates 422 API error.
func Validation(code, message string) *APIError {
return &APIError{Status: http.StatusUnprocessableEntity, Code: code, Message: message}
}
func writeProblem(w http.ResponseWriter, status int, e *APIError, enc Encoder) {
if e == nil {
e = &APIError{Code: "internal_error", Message: "Internal server error"}
}
if status == 0 {
status = http.StatusInternalServerError
}
w.Header().Set("Content-Type", "application/problem+json")
_ = enc.Encode(w, status, e)
}