Guard vs pipe vs interceptor vs filter?
Try code in the playgroundUnderstand the concept
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.
Important ideas
-
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 |
Can you explain it without the notes?
Guard vs pipe vs interceptor vs filter?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
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.
Turn this concept into a clear answer.
- Give one concrete use for each and say when it runs in the lifecycle.
Adapted from 03_NESTJS.md · “Guard vs pipe vs interceptor vs filter?”. Examples are study fragments; framework behavior can depend on your version.