init project
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package errors
|
||||
|
||||
import "net/http"
|
||||
|
||||
const (
|
||||
Success Code = "20000"
|
||||
ServerError Code = "50000"
|
||||
BadRequest Code = "40000"
|
||||
InvalidRequest Code = "40001"
|
||||
Unauthorized Code = "40100"
|
||||
Forbidden Code = "40300"
|
||||
Timeout Code = "50400"
|
||||
)
|
||||
|
||||
type Code string
|
||||
|
||||
var (
|
||||
codeMap = map[Code]string{
|
||||
Success: "Success",
|
||||
BadRequest: "Bad or invalid request",
|
||||
Unauthorized: "Unauthorized Token",
|
||||
Timeout: "Gateway Timeout",
|
||||
ServerError: "Internal Server Error",
|
||||
Forbidden: "Forbidden",
|
||||
InvalidRequest: "Invalid Request",
|
||||
}
|
||||
|
||||
codeHTTPMap = map[Code]int{
|
||||
Success: http.StatusOK,
|
||||
BadRequest: http.StatusBadRequest,
|
||||
Unauthorized: http.StatusUnauthorized,
|
||||
Timeout: http.StatusGatewayTimeout,
|
||||
ServerError: http.StatusInternalServerError,
|
||||
Forbidden: http.StatusForbidden,
|
||||
InvalidRequest: http.StatusUnprocessableEntity,
|
||||
}
|
||||
)
|
||||
|
||||
func (c Code) GetMessage() string {
|
||||
return codeMap[c]
|
||||
}
|
||||
|
||||
func (c Code) GetHTTPCode() int {
|
||||
return codeHTTPMap[c]
|
||||
}
|
||||
|
||||
func (c Code) GetCode() string {
|
||||
return string(c)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user