diff --git a/errors/errors.go b/errors/errors.go index 984bc18..a8a6c91 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -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 } diff --git a/middlewares/error_handler.go b/middlewares/error_handler.go index 71b7735..7758174 100644 --- a/middlewares/error_handler.go +++ b/middlewares/error_handler.go @@ -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) @@ -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, ¬ImplementedError) { + 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{