gonest
Request Pipeline

Filters

Catching specific exception types and overriding their response

A Filter catches one specific exception type and overrides its default response. Build one with gonest.NewFilter(func(filter *gonest.Filter) {...}).

var FooFilter = gonest.NewFilter(func(filter *gonest.Filter) {
  filter.Catch(&FooError{}, func(ctx *gonest.RestContext, exc *FooError) {
    ctx.Status(http.StatusTeapot).Json(map[string]any{"custom": true})
  })
})

Catch(exemplar, handler) matches by the exact concrete type of the panic value (via reflect.TypeOf) — not an interface or a type hierarchy. Uncaught types fall through to the default {name, message, details} panic handler (see Exceptions).

Applying a filter

var UserController = gonest.NewController(func(controller *gonest.Controller) {
  controller.Filters(FooFilter)
})

var AppModule = gonest.NewModule(func(module *gonest.Module) {
  module.Filters(FooFilter) // global — root module only
})

A controller-level filter wins over a global one for the same caught type. Filter wraps the entire pipeline — Middleware, Guard, Interceptor, and Handler — so it catches panics from anywhere inside that chain, not just the Handler. See Overview & Execution Order.

Next steps

On this page