-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
58 lines (55 loc) · 1.37 KB
/
http.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
package ez
import (
"net/http"
)
// HTTPStatusToError converts a HTTP error code to a standar application error code
func HTTPStatusToError(status int) string {
switch status {
case http.StatusConflict:
return ECONFLICT
case http.StatusInternalServerError:
return EINTERNAL
case http.StatusBadRequest:
return EINVALID
case http.StatusNotFound:
return ENOTFOUND
case http.StatusForbidden:
return ENOTAUTHORIZED
case http.StatusUnauthorized:
return ENOTAUTHENTICATED
case http.StatusTooManyRequests:
return ERESOURCEEXHAUSTED
case http.StatusNotImplemented:
return ENOTIMPLEMENTED
case http.StatusServiceUnavailable:
return EUNAVAILABLE
default:
return EINTERNAL
}
}
// ErrorToHTTPStatus converts an standar application error code to a HTTP status
func ErrorToHTTPStatus(err error) int {
code := ErrorCode(err)
switch code {
case ECONFLICT:
return http.StatusConflict
case EINTERNAL:
return http.StatusInternalServerError
case EINVALID:
return http.StatusBadRequest
case ENOTFOUND:
return http.StatusNotFound
case ENOTAUTHORIZED:
return http.StatusForbidden
case ENOTAUTHENTICATED:
return http.StatusUnauthorized
case ERESOURCEEXHAUSTED:
return http.StatusTooManyRequests
case ENOTIMPLEMENTED:
return http.StatusNotImplemented
case EUNAVAILABLE:
return http.StatusServiceUnavailable
default:
return http.StatusInternalServerError
}
}