Documentation
¶
Overview ¶
Package httperr provides error types with HTTP status codes for API error handling.
This package allows errors to carry their intended HTTP response code through the call stack, enabling centralized error handling in API handlers. The CodedError type implements the standard error interface and supports error wrapping via errors.Is() and errors.As().
Basic Usage ¶
Create errors with HTTP status codes:
// Create a new error with a status code
err := httperr.New("resource not found", http.StatusNotFound)
// Wrap an existing error with a status code
err := httperr.WithCode(err, http.StatusBadRequest)
Extracting Status Codes ¶
Extract the HTTP status code from an error chain:
code := httperr.Code(err) // Returns the code if err contains a CodedError // Returns http.StatusInternalServerError (500) if no CodedError found // Returns http.StatusOK (200) if err is nil
Error Wrapping ¶
CodedError supports the standard Go error wrapping pattern:
sentinel := errors.New("database connection failed")
err := httperr.WithCode(sentinel, http.StatusServiceUnavailable)
// errors.Is works through the wrapper
if errors.Is(err, sentinel) {
// handle specific error
}
// errors.As can extract the CodedError
var coded *httperr.CodedError
if errors.As(err, &coded) {
log.Printf("HTTP %d: %s", coded.HTTPCode(), coded.Error())
}
HTTP Handler Example ¶
Use in an HTTP handler for centralized error responses:
func handleError(w http.ResponseWriter, err error) {
code := httperr.Code(err)
http.Error(w, err.Error(), code)
}
func myHandler(w http.ResponseWriter, r *http.Request) {
result, err := doSomething()
if err != nil {
handleError(w, err)
return
}
// ...
}
Package httperr provides error types with HTTP status codes for API error handling.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Code ¶
Code extracts the HTTP status code from an error. It unwraps the error chain looking for a CodedError. If no CodedError is found, it returns http.StatusInternalServerError (500).
Types ¶
type CodedError ¶
type CodedError struct {
// contains filtered or unexported fields
}
CodedError wraps an error with an HTTP status code. This allows errors to carry their intended HTTP response code through the call stack, enabling centralized error handling in API handlers.
func (*CodedError) Error ¶
func (e *CodedError) Error() string
Error implements the error interface.
func (*CodedError) HTTPCode ¶
func (e *CodedError) HTTPCode() int
HTTPCode returns the HTTP status code associated with this error.
func (*CodedError) Unwrap ¶
func (e *CodedError) Unwrap() error
Unwrap returns the underlying error for errors.Is() and errors.As() compatibility.