filter

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package filter is the last line of the request pipeline: it turns anything that escaped a handler into a response.

Install ErrorHandler as Fiber's Config.ErrorHandler and no code path can return a bare Go error to a client.

What it guarantees, which hand-written error handling does not:

  • Every failure is logged ONCE, with its internal cause, under the request's trace id — including the ones a handler returned without logging.
  • No unclassified error's text reaches a client. An error that is not an *errs.Error is by definition one nobody checked for leaks, so it becomes a generic 500 and the original goes to the log.
  • A 404 for an unrouted path looks like every other response, so a client's error parsing does not have to special-case the framework's own replies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(opt Options) func(fiber.Ctx, error) error

ErrorHandler returns a handler for fiber.Config.ErrorHandler.

app := fiber.New(fiber.Config{ErrorHandler: filter.ErrorHandler(filter.Options{})})

func NotFoundHandler

func NotFoundHandler() fiber.Handler

NotFoundHandler is the terminal handler for unmatched routes. Register it LAST, after every module: `app.Use(filter.NotFoundHandler())`.

Types

type Filter

type Filter interface {
	Name() string
	Catch(c fiber.Ctx, err error) (handled bool, writeErr error)
}

Filter handles errors of a shape it recognises.

Catch returns handled=false to pass the error to the next filter. The chain always ends in the built-in behaviour, so a filter never has to handle everything.

func Of

func Of(name string, fn func(c fiber.Ctx, err error) (bool, error)) Filter

Of builds a Filter from a name and a function.

type Func

type Func struct {
	Label string
	Fn    func(c fiber.Ctx, err error) (bool, error)
}

Func adapts a plain function to Filter.

func (Func) Catch

func (f Func) Catch(c fiber.Ctx, err error) (bool, error)

Catch implements Filter.

func (Func) Name

func (f Func) Name() string

Name implements Filter.

type Options

type Options struct {
	// Filters run in order before the built-in mapping.
	Filters []Filter
	// LogClientErrors logs 4xx responses too. Off by default: a thousand
	// validation failures a minute is a client bug, and logging them buries
	// the server bugs.
	LogClientErrors bool
}

Options configures the error handler.

Jump to

Keyboard shortcuts

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