Guard vs pipe vs interceptor vs filter?
Practice this questionDefinition:
Guards, pipes, interceptors and filters are separate request extension points with different jobs. A guard decides whether work may proceed; a pipe validates or transforms arguments; an interceptor wraps execution; a filter handles uncaught errors. Choosing the correct point avoids mixing authentication, validation and formatting into business methods.
Key points:
-
Guard: “Can this request continue?” Pipe: “Is this input valid, and should it change type?”
-
Interceptor: wraps handler work for timing, response mapping or similar logic.
-
Filter: turns an uncaught exception into an error response.
-
Business rules that must apply outside HTTP belong in the service too, even if a guard or pipe handles request-level checks.
Comparison:
| Tool | Main question | Example |
|---|---|---|
| Guard | May this request proceed? | Require an admin role |
| Pipe | Is this argument valid? | Parse and validate an integer ID |
| Interceptor | What wraps handler execution? | Measure handler duration |
| Filter | How should an uncaught error be returned? | Map a domain error to HTTP |
Interview point:
- Give one concrete use for each and say when it runs in the lifecycle.