Interceptor usage and chain composition
An interceptor wraps the next request handler. Its request-side logic runs on the way in; its response/error logic runs on the way out. The first interceptor is therefore the outermost wrapper.
Installation
npm install @connectum/interceptorspnpm add @connectum/interceptorsbun add @connectum/interceptorsShare protobuf and Connect peers with the application; see peer dependency compatibility.
The default chain
Without a fallback, createDefaultInterceptors() builds enabled wrappers in this order:
With fallback enabled, validation moves directly outside it. Fallback stands outside circuitBreaker and retry, inside timeout and bulkhead. See Fallback position.
| Interceptor | Enablement | Guide |
|---|---|---|
| errorHandler | enabled by default | Error handling |
| timeout | opt-in | Timeout |
| bulkhead | opt-in | Bulkhead |
| circuitBreaker | opt-in | Circuit breaker |
| retry | opt-in | Retry |
| fallback | opt-in, requires a handler | Fallback |
| validation | enabled by default | Validation |
| serializer | opt-in | Serialization |
Request logging is a separate factory, not a member of this chain: Request logging.
Configure the server
With routes registered by your application:
import { createServer } from '@connectum/core';
import { createDefaultInterceptors } from '@connectum/interceptors';
const server = createServer({
services: [routes],
interceptors: createDefaultInterceptors({
timeout: { duration: 10_000 },
bulkhead: { capacity: 20, queueSize: 20 },
}),
});Error handling and validation remain enabled. Resilience changes request semantics and is opt-in; retry can repeat side effects, and a circuit breaker normally belongs on an outgoing client rather than an inbound server.
Select wrappers
Each key accepts true, false, undefined or an options object, except:
validationaccepts only a boolean.fallbackacceptsfalseor an options object with ahandler.fallback: trueis refused because there is no default fallback handler.
null, numbers, strings, arrays and functions are refused with a TypeError that names the key. An unsupported value never silently disables a wrapper.
The supported keys are errorHandler, timeout, bulkhead, circuitBreaker, retry, fallback, validation and serializer. Unknown keys throw a TypeError naming the key and listing the known keys, so timout cannot leave a protection disabled. The options argument itself must be an object.
The logger and method filter are not default-chain members. Create them with createLoggerInterceptor() and createMethodFilterInterceptor() and add them to the array yourself.
Resilience, logger and serializer factories validate options when called, including when the default-chain helper constructs them. Wrong types throw TypeError; accepted types with values outside their range throw RangeError. Diagnostics name the factory and option; numeric diagnostics also name the rule and received value. See each focused guide's option limits, the generated options API and option-validation migration.
Add custom middleware
Spread the default chain when your custom wrapper belongs inside it:
const interceptors = [...createDefaultInterceptors(), myCustomInterceptor];Authentication and authorization need a deliberate position immediately after error handling: errorHandler → AUTH → AUTHZ → resilience. Use the manual composition example instead of appending auth after the default chain.
The same interceptor array can be passed to a ConnectRPC client transport; see client interceptors.
Execution order
The first wrapper's before-logic runs first and its after-logic runs last. That is why error handling is outermost and serialization innermost. In the default chain the breaker wraps retry, so a logical request counts at most one breaker failure regardless of its retry attempts.
Native per-service interceptors replace the global chain; they do not append to it. Include every required wrapper in a replacement chain. Use method filtering to select wrappers by service/method patterns. skipGrpcServices matches service names starting with grpc., such as Health and Reflection; it does not skip the gRPC wire protocol.
Custom-wrapper practices
- Keep error handling outside work whose thrown/rejected errors it must normalize.
- Create a new request with
{ ...req, message: newMessage }rather than mutatingreq.messagein place. - Unless rejecting or synthesizing a response, return the result of
next(req). - Release timers and counters in
finally. - Use
import type { Interceptor }and factory functions for configurable wrappers. - Use method filtering for business routing;
skip*options describe a wrapper's call-kind or operational-service exclusions.
Previously linked sections
Follow an earlier link to its focused interceptor guide
- Chain composition
- Client error messages
- Cancellation and streaming scope
- Retry delay
- Circuit breaker placement and classification
- Fallback access refusals
- Fallback response
- Fallback position
- Validation before fallback
- Fallback handler result
- Option ranges and validation
- Bulkhead refusal
- Standalone usage
- Request logging
- What a call writes
- Message bodies are opt-in
- Logger failures