errors.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package errors
  2. import "net/http"
  3. // apiError is an error wrapper that also
  4. // holds information about response status codes.
  5. type apiError struct {
  6. error
  7. statusCode int
  8. }
  9. // HTTPErrorStatusCode returns a status code.
  10. func (e apiError) HTTPErrorStatusCode() int {
  11. return e.statusCode
  12. }
  13. // NewErrorWithStatusCode allows you to associate
  14. // a specific HTTP Status Code to an error.
  15. // The Server will take that code and set
  16. // it as the response status.
  17. func NewErrorWithStatusCode(err error, code int) error {
  18. return apiError{err, code}
  19. }
  20. // NewBadRequestError creates a new API error
  21. // that has the 400 HTTP status code associated to it.
  22. func NewBadRequestError(err error) error {
  23. return NewErrorWithStatusCode(err, http.StatusBadRequest)
  24. }
  25. // NewRequestForbiddenError creates a new API error
  26. // that has the 403 HTTP status code associated to it.
  27. func NewRequestForbiddenError(err error) error {
  28. return NewErrorWithStatusCode(err, http.StatusForbidden)
  29. }
  30. // NewRequestNotFoundError creates a new API error
  31. // that has the 404 HTTP status code associated to it.
  32. func NewRequestNotFoundError(err error) error {
  33. return NewErrorWithStatusCode(err, http.StatusNotFound)
  34. }
  35. // NewRequestConflictError creates a new API error
  36. // that has the 409 HTTP status code associated to it.
  37. func NewRequestConflictError(err error) error {
  38. return NewErrorWithStatusCode(err, http.StatusConflict)
  39. }