]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - api/const.go
Refactor Portfolio balance.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / const.go
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 EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20
10
11 const (
12 OK Status = iota
13 NOK
14
15 BadRequest ErrorCode = iota + 1
16 EmailExists
17 ExternalServiceTimeout
18 InternalError
19 InvalidCredentials
20 InvalidEmail
21 InvalidMarketCredentials
22 IPRestrictedApiKey
23 InvalidOtp
24 InvalidPassword
25 NeedOtpValidation
26 NotAuthorized
27 NotFound
28 OtpAlreadySetup
29 OtpNotSetup
30 UserNotConfirmed
31 )
32
33 func StatusToHttpCode(status Status, code ErrorCode) int {
34 if status == OK {
35 return http.StatusOK
36 }
37
38 switch code {
39 case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey:
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
53
54 case ExternalServiceTimeout:
55 return http.StatusGatewayTimeout
56 }
57
58 return http.StatusInternalServerError
59 }