Skip to content

fix: added http error codes#146

Open
NeelMalwatkar wants to merge 2 commits intotink3rlabs:mainfrom
NeelMalwatkar:main
Open

fix: added http error codes#146
NeelMalwatkar wants to merge 2 commits intotink3rlabs:mainfrom
NeelMalwatkar:main

Conversation

@NeelMalwatkar
Copy link

@NeelMalwatkar NeelMalwatkar commented Feb 19, 2026

Issue : #144

  • Added MethodNotAllowed (405)
  • Added Conflict (409)
  • Added Gone (410)
  • Added UnsupportedMediaType (415)
  • Added UnprocessableEntity (422)
  • Added TooManyRequests (429)
  • Added InternalServerError (500)
  • Added NotImplemented (501)
  • Added BadGateway (502)
  • Added GatewayTimeout (504)
  • Added RequestTimeout (408)

errors/errors.go Outdated
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.

@ayashjorden
Copy link
Contributor

While I don't want to scope-creep this PR, error_handler.go may benefit from some generalization or refactoring to make it DRY.
If this becomes tangled, I'm ok with improving in a separate PR

@NeelMalwatkar
Copy link
Author

While I don't want to scope-creep this PR, error_handler.go may benefit from some generalization or refactoring to make it DRY. If this becomes tangled, I'm ok with improving in a separate PR

I was thinking of doing it this way:

  1. errors/errors.go- Add a StatusCoder interface and a StatusCode() method to each error type:
type StatusCoder interface {
    StatusCode() int
}

// Each type gets its own status code
func (e *BadRequest) StatusCode() int    { return 400 }
func (e *NotFound) StatusCode() int      { return 404 }
func (e *Forbidden) StatusCode() int     { return 403 }
// ... same for all 16 types
  1. In error_handler.go - Replace all if blocks with a single interface check:
  var sc Errors.StatusCoder
  if errors.As(err, &sc) {
      renderError(w, r, sc.StatusCode(), err.Error())
      return
  }

I can make a separate PR for this if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants