04 / 05

What is the binding order precedence when the same enhancer type is applied at global, controller, and method level in NestJS?

For guards, interceptors, and pipes the execution order is global → controller → method. For exception filters the resolution order inverts: method → controller → global. The first filter whose @Catch() type matches the thrown exception handles it.

Guard execution order across binding levels
Precedence rules by enhancer type:
  1. 1

    Guards — global → controller → method. All must return true for the request to proceed.

  2. 2

    Interceptors — global → controller → method (pre). Reverse on the way out (post).

  3. 3

    Pipes — global → controller → method. Each pipe runs on its bound parameter.

  4. 4

    Exception filters — method → controller → global. First matching @Catch() type wins.

  5. 5

    The filter reversal is the most commonly confused rule — remember: most specific filter wins.

Difficulty: 5/10
Topics: enhancer precedence, global vs controller vs method, NestJS lifecycle

Scenario Questions

0-2 years experience
  1. 1

    You have a NestJS controller with a @UseGuards(AuthGuard) at the controller level and also a @UseGuards(RoleGuard) on a specific route method. Which guard runs first when a request hits that route, and why?

  2. 2

    If you register a global pipe that trims strings, but also add a method‑level pipe that validates an email format, what will be the order of execution for a request to that method?

  3. 3

    Suppose you accidentally apply the same interceptor both globally and on a controller. What effect does the duplicate have on the request handling order?

2-5 years experience
  1. 1

    During a code review you notice that a method‑level exception filter is not catching errors that a controller‑level filter catches. Explain why that might happen and how you would fix it.

  2. 2

    Your team added a global rate‑limiting interceptor, but a specific endpoint still allows unlimited calls. Walk through the steps you’d take to debug the binding order.

  3. 3

    You need to enforce different validation rules for a POST endpoint while keeping a global validation pipe. How would you arrange the pipes, and what precedence rules would you rely on?

5-8 years experience
  1. 1

    Design a strategy for applying logging interceptors at different layers (global, controller, method) while ensuring that method‑level logs are not duplicated. Discuss how NestJS binding order influences your design.

  2. 2

    In a microservice gateway you have a global authentication guard, a controller‑level role guard, and a method‑level permission guard. Explain the security implications if the precedence were reversed, and how you would enforce the correct order.

  3. 3

    You discover a performance regression caused by a globally applied serialization interceptor that runs after a method‑level interceptor that already serializes the response. How would you restructure the enhancer bindings to avoid redundant work?

8+ years experience
  1. 1

    Your organization is migrating legacy Express middleware into NestJS enhancers. How would you decide which middleware becomes a global, controller, or method enhancer, considering binding precedence and future maintainability?

  2. 2

    Across multiple teams, some services rely on global exception filters while others need fine‑grained method filters. Propose a governance model that leverages NestJS precedence to avoid conflicts and ensure consistent error handling.

  3. 3

    When scaling to thousands of endpoints, you notice that the order of interceptor execution impacts request latency. How would you audit and refactor the enhancer hierarchy to optimize performance while preserving semantics?

Follow-up Questions

  • What changes if you remove the method‑level decorator?
  • When might you prefer a global enhancer over a more specific one?
  • How does this precedence impact error handling compared to request transformation?