diff options
Diffstat (limited to 'api/const.go')
-rw-r--r-- | api/const.go | 74 |
1 files changed, 41 insertions, 33 deletions
diff --git a/api/const.go b/api/const.go index 2ad4d61..4f608d5 100644 --- a/api/const.go +++ b/api/const.go | |||
@@ -1,58 +1,66 @@ | |||
1 | package api | 1 | package api |
2 | 2 | ||
3 | import "net/http" | 3 | import ( |
4 | "net/http" | ||
4 | 5 | ||
5 | //go:generate stringer -type=Status,ErrorCode -output const_string.go | 6 | "github.com/jloup/utils" |
7 | ) | ||
8 | |||
9 | //go:generate stringer -type=Status -output const_string.go | ||
6 | type Status uint32 | 10 | type Status uint32 |
7 | type ErrorCode uint32 | ||
8 | 11 | ||
9 | const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20 | 12 | const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20 |
10 | 13 | ||
11 | const ( | 14 | const ( |
12 | OK Status = iota | 15 | OK Status = iota |
13 | NOK | 16 | NOK |
17 | ) | ||
14 | 18 | ||
15 | BadRequest ErrorCode = iota + 1 | 19 | var ( |
16 | EmailExists | 20 | errorFlagCounter utils.Counter = 0 |
17 | ExternalServiceTimeout | 21 | BadRequest utils.Flag = utils.InitFlag(&errorFlagCounter, "BadRequest") |
18 | InternalError | 22 | EmailExists utils.Flag = utils.InitFlag(&errorFlagCounter, "EmailExists") |
19 | InvalidCredentials | 23 | ExternalServiceTimeout utils.Flag = utils.InitFlag(&errorFlagCounter, "ExternalServiceTimeout") |
20 | InvalidEmail | 24 | InternalError utils.Flag = utils.InitFlag(&errorFlagCounter, "InternalError") |
21 | InvalidMarketCredentials | 25 | InvalidCredentials utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidCredentials") |
22 | MarketCredentialsNotConfigured | 26 | InvalidEmail utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidEmail") |
23 | IPRestrictedApiKey | 27 | InvalidMarketCredentials utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidMarketCredentials") |
24 | InvalidOtp | 28 | MarketCredentialsNotConfigured utils.Flag = utils.InitFlag(&errorFlagCounter, "MarketCredentialsNotConfigured") |
25 | InvalidPassword | 29 | IPRestrictedApiKey utils.Flag = utils.InitFlag(&errorFlagCounter, "IPRestrictedApiKey") |
26 | NeedOtpValidation | 30 | InvalidOtp utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidOtp") |
27 | NotAuthorized | 31 | InvalidPassword utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidPassword") |
28 | NotFound | 32 | NeedOtpValidation utils.Flag = utils.InitFlag(&errorFlagCounter, "NeedOtpValidation") |
29 | OtpAlreadySetup | 33 | NotAuthorized utils.Flag = utils.InitFlag(&errorFlagCounter, "NotAuthorized") |
30 | OtpNotSetup | 34 | NotFound utils.Flag = utils.InitFlag(&errorFlagCounter, "NotFound") |
31 | UserNotConfirmed | 35 | OtpAlreadySetup utils.Flag = utils.InitFlag(&errorFlagCounter, "OtpAlreadySetup") |
36 | OtpNotSetup utils.Flag = utils.InitFlag(&errorFlagCounter, "OtpNotSetup") | ||
37 | UserNotConfirmed utils.Flag = utils.InitFlag(&errorFlagCounter, "UserNotConfirmed") | ||
38 | |||
39 | StatusBadRequest utils.Flag = utils.Join("BadRequest", BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured) | ||
40 | StatusUnauthorized utils.Flag = utils.Join("Unauthorized", InvalidCredentials, InvalidOtp) | ||
41 | StatusForbidden utils.Flag = utils.Join("Forbidden", UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation) | ||
42 | StatusConflicts utils.Flag = utils.Join("Conflicts", EmailExists) | ||
43 | StatusNotFound utils.Flag = utils.Join("NotFound", NotFound) | ||
44 | StatusGatewayTimeout utils.Flag = utils.Join("GatewayTimeout", ExternalServiceTimeout) | ||
32 | ) | 45 | ) |
33 | 46 | ||
34 | func StatusToHttpCode(status Status, code ErrorCode) int { | 47 | func StatusToHttpCode(status Status, code utils.Flag) int { |
35 | if status == OK { | 48 | if status == OK { |
36 | return http.StatusOK | 49 | return http.StatusOK |
37 | } | 50 | } |
38 | 51 | ||
39 | switch code { | 52 | switch { |
40 | case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured: | 53 | case utils.Intersect(code, StatusBadRequest): |
41 | return http.StatusBadRequest | 54 | return http.StatusBadRequest |
42 | 55 | case utils.Intersect(code, StatusUnauthorized): | |
43 | case InvalidCredentials, InvalidOtp: | ||
44 | return http.StatusUnauthorized | 56 | return http.StatusUnauthorized |
45 | 57 | case utils.Intersect(code, StatusForbidden): | |
46 | case UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation: | ||
47 | return http.StatusForbidden | 58 | return http.StatusForbidden |
48 | 59 | case utils.Intersect(code, StatusConflicts): | |
49 | case EmailExists: | ||
50 | return http.StatusConflict | 60 | return http.StatusConflict |
51 | 61 | case utils.Intersect(code, StatusNotFound): | |
52 | case NotFound: | ||
53 | return http.StatusNotFound | 62 | return http.StatusNotFound |
54 | 63 | case utils.Intersect(code, StatusGatewayTimeout): | |
55 | case ExternalServiceTimeout: | ||
56 | return http.StatusGatewayTimeout | 64 | return http.StatusGatewayTimeout |
57 | } | 65 | } |
58 | 66 | ||