-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_type_enum.go
64 lines (56 loc) · 1.6 KB
/
error_type_enum.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
package berr
// ErrorType denotes common types of errors and additionally provides standard string and HTTP type mappings.
type ErrorType int
const (
UndefinedErrorType ErrorType = iota
InvalidErrorType
ApplicationErrorType
AuthenticationErrorType
AuthorizationErrorType
NotFoundErrorType
ConflictErrorType
ValueMissingErrorType
ValueInvalidErrorType
UnimplementedErrorType
TimeoutErrorType
)
var errorTypeStringMap = map[ErrorType]string{
UndefinedErrorType: "undefined",
InvalidErrorType: "invalid",
ApplicationErrorType: "application",
AuthenticationErrorType: "authentication",
AuthorizationErrorType: "authorization",
NotFoundErrorType: "not_found",
ConflictErrorType: "conflict",
ValueInvalidErrorType: "value_invalid",
ValueMissingErrorType: "value_missing",
UnimplementedErrorType: "unimplemented",
TimeoutErrorType: "timeout",
}
var errorTypeHTTPCodeMap = map[ErrorType]int{
UndefinedErrorType: 500,
InvalidErrorType: 500,
ApplicationErrorType: 500,
AuthenticationErrorType: 401,
AuthorizationErrorType: 403,
NotFoundErrorType: 404,
ConflictErrorType: 409,
ValueInvalidErrorType: 422,
ValueMissingErrorType: 422,
UnimplementedErrorType: 501,
TimeoutErrorType: 504,
}
func (e ErrorType) String() string {
errorTypeStr, found := errorTypeStringMap[e]
if !found {
return errorTypeStringMap[InvalidErrorType]
}
return errorTypeStr
}
func (e ErrorType) HTTPCode() int {
errorTypeCode, found := errorTypeHTTPCodeMap[e]
if !found {
return errorTypeHTTPCodeMap[InvalidErrorType]
}
return errorTypeCode
}