aboutsummaryrefslogblamecommitdiff
path: root/api/const.go
blob: 2ad4d6135af2920bf9b899970910e38fe17e6f33 (plain) (tree)
1
2
3
4
5
6
7
8
9







                                                                     
                                           
 





                                       
                              


                          
                                
                                      
                          















                                                          
                                                                                                                                     












                                                                                              


                                                



                                             
package api

import "net/http"

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

const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20

const (
	OK Status = iota
	NOK

	BadRequest ErrorCode = iota + 1
	EmailExists
	ExternalServiceTimeout
	InternalError
	InvalidCredentials
	InvalidEmail
	InvalidMarketCredentials
	MarketCredentialsNotConfigured
	IPRestrictedApiKey
	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, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured:
		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

	case ExternalServiceTimeout:
		return http.StatusGatewayTimeout
	}

	return http.StatusInternalServerError
}