httperror

package
v0.0.0-...-56d8f82 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 3 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultErrorHandler = ErrorHandlerFunc(func(w http.ResponseWriter, r *http.Request, err error) {
	if err, ok := err.(HttpError); ok {
		http.Error(w, err.Error(), err.StatusCode())
		return
	}

	http.Error(w, err.Error(), http.StatusInternalServerError)
})

DefaultErrorHandler is the default error handler.

Functions

func FormatError

func FormatError(err error) string

TODO: move it to some other module.

func WithErrorHandler

func WithErrorHandler(errorHandler ErrorHandler) func(HandlerFunc) http.HandlerFunc

WithErrorHandler returns an http.Handler that handles errors by errorHandler.

Types

type ErrorHandler

type ErrorHandler interface {
	ServeHTTP(w http.ResponseWriter, r *http.Request, err error)
}

ErrorHandler is an interface that can handle errors returned by an Handler.

type ErrorHandlerFunc

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)

ErrorHandlerFunc is an adapter to handle errors returned by an Handler.

func (ErrorHandlerFunc) ServeHTTP

func (f ErrorHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, err error)

type Handler

type Handler interface {
	ServeHTTP(w http.ResponseWriter, r *http.Request) error
}

Handler is like http.Handler, but with an additional error return value.

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

HandlerFunc is an adapter to allow the use of ordinary functions as HTTP handlers.

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error

type HttpError

type HttpError struct {
	// contains filtered or unexported fields
}

HttpError is an error that cares additional information about the HTTP status for the error.

func FromError

func FromError(err error, code int, message ...string) HttpError

FromError creates a new HttpError with the given error and code.

func New

func New(message string, code int, errors ...error) HttpError

New creates a new HttpError with the given message and code.

func (HttpError) Error

func (e HttpError) Error() string

func (HttpError) StatusCode

func (e HttpError) StatusCode() int

func (HttpError) Unwrap

func (e HttpError) Unwrap() []error

Jump to

Keyboard shortcuts

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