-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
76 lines (71 loc) · 2.08 KB
/
status.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
package httperror
import (
"artk.dev/apperror"
"net/http"
)
// EncodeKind maps an apperror.Kind to an HTTP status code.
func EncodeKind(kind apperror.Kind) int {
switch kind {
case apperror.OK:
return http.StatusOK
case apperror.ValidationError:
return http.StatusBadRequest
case apperror.UnauthorizedError:
return http.StatusUnauthorized
case apperror.ForbiddenError:
return http.StatusForbidden
case apperror.NotFoundError:
return http.StatusNotFound
case apperror.ConflictError:
return http.StatusConflict
case apperror.PreconditionFailedError:
return http.StatusPreconditionFailed
case apperror.TooManyRequestsError:
return http.StatusTooManyRequests
case apperror.TimeoutError:
return http.StatusServiceUnavailable
default:
return http.StatusInternalServerError
}
}
// DecodeKind maps an HTTP status code to an apperror.Kind.
//
//gocyclo:ignore
func DecodeKind(status int) apperror.Kind {
// While it might be advisable to handle success status codes before
// calling this function, we provide a reasonably safe default.
if status < http.StatusBadRequest {
return apperror.OK
}
switch status {
case http.StatusBadRequest:
return apperror.ValidationError
case http.StatusUnauthorized:
return apperror.UnauthorizedError
case http.StatusForbidden:
return apperror.ForbiddenError
case http.StatusNotFound:
return apperror.NotFoundError
case http.StatusConflict:
return apperror.ConflictError
case http.StatusPreconditionFailed:
return apperror.PreconditionFailedError
case http.StatusTooManyRequests:
return apperror.TooManyRequestsError
case http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
// Detect infrastructure (e.g., load balancer) errors,
// in addition to application errors.
return apperror.TimeoutError
default:
// Errors in the 400 range are client errors.
// Validation is the closest kind.
if status < http.StatusInternalServerError {
return apperror.ValidationError
}
// Errors in the 500 range are server errors.
// Unknown is the closest kind.
return apperror.UnknownError
}
}