errhandler

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: MIT Imports: 2 Imported by: 1

README

errhandler

Wraps Go's http.HandlerFunc, allowing for simpler use of http.NewServeMux().

Install
go get github.com/codingconcepts/errhandler
Usage

Wrap Go's stdlib http.HandlerFunc using errhandler.Wrap():

mux := http.NewServeMux()
mux.Handle("POST /products", errhandler.Wrap(addProduct))

Update your existing handler signatures, by adding an error return type and utilizing the optional errhandler.ParseJSON and errhandler.SendJSON helper functions to reduce the amount of boilerplate code handlers require:

func addProduct(w http.ResponseWriter, r *http.Request) error {
  var p product
  if err := errhandler.ParseJSON(r, &p); err != nil {
    return err

    // Or, if you'd prefer to customise the status code:
    // return errhandler.Error(http.StatusUnprocessableEntity, err)
  }
  
  products[p.ID] = p
  
  return errhandler.SendJSON(w, p)
}
Middleware

errhandler contains helper objects for building middleware

  • A errhandler.Middleware type, which is simply a function that takes a Wrap function and returns a Wrap function:
mux := http.NewServeMux()
mux.Handle("GET /products/{id}", errhandler.Wrap(midLog(getProduct)))

...

func midLog(n errhandler.Wrap) errhandler.Wrap {
	return func(w http.ResponseWriter, r *http.Request) error {
		log.Printf("1 %s %s", r.Method, r.URL.Path)
		return n(w, r)
	}
}

func addProduct(w http.ResponseWriter, r *http.Request) error {
  ...
}
  • A errhandler.Chain function, which allows Middleware functions easy to chain:
chain := errhandler.Chain(midLog1, midLog2)

mux := http.NewServeMux()
mux.Handle("GET /products/{id}", errhandler.Wrap(chain(getProducts)))

func midLog1(n errhandler.Wrap) errhandler.Wrap {
	return func(w http.ResponseWriter, r *http.Request) error {
		log.Printf("1 %s %s", r.Method, r.URL.Path)
		return n(w, r)
	}
}

func midLog2(n errhandler.Wrap) errhandler.Wrap {
	return func(w http.ResponseWriter, r *http.Request) error {
		log.Printf("2 %s %s", r.Method, r.URL.Path)
		return n(w, r)
	}
}

func addProduct(w http.ResponseWriter, r *http.Request) error {
  ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseJSON

func ParseJSON(r *http.Request, val any) error

ParseJSON can be used to parse a string from the body of a request.

func SendJSON

func SendJSON(w http.ResponseWriter, data interface{}) error

SendJSON returns a JSON response to the caller, or fails with an error.

func SendString

func SendString(w http.ResponseWriter, message string) error

SendString returns a string response to the caller, or fails with an error.

Types

type HTTPError added in v0.0.4

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

HTTPError allows you to return custom status codes.

func Error added in v0.0.4

func Error(status int, err error) HTTPError

Error returns a new instance of Error.

func (HTTPError) Error added in v0.0.4

func (err HTTPError) Error() string

type Middleware added in v0.0.5

type Middleware func(Wrap) Wrap

Middleware is a helper for adding middleware to wrapped handelers.

func Chain added in v0.0.5

func Chain(m ...Middleware) Middleware

Chain multiple middleware functions together into a single middleware.

type Wrap

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

Wrap an http.HandlerFunc, allowing for errors to be handled in a more familiar way inside your handlers.

func (Wrap) ServeHTTP

func (fn Wrap) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is invoked when the HTTP handler is called, capturing and returning any errors encountered in the ErrHandlerFunc.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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