Documentation
¶
Overview ¶
Package httperr provides HTTP-aware error types for use with JSON APIs.
HTTPError carries status code and application error code (e.g. BAD_REQUEST, NOT_FOUND). CodeFromStatus maps HTTP status codes to default application codes. Use New for custom errors and NewValidationErrorf for validation failures. Sentinel errors (ErrInvalidID, ErrNotAuthenticated) are provided for common cases.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidID is a 400 error with code INVALID_ID (e.g. invalid UUID in path). ErrInvalidID = &HTTPError{ Err: errors.New("invalid ID"), StatusCode: http.StatusBadRequest, Code: "INVALID_ID", IsExpected: true, } // ErrNotAuthenticated is a 401 error with code NOT_AUTHENTICATED. ErrNotAuthenticated = &HTTPError{ Err: errors.New("not authenticated"), StatusCode: http.StatusUnauthorized, Code: "NOT_AUTHENTICATED", IsExpected: true, } // ErrForbidden is a 403 error with code FORBIDDEN. ErrForbidden = &HTTPError{ Err: errors.New("forbidden"), StatusCode: http.StatusForbidden, Code: "FORBIDDEN", IsExpected: true, } // ErrNotFound is a 404 error with code NOT_FOUND. ErrNotFound = &HTTPError{ Err: errors.New("not found"), StatusCode: http.StatusNotFound, Code: "NOT_FOUND", IsExpected: true, } // ErrConflict is a 409 error with code CONFLICT. ErrConflict = &HTTPError{ Err: errors.New("conflict"), StatusCode: http.StatusConflict, Code: "CONFLICT", IsExpected: true, } // ErrGone is a 410 error with code GONE. ErrGone = &HTTPError{ Err: errors.New("gone"), StatusCode: http.StatusGone, Code: "GONE", IsExpected: true, } // ErrUnprocessableEntity is a 422 error with code VALIDATION_ERROR. ErrUnprocessableEntity = &HTTPError{ Err: errors.New("unprocessable entity"), StatusCode: http.StatusUnprocessableEntity, Code: "VALIDATION_ERROR", IsExpected: true, } // ErrTooManyRequests is a 429 error with code RATE_LIMIT_EXCEEDED. ErrTooManyRequests = &HTTPError{ Err: errors.New("too many requests"), StatusCode: http.StatusTooManyRequests, Code: "RATE_LIMIT_EXCEEDED", IsExpected: true, } ErrServiceUnavailable = &HTTPError{ Err: errors.New("service unavailable"), StatusCode: http.StatusServiceUnavailable, Code: "SERVICE_UNAVAILABLE", IsExpected: false, } )
Functions ¶
func CodeFromStatus ¶ added in v0.1.1
CodeFromStatus returns the application error code for a given HTTP status.
func IsExpectedClientError ¶ added in v0.1.2
Types ¶
type HTTPError ¶
type HTTPError struct {
Err error
StatusCode int
Code string
// IsExpected is true for client errors (4xx); callers may use it to avoid logging as server errors.
IsExpected bool
}
HTTPError represents an error with HTTP status and application code.
func New ¶
New returns an HTTPError with the given error, status code, and code. IsExpected is true for 4xx.
func NewValidationErrorf ¶
NewValidationErrorf creates an HTTPError with status 400 and code VALIDATION_ERROR for dynamic validation messages. For semantic "request body valid JSON but business validation failed" use ErrUnprocessableEntity (422) from sentinels.