LESSON 08 / 18·2 min readIntermediate

Guard vs pipe vs interceptor vs filter?

Try code in the playground
01 Understand02 Recall03 Prepare

Understand 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:

ToolMain questionExample
GuardMay this request proceed?Require an admin role
PipeIs this argument valid?Parse and validate an integer ID
InterceptorWhat wraps handler execution?Measure handler duration
FilterHow should an uncaught error be returned?Map a domain error to HTTP
PAUSE & RECALL

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.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Give one concrete use for each and say when it runs in the lifecycle.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Why use a DTO and ValidationPipe?

Adapted from 03_NESTJS.md · “Guard vs pipe vs interceptor vs filter?”. Examples are study fragments; framework behavior can depend on your version.