-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.go
72 lines (59 loc) · 1.47 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
package apierror
import (
"encoding/json"
"fmt"
)
type APIErrorResponse struct {
Success bool `json:"success" tstype:"false"`
Error APIError `json:"error" tstype:",required"`
}
type APIError struct {
ErrorCode string `json:"code"`
// Human-friendly description that can be shown to the user.
Detail string `json:"message"`
Data json.RawMessage `json:"data,omitempty"`
// The original error that caused this response, not forwarded to the end user.
// Can be nil
RawError error `json:"-"`
// HTTP status that the error should have.
Status int `json:"-"`
}
func Error(code string, detail string) *APIError {
return &APIError{
ErrorCode: code,
Detail: detail,
}
}
func InternalError(err error, detail string) *APIError {
return &APIError{
ErrorCode: ErrorCodeInternalServerError,
RawError: err,
Detail: detail,
}
}
func ErrorWithData(code string, detail string, data json.RawMessage) *APIError {
return &APIError{
ErrorCode: code,
Detail: detail,
Data: data,
}
}
func (e *APIError) WithError(err error) *APIError {
e.RawError = err
return e
}
func (e *APIError) WithStatus(status int) *APIError {
e.Status = status
return e
}
func (e *APIError) Error() string {
errMsg := ""
if e.RawError != nil {
errMsg = e.RawError.Error()
}
return fmt.Sprintf("%s (detail=%s) %s", e.ErrorCode, e.Detail, errMsg)
}
func IsAPIErrorWithCode(err error, errorCode string) bool {
aerr, ok := err.(*APIError)
return ok && aerr.ErrorCode == errorCode
}