blob: 2ad4d6135af2920bf9b899970910e38fe17e6f33 (
plain) (
blame)
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
|
package api
import "net/http"
//go:generate stringer -type=Status,ErrorCode -output const_string.go
type Status uint32
type ErrorCode uint32
const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20
const (
OK Status = iota
NOK
BadRequest ErrorCode = iota + 1
EmailExists
ExternalServiceTimeout
InternalError
InvalidCredentials
InvalidEmail
InvalidMarketCredentials
MarketCredentialsNotConfigured
IPRestrictedApiKey
InvalidOtp
InvalidPassword
NeedOtpValidation
NotAuthorized
NotFound
OtpAlreadySetup
OtpNotSetup
UserNotConfirmed
)
func StatusToHttpCode(status Status, code ErrorCode) int {
if status == OK {
return http.StatusOK
}
switch code {
case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured:
return http.StatusBadRequest
case InvalidCredentials, InvalidOtp:
return http.StatusUnauthorized
case UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation:
return http.StatusForbidden
case EmailExists:
return http.StatusConflict
case NotFound:
return http.StatusNotFound
case ExternalServiceTimeout:
return http.StatusGatewayTimeout
}
return http.StatusInternalServerError
}
|