httperr

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

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

func Code(err error) int

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).

func New

func New(message string, code int) error

New creates a new error with the given message and HTTP status code. This is a convenience function equivalent to WithCode(errors.New(message), code).

func WithCode

func WithCode(err error, code int) error

WithCode wraps an error with an HTTP status code. The returned error implements Unwrap() for use with errors.Is() and errors.As(). If err is nil, WithCode returns nil.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL