]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - api/const.go
Refactor Portfolio balance.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / const.go
CommitLineData
7a9e5112 1package api
2
3import "net/http"
4
5//go:generate stringer -type=Status,ErrorCode -output const_string.go
6type Status uint32
7type ErrorCode uint32
8
24e47979 9const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20
2f91f20a 10
7a9e5112 11const (
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
908ee2dd 22 IPRestrictedApiKey
7a9e5112 23 InvalidOtp
24 InvalidPassword
25 NeedOtpValidation
26 NotAuthorized
27 NotFound
28 OtpAlreadySetup
29 OtpNotSetup
30 UserNotConfirmed
31)
32
33func StatusToHttpCode(status Status, code ErrorCode) int {
34 if status == OK {
35 return http.StatusOK
36 }
37
38 switch code {
908ee2dd 39 case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey:
7a9e5112 40 return http.StatusBadRequest
41
42 case InvalidCredentials, InvalidOtp:
43 return http.StatusUnauthorized
44
45 case UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation:
46 return http.StatusForbidden
47
48 case EmailExists:
49 return http.StatusConflict
50
51 case NotFound:
52 return http.StatusNotFound
2f91f20a 53
54 case ExternalServiceTimeout:
55 return http.StatusGatewayTimeout
7a9e5112 56 }
57
58 return http.StatusInternalServerError
59}