handlerx

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package handlerx provides a generic, framework-agnostic middleware chain for RPC handlers. It depends only on the errorx package within this module.

Handler[Req, Resp] is the core function type. Interceptor[Req, Resp] wraps a Handler to inject logic before and after the call. Chain composes multiple interceptors around a handler, executing them outermost-first.

Built-in interceptors: WithTimeout and WithRecovery.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler[Req, Resp any] func(ctx context.Context, req Req) (Resp, error)

Handler is a generic RPC handler function.

func Chain

func Chain[Req, Resp any](handler Handler[Req, Resp], interceptors ...Interceptor[Req, Resp]) Handler[Req, Resp]

Chain composes interceptors around a handler and returns a new Handler. Execution order: interceptors[0] is outermost, handler is innermost. Calling Chain with no interceptors returns the handler unchanged.

type Interceptor

type Interceptor[Req, Resp any] func(ctx context.Context, req Req, next Handler[Req, Resp]) (Resp, error)

Interceptor wraps a Handler, allowing logic to run before and after the next handler in the chain.

func WithRecovery

func WithRecovery[Req, Resp any]() Interceptor[Req, Resp]

WithRecovery returns an Interceptor that catches panics from downstream handlers and converts them to errors, preventing goroutine crashes. The returned error wraps panicx.ErrPanic and can be detected with errors.Is(err, panicx.ErrPanic).

Note: runtime.Goexit is not a panic and is not caught by WithRecovery.

func WithTimeout

func WithTimeout[Req, Resp any](d time.Duration) Interceptor[Req, Resp]

WithTimeout returns an Interceptor that applies a timeout to the request context. The timeout is enforced per call.

Jump to

Keyboard shortcuts

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