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
88 changes: 88 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,91 @@ type Unauthorized struct {
func (e *Unauthorized) Error() string {
return e.Message
}

type MethodNotAllowed struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @NeelMalwatkar ,
This is a prime candidate for Composition (part of how the Go programing language is designed)

// 1. Define the base structure
type BaseError struct {
    Message string
}

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

// 2. Compose specific errors using the base
type MethodNotAllowed struct{ BaseError }
type Conflict struct{ BaseError }
type Gone struct{ BaseError }
type UnsupportedMediaType struct{ BaseError }
// ... and so on

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, i will re-write all the errors using this technique.

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
}
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