From 7a9e5112eaaea58d55f181d3e5296e4ff839921c Mon Sep 17 00:00:00 2001 From: jloup Date: Wed, 14 Feb 2018 14:19:09 +0100 Subject: initial commit --- api/const.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 api/const.go (limited to 'api/const.go') 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 @@ +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 +} -- cgit v1.2.3