Documentation
¶
Overview ¶
errors/errors.go
errors/http.go
Index ¶
- Constants
- func As(err error, target any) bool
- func CodeFromError(err error) string
- func Handler(h http.Handler, logger *zap.Logger) http.Handler
- func HandlerFunc(h http.HandlerFunc, logger *zap.Logger) http.HandlerFunc
- func Is(err, target error) bool
- func Join(errs ...error) error
- func MethodNotAllowedHandler() http.Handler
- func Middleware(logger *zap.Logger) func(http.Handler) http.Handler
- func NotFoundHandler() http.Handler
- func StatusFromError(err error) int
- func WrapHandler(h ErrorHandlerFunc) http.HandlerFunc
- func WrapWithLogger(h ErrorHandlerFunc, logger *zap.Logger) http.HandlerFunc
- func Write(w http.ResponseWriter, err error)
- func WriteWithLogger(w http.ResponseWriter, err error, logger *zap.Logger)
- type Error
- func AlreadyExists(message string) *Error
- func AuthenticationFailed(message string) *Error
- func BadRequest(message string) *Error
- func Conflict(message string) *Error
- func Forbidden(message string) *Error
- func From(err error) *Error
- func Gone(message string) *Error
- func Internal(message string) *Error
- func InvalidInput(message string) *Error
- func MethodNotAllowed(message string) *Error
- func New(code, message string, status int) *Error
- func NotFound(message string) *Error
- func NotImplemented(message string) *Error
- func PermissionDenied(message string) *Error
- func ServiceUnavailable(message string) *Error
- func Timeout(message string) *Error
- func TooManyRequests(message string) *Error
- func Unauthorized(message string) *Error
- func UnprocessableEntity(message string) *Error
- func Validation(message string) *Error
- func Wrap(err error, code, message string, status int) *Error
- type ErrorHandlerFunc
- type FieldError
- type Response
- type ValidationErrors
Constants ¶
const ( CodeBadRequest = "bad_request" CodeForbidden = "forbidden" CodeNotFound = "not_found" CodeMethodNotAllowed = "method_not_allowed" CodeConflict = "conflict" CodeGone = "gone" CodeUnprocessableEntity = "unprocessable_entity" CodeTooManyRequests = "too_many_requests" CodeInternalError = "internal_error" CodeNotImplemented = "not_implemented" CodeTimeout = "timeout" CodeValidationFailed = "validation_failed" CodeAlreadyExists = "already_exists" CodeInvalidInput = "invalid_input" CodeAuthenticationFailed = "authentication_failed" CodePermissionDenied = "permission_denied" )
Common error codes as constants for consistency.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As finds the first error in err's chain that matches target. Re-exported from standard errors package for convenience.
func CodeFromError ¶
CodeFromError returns the error code for an error. Returns "internal_error" if the error is not an *Error.
func HandlerFunc ¶
func HandlerFunc(h http.HandlerFunc, logger *zap.Logger) http.HandlerFunc
HandlerFunc is like Handler but for http.HandlerFunc.
func Is ¶
Is reports whether any error in err's chain matches target. Re-exported from standard errors package for convenience.
func Join ¶
Join returns an error that wraps the given errors. Re-exported from standard errors package for convenience.
func MethodNotAllowedHandler ¶
MethodNotAllowedHandler returns an http.Handler that responds with 405 Method Not Allowed.
func Middleware ¶
Middleware returns middleware that handles errors returned from handlers. Use with ErrorHandlerFunc-style handlers via context.
func NotFoundHandler ¶
NotFoundHandler returns an http.Handler that responds with 404 Not Found.
func StatusFromError ¶
StatusFromError returns the HTTP status code for an error. Returns 500 if the error is not an *Error.
func WrapHandler ¶
func WrapHandler(h ErrorHandlerFunc) http.HandlerFunc
WrapHandler converts an ErrorHandlerFunc to a standard http.HandlerFunc.
func WrapWithLogger ¶
func WrapWithLogger(h ErrorHandlerFunc, logger *zap.Logger) http.HandlerFunc
WrapWithLogger converts an ErrorHandlerFunc to a standard http.HandlerFunc, logging internal errors.
func Write ¶
func Write(w http.ResponseWriter, err error)
Write writes an error as JSON to the response. It sets the appropriate HTTP status code and Content-Type header.
func WriteWithLogger ¶
func WriteWithLogger(w http.ResponseWriter, err error, logger *zap.Logger)
WriteWithLogger writes an error and logs internal errors.
Types ¶
type Error ¶
type Error struct {
// Code is a machine-readable error code (e.g., "not_found", "validation_failed")
Code string `json:"code"`
// Message is a human-readable error message
Message string `json:"message"`
// Status is the HTTP status code (not included in JSON by default)
Status int `json:"-"`
// Details contains additional error context (optional)
Details map[string]any `json:"details,omitempty"`
// Err is the underlying error (not included in JSON)
Err error `json:"-"`
}
Error represents a structured application error with code, message, and HTTP status.
func AlreadyExists ¶
AlreadyExists creates a 409 Conflict error with already_exists code.
func AuthenticationFailed ¶
AuthenticationFailed creates a 401 Unauthorized error with authentication_failed code.
func BadRequest ¶
BadRequest creates a 400 Bad Request error.
func InvalidInput ¶
InvalidInput creates a 400 Bad Request error with invalid_input code.
func MethodNotAllowed ¶
MethodNotAllowed creates a 405 Method Not Allowed error.
func NotImplemented ¶
NotImplemented creates a 501 Not Implemented error.
func PermissionDenied ¶
PermissionDenied creates a 403 Forbidden error with permission_denied code.
func ServiceUnavailable ¶
ServiceUnavailable creates a 503 Service Unavailable error.
func TooManyRequests ¶
TooManyRequests creates a 429 Too Many Requests error.
func Unauthorized ¶
Unauthorized creates a 401 Unauthorized error.
func UnprocessableEntity ¶
UnprocessableEntity creates a 422 Unprocessable Entity error.
func Validation ¶
Validation creates a 400 Bad Request error with validation code.
func (*Error) HTTPStatus ¶
HTTPStatus returns the HTTP status code for the error.
func (*Error) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Error) WithDetail ¶
WithDetail adds a single detail to the error.
func (*Error) WithDetails ¶
WithDetails adds details to the error.
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request) error
ErrorHandlerFunc is a handler function that returns an error. If the error is non-nil, it is written as JSON.
type FieldError ¶
type FieldError struct {
Field string `json:"field"`
Message string `json:"message"`
Code string `json:"code,omitempty"`
}
FieldError represents a validation error on a specific field.
type Response ¶
type Response struct {
Error *Error `json:"error"`
}
Response is the JSON structure returned for errors.
type ValidationErrors ¶
type ValidationErrors struct {
Errors []FieldError `json:"errors"`
}
ValidationErrors holds multiple field-level validation errors.
func NewValidationErrors ¶
func NewValidationErrors() *ValidationErrors
NewValidationErrors creates a new ValidationErrors.
func (*ValidationErrors) Add ¶
func (v *ValidationErrors) Add(field, message string) *ValidationErrors
Add adds a field error.
func (*ValidationErrors) AddWithCode ¶
func (v *ValidationErrors) AddWithCode(field, message, code string) *ValidationErrors
AddWithCode adds a field error with a code.
func (*ValidationErrors) Error ¶
func (v *ValidationErrors) Error() string
Error implements the error interface.
func (*ValidationErrors) HasErrors ¶
func (v *ValidationErrors) HasErrors() bool
HasErrors returns true if there are validation errors.
func (*ValidationErrors) ToError ¶
func (v *ValidationErrors) ToError() *Error
ToError converts ValidationErrors to an *Error if there are errors.