errmux

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package errmux provides types that extend http.ServeMux with error handling capabilities.

ServeMux as defined by this package works exactly the same as http.ServeMux with the exception, that Handler and HandlerFunc respectively return an error value. Any non-nil error causes the response to be discarded and the error gets handled.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandler

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

ErrorHandler defines a function type for handling errors that occured during request handling. The function is passed the original http.ResponseWriter and error handling happens unbuffered.

type Handler

type Handler interface {
	// ServeHTTP serves an [http.Request] producing response to an
	// [http.ResponseWriter]. The writer is buffered and any data written may
	// be discarded if ServeHTTP returns a non-nil error.
	ServeHTTP(http.ResponseWriter, *http.Request) error
}

Handler defines an extension of http.Handler that returns and error value which causes normal response handling to be aborted and the error being handled.

type HandlerFunc

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

HandlerFunc is a convenience function type to implement Handler with just a function.

func (HandlerFunc) ServeHTTP

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

type ServeMux

type ServeMux struct {
	ErrorHandler ErrorHandler
	// contains filtered or unexported fields
}

ServeMux works like a http.ServeMux but with support for error-aware request handling.

Example
package main

import (
	"errors"
	"fmt"
	"net/http"

	"github.com/halimath/httputils/errmux"
	"github.com/halimath/httputils/response"
)

func main() {
	mux := errmux.NewServeMux()

	errMissingQueryParameter := errors.New("missing query parameter")

	mux.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) error {
		if msg := r.URL.Query().Get("msg"); len(msg) > 0 {
			return response.PlainText(w, r, msg)
		}

		return fmt.Errorf("%w: %s", errMissingQueryParameter, "msg")
	})

	mux.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
		if errors.Is(err, errMissingQueryParameter) {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}

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

	http.ListenAndServe(":8080", mux)
}

func NewServeMux

func NewServeMux() *ServeMux

func (*ServeMux) Handle

func (mux *ServeMux) Handle(pattern string, handler Handler)

Handle registers the handler for the given pattern. If the given pattern conflicts, with one that is already registered, Handle panics.

func (*ServeMux) HandleFunc

func (mux *ServeMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request) error)

HandleFunc registers the handler function for the given pattern. If the given pattern conflicts, with one that is already registered, HandleFunc panics.

func (*ServeMux) ServeHTTP

func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.

Jump to

Keyboard shortcuts

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