96 lines
2.5 KiB
Go
Raw Normal View History

2023-10-08 15:59:42 +07:00
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"
errUserIsNotFound ErrType = "User is not found"
errInvalidLogin ErrType = "User email or password is invalid"
errUnauthorized ErrType = "Unauthorized"
)
var (
ErrorBadRequest = NewServiceException(errBadRequest)
ErrorInvalidRequest = NewServiceException(errInvalidRequest)
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)
)
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 (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
default:
return http.StatusInternalServerError
}
}
func (s *ServiceException) MapErrorsToCode() Code {
switch s.ErrorType() {
case errUnauthorized:
return Unauthorized
case errConnectTimeOut:
return Timeout
case errBadRequest:
return BadRequest
default:
return ServerError
}
}