]>
Commit | Line | Data |
---|---|---|
1 | package api | |
2 | ||
3 | import "net/http" | |
4 | ||
5 | //go:generate stringer -type=Status,ErrorCode -output const_string.go | |
6 | type Status uint32 | |
7 | type ErrorCode uint32 | |
8 | ||
9 | const ( | |
10 | OK Status = iota | |
11 | NOK | |
12 | ||
13 | BadRequest ErrorCode = iota + 1 | |
14 | EmailExists | |
15 | InternalError | |
16 | InvalidCredentials | |
17 | InvalidEmail | |
18 | InvalidOtp | |
19 | InvalidPassword | |
20 | NeedOtpValidation | |
21 | NotAuthorized | |
22 | NotFound | |
23 | OtpAlreadySetup | |
24 | OtpNotSetup | |
25 | UserNotConfirmed | |
26 | ) | |
27 | ||
28 | func StatusToHttpCode(status Status, code ErrorCode) int { | |
29 | if status == OK { | |
30 | return http.StatusOK | |
31 | } | |
32 | ||
33 | switch code { | |
34 | case BadRequest, InvalidPassword, InvalidEmail: | |
35 | return http.StatusBadRequest | |
36 | ||
37 | case InvalidCredentials, InvalidOtp: | |
38 | return http.StatusUnauthorized | |
39 | ||
40 | case UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation: | |
41 | return http.StatusForbidden | |
42 | ||
43 | case EmailExists: | |
44 | return http.StatusConflict | |
45 | ||
46 | case NotFound: | |
47 | return http.StatusNotFound | |
48 | } | |
49 | ||
50 | return http.StatusInternalServerError | |
51 | } |