]>
Commit | Line | Data |
---|---|---|
7a9e5112 | 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 | ||
2f91f20a | 9 | const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 10 |
10 | ||
7a9e5112 | 11 | const ( |
12 | OK Status = iota | |
13 | NOK | |
14 | ||
15 | BadRequest ErrorCode = iota + 1 | |
16 | EmailExists | |
2f91f20a | 17 | ExternalServiceTimeout |
7a9e5112 | 18 | InternalError |
19 | InvalidCredentials | |
20 | InvalidEmail | |
2f91f20a | 21 | InvalidMarketCredentials |
7a9e5112 | 22 | InvalidOtp |
23 | InvalidPassword | |
24 | NeedOtpValidation | |
25 | NotAuthorized | |
26 | NotFound | |
27 | OtpAlreadySetup | |
28 | OtpNotSetup | |
29 | UserNotConfirmed | |
30 | ) | |
31 | ||
32 | func StatusToHttpCode(status Status, code ErrorCode) int { | |
33 | if status == OK { | |
34 | return http.StatusOK | |
35 | } | |
36 | ||
37 | switch code { | |
2f91f20a | 38 | case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials: |
7a9e5112 | 39 | return http.StatusBadRequest |
40 | ||
41 | case InvalidCredentials, InvalidOtp: | |
42 | return http.StatusUnauthorized | |
43 | ||
44 | case UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation: | |
45 | return http.StatusForbidden | |
46 | ||
47 | case EmailExists: | |
48 | return http.StatusConflict | |
49 | ||
50 | case NotFound: | |
51 | return http.StatusNotFound | |
2f91f20a | 52 | |
53 | case ExternalServiceTimeout: | |
54 | return http.StatusGatewayTimeout | |
7a9e5112 | 55 | } |
56 | ||
57 | return http.StatusInternalServerError | |
58 | } |