httperr

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 4 Imported by: 7

README

httperr

Build Status Coverage Status

I've been doing this in several different projects, I finally decided to convert it to a proper lib.

The idea is to add an error return to HTTP handler functions, so you can avoid writing if err != nil { http.Error(w, err); return } everywhere.

The basic usage looks like:

mux.Handle("/", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
  err := doSomething()
  return err
}))

This will yield a 500 and return a JSON like {"error":"doSomething error"}.

The lib also provide a Wrap function, so you can decide which status code you want:

mux.Handle("/e", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
  err := doSomething()
  return httperr.Wrap(err, http.StatusBadRequest)
}))

Or, you can throw errors with a status, e.g.:

mux.Handle("/e", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
  if something {
	return httperr.Errorf(http.StatusBadRequest, "something: %v", something)
  }
  return nil
}))

So, this is it! You can also check the examples folder for a "real" usage.

Documentation

Overview

Package httperr provides a middleware to make it easier to handle http errors in a common way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultErrorHandler added in v1.3.0

func DefaultErrorHandler(w http.ResponseWriter, err error, status int)

DefaultErrorHandler is the default error handler. It converts the error to JSON and prints writes it to the response.

func Errorf added in v1.2.0

func Errorf(status int, format string, args ...interface{}) error

Errorf creates a new error and wraps it with the given status

func New

func New(next Handler) http.Handler

New wraps a given http.Handler and returns a http.Handler.

func NewF

func NewF(next HandlerFunc) http.Handler

NewF wraps a given http.HandlerFunc and return a http.Handler.

func NewFWithHandler added in v1.3.0

func NewFWithHandler(next HandlerFunc, eh ErrorHandler) http.Handler

NewFWithHandler wraps a given http.HandlerFunc and return a http.Handler. You can also customize how the error is handled.

func NewWithHandler added in v1.3.0

func NewWithHandler(next Handler, eh ErrorHandler) http.Handler

NewWithHandler() wraps a given http.Handler and returns a http.Handler. You can also customize how the error is handled.

func Wrap

func Wrap(err error, status int) error

Wrap a given error with the given status. Returns nil if the given error is nil.

Types

type Error

type Error struct {
	Err    error
	Status int
}

Error is a HTTP error with an underlying error and a status code.

func (Error) Error

func (e Error) Error() string

func (Error) Is added in v1.4.0

func (e Error) Is(err error) bool

Is conforms with errors.Is.

type ErrorHandler added in v1.3.0

type ErrorHandler func(w http.ResponseWriter, err error, status int)

ErrorHandler handles an error.

type Handler

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

Handler is like http.Handler, but the ServeHTTP method can also return an error.

type HandlerFunc

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

HandlerFunc is just like http.HandlerFunc.

func (HandlerFunc) ServeHTTP

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

ServeHTTP calls f(w, r).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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