143 lines
4.5 KiB
Go
143 lines
4.5 KiB
Go
package errors
|
|
|
|
import "net/http"
|
|
|
|
type ErrType string
|
|
|
|
const (
|
|
errRequestTimeOut ErrType = "Request Timeout to 3rd Party"
|
|
errConnectTimeOut ErrType = "Connect Timeout to 3rd Party"
|
|
errFailedExternalCall ErrType = "Failed response from 3rd Party call"
|
|
errExternalCall ErrType = "error on 3rd Party call"
|
|
errInvalidRequest ErrType = "Invalid Request"
|
|
errBadRequest ErrType = "Bad Request"
|
|
errOrderNotFound ErrType = "Astria order is not found"
|
|
errCheckoutIDNotDefined ErrType = "Checkout client id not found"
|
|
errInternalServer ErrType = "Internal Server error"
|
|
errExternalServer ErrType = "External Server error"
|
|
errUserIsNotFound ErrType = "User is not found"
|
|
errInvalidLogin ErrType = "User email or password is invalid"
|
|
errUnauthorized ErrType = "Unauthorized"
|
|
errInsufficientBalance ErrType = "Insufficient Balance"
|
|
errInactivePartner ErrType = "Partner's license is invalid or has expired. Please contact Admin Support."
|
|
errTicketAlreadyUsed ErrType = "Ticket Already Used."
|
|
errProductIsRequired ErrType = "Product"
|
|
errEmailAndPhoneNumberRequired ErrType = "Email or Phone is required"
|
|
errEmailAlreadyRegistered ErrType = "Email is already registered"
|
|
errPhoneNumberAlreadyRegistered ErrType = "Phone is already registered"
|
|
)
|
|
|
|
var (
|
|
ErrorBadRequest = NewServiceException(errBadRequest)
|
|
ErrorInvalidRequest = NewServiceException(errInvalidRequest)
|
|
ErrorExternalRequest = NewServiceException(errExternalServer)
|
|
ErrorUnauthorized = NewServiceException(errUnauthorized)
|
|
ErrorOrderNotFound = NewServiceException(errOrderNotFound)
|
|
ErrorClientIDNotDefined = NewServiceException(errCheckoutIDNotDefined)
|
|
ErrorRequestTimeout = NewServiceException(errRequestTimeOut)
|
|
ErrorExternalCall = NewServiceException(errExternalCall)
|
|
ErrorFailedExternalCall = NewServiceException(errFailedExternalCall)
|
|
ErrorConnectionTimeOut = NewServiceException(errConnectTimeOut)
|
|
ErrorInternalServer = NewServiceException(errInternalServer)
|
|
ErrorUserIsNotFound = NewServiceException(errUserIsNotFound)
|
|
ErrorUserInvalidLogin = NewServiceException(errInvalidLogin)
|
|
ErrorInsufficientBalance = NewServiceException(errInsufficientBalance)
|
|
ErrorInvalidLicense = NewServiceException(errInactivePartner)
|
|
ErrorTicketInvalidOrAlreadyUsed = NewServiceException(errTicketAlreadyUsed)
|
|
ErrorPhoneNumberEmailIsRequired = NewServiceException(errEmailAndPhoneNumberRequired)
|
|
ErrorPhoneNumberIsAlreadyRegistered = NewServiceException(errPhoneNumberAlreadyRegistered)
|
|
ErrorEmailIsAlreadyRegistered = NewServiceException(errEmailAlreadyRegistered)
|
|
)
|
|
|
|
type Error interface {
|
|
ErrorType() ErrType
|
|
MapErrorsToHTTPCode() int
|
|
MapErrorsToCode() Code
|
|
error
|
|
}
|
|
|
|
type ServiceException struct {
|
|
errorType ErrType
|
|
message string
|
|
}
|
|
|
|
func NewServiceException(errType ErrType) *ServiceException {
|
|
return &ServiceException{
|
|
errorType: errType,
|
|
message: string(errType),
|
|
}
|
|
}
|
|
|
|
func NewError(errType ErrType, message string) *ServiceException {
|
|
return &ServiceException{
|
|
errorType: errType,
|
|
message: message,
|
|
}
|
|
}
|
|
|
|
func NewErrorMessage(errType *ServiceException, message string) *ServiceException {
|
|
return &ServiceException{
|
|
errorType: errType.ErrorType(),
|
|
message: message,
|
|
}
|
|
}
|
|
|
|
func (s *ServiceException) ErrorType() ErrType {
|
|
return s.errorType
|
|
}
|
|
|
|
func (s *ServiceException) Error() string {
|
|
return s.message
|
|
}
|
|
|
|
func (s *ServiceException) MapErrorsToHTTPCode() int {
|
|
switch s.ErrorType() {
|
|
case errBadRequest:
|
|
return http.StatusBadRequest
|
|
|
|
case errInvalidRequest:
|
|
return http.StatusBadRequest
|
|
|
|
case errInvalidLogin:
|
|
return http.StatusBadRequest
|
|
|
|
case errUserIsNotFound:
|
|
return http.StatusBadRequest
|
|
|
|
default:
|
|
return http.StatusInternalServerError
|
|
}
|
|
}
|
|
|
|
func (s *ServiceException) MapErrorsToCode() Code {
|
|
switch s.ErrorType() {
|
|
|
|
case errUnauthorized:
|
|
return Unauthorized
|
|
|
|
case errConnectTimeOut:
|
|
return Timeout
|
|
|
|
case errBadRequest:
|
|
return BadRequest
|
|
|
|
case errUserIsNotFound:
|
|
return BadRequest
|
|
|
|
case errInvalidLogin:
|
|
return BadRequest
|
|
|
|
case errInsufficientBalance:
|
|
return BadRequest
|
|
|
|
case errInactivePartner:
|
|
return BadRequest
|
|
|
|
case errTicketAlreadyUsed:
|
|
return CheckinInvalid
|
|
|
|
default:
|
|
return BadRequest
|
|
}
|
|
}
|