aboutsummaryrefslogtreecommitdiff
path: root/api/const.go
blob: 2edd6f4b941f2defe30b4b6a7f87efa1b69aff7f (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
package api

import "net/http"

//go:generate stringer -type=Status,ErrorCode -output const_string.go
type Status uint32
type ErrorCode uint32

const (
	OK Status = iota
	NOK

	BadRequest ErrorCode = iota + 1
	EmailExists
	InternalError
	InvalidCredentials
	InvalidEmail
	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:
		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
	}

	return http.StatusInternalServerError
}