From 880c9245ac1df17f8e6aed2b67919b85c92424ee Mon Sep 17 00:00:00 2001 From: NeelMalwatkar Date: Thu, 19 Feb 2026 00:37:39 -0500 Subject: [PATCH] fix: added http error codes --- errors/errors.go | 88 +++++++++++++++++++++++++ middlewares/error_handler.go | 121 +++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) diff --git a/errors/errors.go b/errors/errors.go index 984bc18..92b51dc 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -39,3 +39,91 @@ type Unauthorized struct { func (e *Unauthorized) Error() string { return e.Message } + +type MethodNotAllowed struct { + Message string +} + +func (e *MethodNotAllowed) Error() string { + return e.Message +} + +type Conflict struct { + Message string +} + +func (e *Conflict) Error() string { + return e.Message +} + +type Gone struct { + Message string +} + +func (e *Gone) Error() string { + return e.Message +} + +type UnsupportedMediaType struct { + Message string +} + +func (e *UnsupportedMediaType) Error() string { + return e.Message +} + +type UnprocessableEntity struct { + Message string +} + +func (e *UnprocessableEntity) Error() string { + return e.Message +} + +type TooManyRequests struct { + Message string +} + +func (e *TooManyRequests) Error() string { + return e.Message +} + +type InternalServerError struct { + Message string +} + +func (e *InternalServerError) Error() string { + return e.Message +} + +type BadGateway struct { + Message string +} + +func (e *BadGateway) Error() string { + return e.Message +} + +type GatewayTimeout struct { + Message string +} + +func (e *GatewayTimeout) Error() string { + return e.Message +} + +type RequestTimeout struct { + Message string +} + +func (e *RequestTimeout) Error() string { + return e.Message +} + +type NotImplemented struct { + Message string +} + +func (e *NotImplemented) Error() string { + return e.Message +} 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{