interceptor

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: 3 Imported by: 0

Documentation

Overview

Package interceptor wraps a handler on both sides: before it runs, after it returns, and around a failure.

A guard answers yes or no; an interceptor observes and may change what happens — timing, caching, retries, a response header, an audit record written only when the write succeeded.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Use

func Use(is ...Interceptor) fiber.Handler

Use composes interceptors into one fiber middleware. The first listed is the outermost: it sees the request first and the response last.

Types

type Func

type Func struct {
	Label string
	Wrap  func(c fiber.Ctx, next fiber.Handler) error
}

Func adapts a plain function to Interceptor.

func (Func) Intercept

func (f Func) Intercept(c fiber.Ctx, next fiber.Handler) error

Intercept implements Interceptor.

func (Func) Name

func (f Func) Name() string

Name implements Interceptor.

type Interceptor

type Interceptor interface {
	Name() string
	Intercept(c fiber.Ctx, next fiber.Handler) error
}

Interceptor wraps the rest of the chain.

An implementation MUST call next exactly once on the paths where it intends the handler to run, and must return next's error unless it is deliberately replacing the outcome. Swallowing that error turns a failed write into a 200.

func After

func After(name string, fn func(c fiber.Ctx, handlerErr error)) Interceptor

After runs fn once the handler has returned, with the handler's error.

fn's own error is ignored on purpose: an audit write that fails must not turn a successful write into a 500 the client will retry.

func Header(key, value string) Interceptor

Header sets a response header on the way out.

func Of

func Of(name string, wrap func(c fiber.Ctx, next fiber.Handler) error) Interceptor

Of builds an Interceptor from a name and a function.

func OnSuccess

func OnSuccess(name string, fn func(c fiber.Ctx)) Interceptor

OnSuccess runs fn only when the handler returned no error. This is where an audit record or an outbox row belongs.

func Timing

func Timing(threshold time.Duration) Interceptor

Timing records how long the handler took and logs anything slower than threshold at warn.

It logs the slow ones only. A line per request is a metric wearing a log's clothing — it costs money to store and nobody reads it; a line for the 1% that were slow is read every time.

Jump to

Keyboard shortcuts

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