Bird
0
0

You want to create a TimeoutInterceptor that applies a 3-second timeout globally but allows a specific route to override it with a 10-second timeout. Which approach correctly implements this behavior?

hard📝 Application Q15 of 15
NestJS - Interceptors

You want to create a TimeoutInterceptor that applies a 3-second timeout globally but allows a specific route to override it with a 10-second timeout. Which approach correctly implements this behavior?

ASet timeout to 10 seconds globally and manually check route inside interceptor to reduce to 3 seconds if needed.
BApply two TimeoutInterceptors globally, one with 3 seconds and one with 10 seconds; the last one wins.
CCreate two separate interceptors and apply the 10-second one only on the specific route without global interceptor.
DUse a global TimeoutInterceptor with 3 seconds and a custom decorator to set metadata for 10 seconds, then read metadata inside the interceptor to override timeout.
Step-by-Step Solution
Solution:
  1. Step 1: Understand global and route-specific timeout needs

    Global interceptor applies 3 seconds by default; specific routes can override by metadata.
  2. Step 2: Implement override using custom decorator and metadata

    Custom decorator sets timeout metadata on route; interceptor reads it and uses that value if present.
  3. Step 3: Evaluate other options

    Applying multiple global interceptors causes conflicts; setting longer timeout globally and reducing for most routes reverses the logic; creating separate interceptors without a global one loses timeout protection on other routes.
  4. Final Answer:

    Use a global TimeoutInterceptor with 3 seconds and a custom decorator to set metadata for 10 seconds, then read metadata inside the interceptor to override timeout. -> Option D
  5. Quick Check:

    Use metadata + decorator to override global timeout [OK]
Quick Trick: Use custom decorator + metadata to override global interceptor timeout [OK]
Common Mistakes:
  • Applying multiple global interceptors causing conflicts
  • Hardcoding timeout values without metadata
  • Not reading route metadata inside interceptor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes