diff options
author | jloup <jean-loup.jamet@trainline.com> | 2018-02-14 14:19:09 +0100 |
---|---|---|
committer | jloup <jean-loup.jamet@trainline.com> | 2018-02-14 14:19:09 +0100 |
commit | 7a9e5112eaaea58d55f181d3e5296e4ff839921c (patch) | |
tree | 968ed193f42a1fad759cc89ad2f8ad5b0091291e /api/const.go | |
download | Front-7a9e5112eaaea58d55f181d3e5296e4ff839921c.tar.gz Front-7a9e5112eaaea58d55f181d3e5296e4ff839921c.tar.zst Front-7a9e5112eaaea58d55f181d3e5296e4ff839921c.zip |
initial commit
Diffstat (limited to 'api/const.go')
-rw-r--r-- | api/const.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/api/const.go b/api/const.go new file mode 100644 index 0000000..2edd6f4 --- /dev/null +++ b/api/const.go | |||
@@ -0,0 +1,51 @@ | |||
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 ( | ||
10 | OK Status = iota | ||
11 | NOK | ||
12 | |||
13 | BadRequest ErrorCode = iota + 1 | ||
14 | EmailExists | ||
15 | InternalError | ||
16 | InvalidCredentials | ||
17 | InvalidEmail | ||
18 | InvalidOtp | ||
19 | InvalidPassword | ||
20 | NeedOtpValidation | ||
21 | NotAuthorized | ||
22 | NotFound | ||
23 | OtpAlreadySetup | ||
24 | OtpNotSetup | ||
25 | UserNotConfirmed | ||
26 | ) | ||
27 | |||
28 | func StatusToHttpCode(status Status, code ErrorCode) int { | ||
29 | if status == OK { | ||
30 | return http.StatusOK | ||
31 | } | ||
32 | |||
33 | switch code { | ||
34 | case BadRequest, InvalidPassword, InvalidEmail: | ||
35 | return http.StatusBadRequest | ||
36 | |||
37 | case InvalidCredentials, InvalidOtp: | ||
38 | return http.StatusUnauthorized | ||
39 | |||
40 | case UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation: | ||
41 | return http.StatusForbidden | ||
42 | |||
43 | case EmailExists: | ||
44 | return http.StatusConflict | ||
45 | |||
46 | case NotFound: | ||
47 | return http.StatusNotFound | ||
48 | } | ||
49 | |||
50 | return http.StatusInternalServerError | ||
51 | } | ||