go.bigb.es/auxilia
Documentation
Index
- func BadRequest(detail string) error
- func Conflict(detail string) error
- func Forbidden(detail string) error
- func Internal(detail string) error
- func NotFound(resource string) error
- func TooManyRequests(detail string) error
- func Unauthorized(detail string) error
- func Unprocessable(detail string) error
- type BadRequestError
- type ConflictError
- type ForbiddenError
- type InternalError
- type NotFoundError
- type Problem
- type TooManyRequestsError
- type UnauthorizedError
- type UnprocessableError
Constants
const ContentType = "application/problem+json"
ContentType is the media type for RFC 7807 problem documents.
Functions
func BadRequest(detail string) error
BadRequest returns a BadRequestError with the given detail message.
func Conflict(detail string) error
Conflict returns a ConflictError with the given detail message.
func Forbidden(detail string) error
Forbidden returns a ForbiddenError with the given detail message.
func Internal(detail string) error
Internal returns an InternalError with the given detail message. The detail is for logs only; it does not appear in the HTTP response body.
func NotFound(resource string) error
NotFound returns a NotFoundError for the given resource name.
func TooManyRequests(detail string) error
TooManyRequests returns a TooManyRequestsError with the given detail message.
func Unprocessable(detail string) error
Unprocessable returns an UnprocessableError with the given detail message.
Types
type BadRequestError struct{ Detail string }
BadRequestError indicates malformed or invalid input from the caller.
type ConflictError struct{ Detail string }
ConflictError indicates the request conflicts with current resource state.
type ForbiddenError struct{ Detail string }
ForbiddenError indicates the request is authenticated but not allowed.
type InternalError struct{ Detail string }
InternalError indicates an unrecoverable server-side failure. The Detail field is for internal logging only — it never appears in the HTTP response body unless an explicit culpa.PublicDetail is attached.
type NotFoundError struct{ Resource string }
NotFoundError indicates the requested resource does not exist.
type Problem struct { // Type is a URI identifying the problem. Defaults to "about:blank". Type string `json:"type"` // Title is a short human-readable summary. Title string `json:"title"` // Status is the HTTP status code. Status int `json:"status"` // Detail is a user-safe explanation. Empty for opaque internal errors. Detail string `json:"detail,omitempty"` // Code is a machine-readable slug. Code string `json:"code,omitempty"` }
Problem is RFC 7807 Problem Details for HTTP APIs.
A Problem is itself an error so that handlers can return it through the usual error chain and rely on FromError to pass it through unchanged.
FromError maps any error onto a Problem.
Resolution order:
- If the error chain contains a Problem, that value is used as the base (so callers can return one directly even when wrapped).
- If the chain contains one of the typed sentinel errors (NotFoundError, BadRequestError, ...), the matching status and code are used.
- Anything else collapses to a Status 500 problem with Title "Internal Server Error" and empty Detail/Code so internal error messages do not leak into responses.
After picking a base, FromError applies culpa overrides discovered in the chain:
- culpa.CodeDetail (when Code is a non-empty string) overrides Code.
- culpa.PublicDetail (when Message is non-empty) overrides Detail.
The override pass also runs for the default 500 case but only sets fields that are still zero, so a culpa.PublicDetail message can opt in to surfacing a user-safe string while the inner error text still does not leak.
FromError(nil) returns the zero Problem (Status 0). Callers should not invoke it on a nil error.
func (p Problem) Write(w http.ResponseWriter)
Write serializes the Problem as JSON, sets the Content-Type to application/problem+json, and writes the status code.
If Type is empty, it is replaced with "about:blank" in the response body without mutating the receiver.
Encoding errors are silently dropped: by the time encoding fails the response has already been partially written and there is nothing useful to do.
type TooManyRequestsError struct{ Detail string }
TooManyRequestsError indicates the caller has hit a rate limit.
func (e TooManyRequestsError) Error() string
Error implements error.
type UnprocessableError struct{ Detail string }
UnprocessableError indicates a syntactically valid request that cannot be processed (RFC 4918 §11.2).
func (e UnprocessableError) Error() string
Error implements error.
Package httpproblem implements RFC 7807 "Problem Details for HTTP APIs" with a bridge to culpa errors.
The package provides a typed Problem struct, a FromError mapper that extracts culpa.CodeDetail and culpa.PublicDetail from the error chain, and a small set of sentinel constructors for common HTTP statuses.
The package is intentionally a subpackage of culpa rather than a top-level auxilia package so that culpa core remains free of the net/http import. Importers who need HTTP responses opt in by importing this package.