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, } )
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 code VALIDATION_ERROR for dynamic validation messages.