Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 18 additions & 33 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
package errors

type BadRequest struct {
type BaseError struct {
Message string
}

func (e *BadRequest) Error() string {
func (e *BaseError) Error() string {
return e.Message
}

type NotFound struct {
Message string
}

func (e *NotFound) Error() string {
return e.Message
}

type ServiceUnavailable struct {
Message string
}

func (e *ServiceUnavailable) Error() string {
return e.Message
}

type Forbidden struct {
Message string
}

func (e *Forbidden) Error() string {
return e.Message
}

type Unauthorized struct {
Message string
}

func (e *Unauthorized) Error() string {
return e.Message
}
type BadRequest struct{ BaseError }
type NotFound struct{ BaseError }
type ServiceUnavailable struct{ BaseError }
type Forbidden struct{ BaseError }
type Unauthorized struct{ BaseError }
type MethodNotAllowed struct{ BaseError }
type Conflict struct{ BaseError }
type Gone struct{ BaseError }
type UnsupportedMediaType struct{ BaseError }
type UnprocessableEntity struct{ BaseError }
type TooManyRequests struct{ BaseError }
type InternalServerError struct{ BaseError }
type BadGateway struct{ BaseError }
type GatewayTimeout struct{ BaseError }
type RequestTimeout struct{ BaseError }
type NotImplemented struct{ BaseError }
121 changes: 121 additions & 0 deletions middlewares/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ func (e *ErrorHandler) Wrap(handler func(w http.ResponseWriter, r *http.Request)
var serviceUnavailable *serviceErrors.ServiceUnavailable
var forbiddenError *serviceErrors.Forbidden
var unauthorizedError *serviceErrors.Unauthorized
var methodNotAllowedError *serviceErrors.MethodNotAllowed
var conflictError *serviceErrors.Conflict
var goneError *serviceErrors.Gone
var unsupportedMediaTypeError *serviceErrors.UnsupportedMediaType
var unprocessableEntityError *serviceErrors.UnprocessableEntity
var tooManyRequestsError *serviceErrors.TooManyRequests
var internalServerError *serviceErrors.InternalServerError
var badGatewayError *serviceErrors.BadGateway
var gatewayTimeoutError *serviceErrors.GatewayTimeout
var requestTimeoutError *serviceErrors.RequestTimeout
var notImplementedError *serviceErrors.NotImplemented

err := handler(w, r)

Expand Down Expand Up @@ -73,6 +84,116 @@ func (e *ErrorHandler) Wrap(handler func(w http.ResponseWriter, r *http.Request)
return
}

if errors.As(err, &methodNotAllowedError) {
render.Status(r, http.StatusMethodNotAllowed)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusMethodNotAllowed),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &conflictError) {
render.Status(r, http.StatusConflict)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusConflict),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &goneError) {
render.Status(r, http.StatusGone)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusGone),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &unsupportedMediaTypeError) {
render.Status(r, http.StatusUnsupportedMediaType)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusUnsupportedMediaType),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &unprocessableEntityError) {
render.Status(r, http.StatusUnprocessableEntity)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusUnprocessableEntity),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &tooManyRequestsError) {
render.Status(r, http.StatusTooManyRequests)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusTooManyRequests),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &internalServerError) {
render.Status(r, http.StatusInternalServerError)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusInternalServerError),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &badGatewayError) {
render.Status(r, http.StatusBadGateway)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusBadGateway),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &gatewayTimeoutError) {
render.Status(r, http.StatusGatewayTimeout)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusGatewayTimeout),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &requestTimeoutError) {
render.Status(r, http.StatusRequestTimeout)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusRequestTimeout),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if errors.As(err, &notImplementedError) {
render.Status(r, http.StatusNotImplemented)
response := types.ErrorResponse{
Status: http.StatusText(http.StatusNotImplemented),
Error: err.Error(),
}
render.JSON(w, r, response)
return
}

if err != nil {
render.Status(r, http.StatusInternalServerError)
response := types.ErrorResponse{
Expand Down