[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"verticals":3,"article-nestjs-weekly-app-filter-scope":44,"search-suggestions":469,"path-membership-nestjs-weekly-app-filter-scope":509,"related-nestjs-weekly-app-filter-scope":510,"code:ts:true:t7j5ih":616,"code:ts:true:nlqxvu":617,"code:ts:true:oruwz8":618,"code:ts:true:1fuddzt":619,"code:ts:true:kuen0b":620,"comments-01a18121-99fa-77f0-83de-5cc8814cbbd4":621},[4,20,32],{"id":5,"slug":6,"name":7,"tagline":8,"description":9,"accentFrom":10,"accentTo":11,"icon":12,"defaultLocale":13,"locales":14,"features":16,"position":19},"019fe637-3d33-714b-b57f-23e163ffca0c","dev","Web Development","Read it. Run it. Prove it.","A post a day on modern web development — most with an editable playground and a quiz that explains every answer. Free, no account needed.","violet-500","cyan-400","◇","en",[13,15],"fa",{"courses":17,"paths":17,"articles":17,"exams":18,"flashcards":18,"packages":17,"community":17,"certificates":17,"teams":17,"commerce":17},true,false,0,{"id":21,"slug":22,"name":23,"tagline":24,"description":25,"accentFrom":26,"accentTo":10,"icon":27,"defaultLocale":13,"locales":28,"features":30,"position":31},"019fe637-3dc2-754c-8657-0f175bfee7c6","lang","Languages","Learn a language the way you learn a codebase.","Grammar explained the way good documentation explains an API — one idea at a time, each with a quiz.","amber-400","⌘",[13,15,29],"es",{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":17,"packages":18,"community":17,"certificates":17,"teams":18,"commerce":18},2,{"id":33,"slug":34,"name":35,"tagline":36,"description":37,"accentFrom":38,"accentTo":39,"icon":40,"defaultLocale":13,"locales":41,"features":42,"position":43},"7b3c16f2-931d-410e-802e-e1fa4edab7de","soft","Soft Skills","The half of the job nobody wrote documentation for.","Weekly, on the parts of working life that decide more than your code does — first weeks, meetings, interviews, promotions, and the people around you. Written from what actually happens, and recorded as a podcast you can listen to on the walk.","emerald-400","teal-300","◉",[13],{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":18,"packages":18,"community":17,"certificates":18,"teams":18,"commerce":18},3,{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":53,"likeCount":19,"commentCount":19,"author":54,"vertical":59,"topic":60,"tags":63,"_count":74,"playground":76,"body":78,"bodyMd":409,"seo":410,"translationGroupId":412,"series":413,"podcastUrl":48,"verticalId":5,"thread":457,"assessments":459,"translations":465,"quiz":467},"01a18121-99fa-77f0-83de-5cc8814cbbd4","nestjs-weekly-app-filter-scope","NestJS APP_FILTER Scope: Why Your Global Filter Might Not Fire",null,"A request-scoped APP_FILTER in NestJS can silently stop catching exceptions. Learn the fix NestJS 12 shipped, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-app-filter-scope.png",15,"2026-11-21T06:53:12.171Z",26,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},"019fe637-3c25-7088-9034-39c9f15dc3c8","Parsa Jiravand","parsa","Frontend engineer · building bestpractic",{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},"nestjs","Nestjs",[64,65,68,71],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},"typescript","Typescript",{"slug":69,"name":70,"color":48},"node","Node",{"slug":72,"name":73,"color":48},"tutorial","Tutorial",{"assessments":75},1,{"slug":46,"title":77},"NestJS APP_FILTER scope playground",{"blocks":79,"version":75},[80,84,87,90,95,98,107,110,113,116,130,134,137,143,147,151,154,157,160,163,166,169,172,175,178,181,184,187,190,194,198,201,204,207,214,217,220,223,228,231,234,237,240,243,246,250,253,256,259,262,265,268,271,274,282,285,293,296,300,303,307,310,313,316,319,322,325,328,331,363,366,373,376,379,382,385,391,394,397,400,403],{"id":81,"html":82,"type":83},"b1","\u003Cp>A team registers a global exception filter with \u003Ccode>APP_FILTER\u003C\u002Fcode> so every unhandled error gets the same JSON shape and the same Sentry breadcrumb. It works in every test, every staging deploy, every manual check. Then, weeks later, someone notices a slice of production 500s never reached Sentry at all — same error types, same routes, just silently missing. Nobody touched the filter. Nobody touched the module it lives in.\u003C\u002Fp>","paragraph",{"id":85,"html":86,"type":83},"b2","\u003Cp>The filter was never a reliable singleton at all. The moment its constructor started injecting a request-scoped provider — to log the current tenant, say — Nest quietly turned the \u003Cem>filter itself\u003C\u002Fem> request-scoped too. And for every NestJS release before 12.1.3, a \u003Ccode>REQUEST\u003C\u002Fcode>- or \u003Ccode>TRANSIENT\u003C\u002Fcode>-scoped class registered as a global enhancer this way wasn&#39;t rejected, wasn&#39;t logged, wasn&#39;t flagged at boot — Nest built it anyway, as an empty object whose constructor had never run, and handed it real requests to handle. \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18104\">The dependency it needed just wasn&#39;t there\u003C\u002Fa>. This article is about that gap, the fix NestJS 12 actually shipped for it (in two separate releases, for two different reasons), and the real-but-narrower hole in exception-filter coverage that remains today.\u003C\u002Fp>",{"id":88,"html":89,"type":83},"b3","\u003Cp>This is written against \u003Cstrong>NestJS 12.5.0\u003C\u002Fstrong> (verified against \u003Ccode>@nestjs\u002Fcore\u003C\u002Fcode>&#39;s npm dist-tags; the 11.x line is on the \u003Ccode>legacy\u003C\u002Fcode> npm tag at \u003Ccode>11.2.10\u003C\u002Fcode>). The specific fixes this article walks through landed in \u003Cstrong>12.1.3\u003C\u002Fstrong> and \u003Cstrong>12.2.0\u003C\u002Fstrong> — both are included in 12.5.0 — and everything described is core-container behavior, so it is identical whether your \u003Ccode>HttpAdapter\u003C\u002Fcode> is Express or Fastify.\u003C\u002Fp>",{"id":91,"html":92,"text":93,"type":94,"level":31},"b4","What you&#39;ll learn","What you'll learn","heading",{"id":96,"html":97,"type":83},"b5","\u003Cp>By the end of this article you&#39;ll be able to:\u003C\u002Fp>",{"id":99,"type":100,"items":101,"ordered":18},"b6","list",[102,103,104,105,106],"Explain exactly how a provider&#39;s injection scope can propagate into a global guard, interceptor, pipe, or filter without you ever writing \u003Ccode>scope:\u003C\u002Fcode> on it","Name the real difference between \u003Ccode>app.useGlobalFilters()\u003C\u002Fcode> and the \u003Ccode>APP_FILTER\u003C\u002Fcode> provider token, and why only one of them can inject dependencies at all","State precisely what changed in NestJS 12.1.3 and 12.2.0, and why filters now get different treatment than guards and interceptors","Recognize the one combination that still silently loses coverage today, even on a current NestJS install","Design a global exception filter that stays reliably global, on purpose",{"id":108,"html":109,"text":109,"type":94,"level":31},"b7","Who this is for",{"id":111,"html":112,"type":83},"b8","\u003Cp>You&#39;ve written a custom exception filter and registered it with \u003Ccode>APP_FILTER\u003C\u002Fcode> from \u003Ccode>@nestjs\u002Fcore\u003C\u002Fcode>. You don&#39;t need prior experience with Nest&#39;s injection scopes — this article builds that model from the ground up.\u003C\u002Fp>",{"id":114,"html":115,"text":115,"type":94,"level":31},"b9","Table of contents",{"id":117,"type":100,"items":118,"ordered":18},"b10",[119,120,121,122,123,124,125,126,127,128,129],"\u003Ca href=\"#the-problem-a-global-filter-that-wasnt\">The problem: a global filter that wasn&#39;t\u003C\u002Fa>","\u003Ca href=\"#the-mental-model-scope-bubbles-enhancers-inherit-it\">The mental model: scope bubbles, enhancers inherit it\u003C\u002Fa>","\u003Ca href=\"#stage-1-two-ways-to-register-a-filter-one-real-difference\">Stage 1: two ways to register a filter, one real difference\u003C\u002Fa>","\u003Ca href=\"#stage-2-how-a-filter-accidentally-stops-being-a-singleton\">Stage 2: how a filter accidentally stops being a singleton\u003C\u002Fa>","\u003Ca href=\"#stage-3-what-nestjs-1213-changed\">Stage 3: what NestJS 12.1.3 changed\u003C\u002Fa>","\u003Ca href=\"#stage-4-what-nestjs-1220-changed-for-filters-specifically\">Stage 4: what NestJS 12.2.0 changed for filters specifically\u003C\u002Fa>","\u003Ca href=\"#stage-5-the-gap-thats-still-there-today\">Stage 5: the gap that&#39;s still there today\u003C\u002Fa>","\u003Ca href=\"#edge-cases-and-gotchas\">Edge cases and gotchas\u003C\u002Fa>","\u003Ca href=\"#best-practices\">Best practices\u003C\u002Fa>","\u003Ca href=\"#faq\">FAQ\u003C\u002Fa>","\u003Ca href=\"#cheat-sheet\">Cheat sheet\u003C\u002Fa>",{"id":131,"html":132,"text":133,"type":94,"level":31},"b11","The problem: a global filter that wasn&#39;t","The problem: a global filter that wasn't",{"id":135,"html":136,"type":83},"b12","\u003Cp>Here&#39;s the filter, trimmed to the part that matters:\u003C\u002Fp>",{"id":138,"code":139,"type":140,"language":141,"highlight":142},"b13","\u002F\u002F all-exceptions.filter.ts\n@Catch()\n@Injectable()\nexport class AllExceptionsFilter implements ExceptionFilter {\n  constructor(private readonly tenantContext: TenantContextService) {}\n\n  catch(exception: unknown, host: ArgumentsHost) {\n    const ctx = host.switchToHttp();\n    const response = ctx.getResponse();\n    \u002F\u002F tenantContext reads the current request's tenant for the error report\n    reportToSentry(exception, this.tenantContext.tenantId);\n    response.status(500).json({ message: 'Internal server error' });\n  }\n}","code","ts",[],{"id":144,"code":145,"type":140,"language":141,"highlight":146},"b14","\u002F\u002F tenant-context.service.ts\n@Injectable({ scope: Scope.REQUEST })\nexport class TenantContextService {\n  constructor(@Inject(REQUEST) private readonly request: Request) {}\n  get tenantId() {\n    return this.request.headers['x-tenant-id'];\n  }\n}",[],{"id":148,"code":149,"type":140,"language":141,"highlight":150},"b15","\u002F\u002F app.module.ts\n@Module({\n  providers: [\n    TenantContextService,\n    { provide: APP_FILTER, useClass: AllExceptionsFilter },\n  ],\n})\nexport class AppModule {}",[],{"id":152,"html":153,"type":83},"b16","\u003Cp>Nothing here looks wrong. \u003Ccode>AllExceptionsFilter\u003C\u002Fcode> is registered with \u003Ccode>APP_FILTER\u003C\u002Fcode>, which the docs describe as making a filter global &quot;regardless of the module in which this construction is employed.&quot; It never calls \u003Ccode>scope: Scope.REQUEST\u003C\u002Fcode> on itself — only \u003Ccode>TenantContextService\u003C\u002Fcode> does that, so it can read the inbound request. But \u003Ccode>TenantContextService\u003C\u002Fcode> is \u003Ccode>REQUEST\u003C\u002Fcode>-scoped, and injecting a \u003Ccode>REQUEST\u003C\u002Fcode>-scoped provider into another provider&#39;s constructor makes \u003Cem>that\u003C\u002Fem> provider \u003Ccode>REQUEST\u003C\u002Fcode>-scoped too. That&#39;s not a bug — it&#39;s how Nest&#39;s injection scope has always worked. It just means \u003Ccode>AllExceptionsFilter\u003C\u002Fcode> is \u003Ccode>REQUEST\u003C\u002Fcode>-scoped now, whether or not anyone intended it.\u003C\u002Fp>",{"id":155,"html":156,"type":83},"b17","\u003Cp>On every NestJS release before 12.1.3, that combination — a \u003Ccode>REQUEST\u003C\u002Fcode>- or \u003Ccode>TRANSIENT\u003C\u002Fcode>-scoped class registered as a global enhancer via \u003Ccode>useClass\u003C\u002Fcode> — booted cleanly, with no startup error and no warning. But it wasn&#39;t a quiet skip: Nest still constructed \u003Ccode>AllExceptionsFilter\u003C\u002Fcode>, as a bare instance whose constructor body never ran, and still called \u003Ccode>catch()\u003C\u002Fcode> on that instance for every matching exception. \u003Ccode>this.tenantContext\u003C\u002Fcode> was \u003Ccode>undefined\u003C\u002Fcode> inside that method, so the filter&#39;s own body threw the moment it tried to read \u003Ccode>tenantId\u003C\u002Fcode> — no Sentry call went out, because the code never got that far, but it also wasn&#39;t a clean, silent handoff to Nest&#39;s default handler the way it might look from the outside. (\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18104\">nestjs\u002Fnest PR #18104\u003C\u002Fa> describes the identical mechanism for a \u003Ccode>useClass\u003C\u002Fcode> global guard, whose \u003Ccode>canActivate\u003C\u002Fcode> would &quot;run with its dependency undefined, causing a 500 on every request.&quot;)\u003C\u002Fp>",{"id":158,"html":159,"text":159,"type":94,"level":31},"b18","The mental model: scope bubbles, enhancers inherit it",{"id":161,"html":162,"type":83},"b19","\u003Cp>\u003Cstrong>The mental model:\u003C\u002Fstrong> a guard, interceptor, pipe, or filter is a provider like any other, which means it follows the exact same scope-propagation rule every other provider does: if its constructor injects a \u003Ccode>REQUEST\u003C\u002Fcode>-scoped (or \u003Ccode>TRANSIENT\u003C\u002Fcode>-scoped) dependency, the enhancer itself inherits that scope — even though you only wrote \u003Ccode>scope:\u003C\u002Fcode> on the dependency, never on the enhancer. Scope bubbles up through constructor injection; it doesn&#39;t stay contained to the provider you explicitly marked.\u003C\u002Fp>",{"id":164,"html":165,"type":83},"b20","\u003Cp>That rule is reasonable and documented for ordinary services. What it does \u003Cem>not\u003C\u002Fem> come with, anywhere in NestJS&#39;s own docs, is a warning that applying it to a \u003Cstrong>global\u003C\u002Fstrong> enhancer changes what &quot;global&quot; means. \u003Ccode>APP_FILTER\u003C\u002Fcode>&#39;s own documentation says it makes a filter global &quot;regardless of the module&quot; — true, but silent on scope. The two ideas collide exactly where this bug lives: a provider-level rule (scope bubbling) meets a global-registration guarantee (APP_FILTER), and the guarantee quietly loses.\u003C\u002Fp>",{"id":167,"html":168,"type":83},"b21","\u003Cp>\u003Cstrong>Key concept:\u003C\u002Fstrong> &quot;global&quot; and &quot;singleton&quot; are two different promises. \u003Ccode>APP_FILTER\u003C\u002Fcode> promises your filter is wired into every route. Scope decides whether Nest can actually keep one instance of it alive for the whole app to share — and if it can&#39;t, the global promise is the one that breaks, not the scope.\u003C\u002Fp>",{"id":170,"html":171,"type":83},"b22","\u003C!-- playground:start -->",{"id":173,"html":174,"text":174,"type":94,"level":31},"b23","🎮 Try it yourself",{"id":176,"html":177,"type":83},"b24","\u003Cp>\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-app-filter-scope\u002Fplayground\">▶️ Open the interactive playground →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":179,"html":180,"type":83},"b25","\u003Cp>\u003Cem>Runs right in your browser — poke at it and watch the concept react live.\u003C\u002Fem>\u003C\u002Fp>",{"id":182,"html":183,"type":83},"b26","\u003C!-- playground:end -->",{"id":185,"html":186,"text":186,"type":94,"level":31},"b27","Stage 1: two ways to register a filter, one real difference",{"id":188,"html":189,"type":83},"b28","\u003Cp>NestJS gives you two ways to make a filter apply everywhere, and they are not interchangeable:\u003C\u002Fp>",{"id":191,"code":192,"type":140,"language":141,"highlight":193},"b29","\u002F\u002F main.ts — instance-based, works but can't use DI\nconst app = await NestFactory.create(AppModule);\napp.useGlobalFilters(new AllExceptionsFilter());\nawait app.listen(3000);",[],{"id":195,"code":196,"type":140,"language":141,"highlight":197},"b30","\u002F\u002F app.module.ts — provider-based, can use DI\n@Module({\n  providers: [{ provide: APP_FILTER, useClass: AllExceptionsFilter }],\n})\nexport class AppModule {}",[],{"id":199,"html":200,"type":83},"b31","\u003Cp>\u003Ccode>app.useGlobalFilters()\u003C\u002Fcode> instantiates the filter yourself, outside of any module — which means it cannot inject anything, because injection only happens inside Nest&#39;s module context. \u003Ccode>APP_FILTER\u003C\u002Fcode> solves exactly that: register the filter as a provider inside any module, and Nest constructs it through the DI container like anything else, dependencies included. That&#39;s the whole reason \u003Ccode>APP_FILTER\u003C\u002Fcode> exists. It&#39;s also exactly the door this bug walks through — DI means scope, and scope is where things went wrong above.\u003C\u002Fp>",{"id":202,"html":203,"text":203,"type":94,"level":31},"b32","Stage 2: how a filter accidentally stops being a singleton",{"id":205,"html":206,"type":83},"b33","\u003Cp>The dangerous part of this bug isn&#39;t that someone deliberately wrote \u003Ccode>@Injectable({ scope: Scope.REQUEST })\u003C\u002Fcode> on a global filter — that would at least be a visible decision to review. It&#39;s that scope arrives as a side effect of an unrelated choice three steps away:\u003C\u002Fp>",{"id":208,"type":100,"items":209,"ordered":17},"b34",[210,211,212,213],"You need the current user, tenant, or request ID inside your filter.","The idiomatic way to get it is a small \u003Ccode>REQUEST\u003C\u002Fcode>-scoped service that wraps \u003Ccode>@Inject(REQUEST)\u003C\u002Fcode>.","You inject that service into your filter&#39;s constructor.","Nest&#39;s scope-bubbling rule — the same one that makes a controller request-scoped when it depends on a request-scoped service — makes your filter request-scoped too, silently.",{"id":215,"html":216,"type":83},"b35","\u003Cp>Nobody on the team ever typed \u003Ccode>scope:\u003C\u002Fcode> on \u003Ccode>AllExceptionsFilter\u003C\u002Fcode>. The filter&#39;s own file never changed. The regression shipped the day \u003Ccode>TenantContextService\u003C\u002Fcode> was added as a dependency — which, if no commit touched \u003Ccode>all-exceptions.filter.ts\u003C\u002Fcode>, made it look like the opposite of the filter&#39;s job was the cause.\u003C\u002Fp>",{"id":218,"html":219,"text":219,"type":94,"level":31},"b36","Stage 3: what NestJS 12.1.3 changed",{"id":221,"html":222,"type":83},"b37","\u003Cp>The \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Freleases\u002Ftag\u002Fv12.1.3\">12.1.3 release\u003C\u002Fa> shipped two separate fixes for this, back to back:\u003C\u002Fp>",{"id":224,"type":100,"items":225,"ordered":18},"b38",[226,227],"\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18066\">&quot;fix(core): stop silently dropping request-scoped global enhancers&quot;\u003C\u002Fa>\u003C\u002Fstrong> — fixed the \u003Cem>other\u003C\u002Fem> half of this family of bugs: a scoped global enhancer registered via \u003Ccode>useExisting\u003C\u002Fcode> or \u003Ccode>useFactory\u003C\u002Fcode> (not the \u003Ccode>useClass\u003C\u002Fcode> style this article uses) really was filtered out and skipped entirely, because Nest&#39;s old check looked for a \u003Ccode>null\u003C\u002Fcode> instance — something a class provider never has. After this fix, Nest tries to actually use the enhancer instead of discarding it.","\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18104\">&quot;fix(core): fail at startup for request-scoped class global enhancers&quot;\u003C\u002Fa>\u003C\u002Fstrong> — the fix for \u003Ccode>AllExceptionsFilter\u003C\u002Fcode>&#39;s actual registration style, \u003Ccode>useClass\u003C\u002Fcode>. Before this landed, a scoped class enhancer wasn&#39;t dropped at all: Nest built it as a broken, constructor-never-ran instance and called it anyway on real requests. After this, Nest refuses to boot the app instead — an explicit startup error, before a single request can be served.",{"id":229,"html":230,"type":83},"b39","\u003Cp>Read together, these two PRs replace &quot;silently does nothing&quot; with &quot;crashes loudly instead.&quot; That&#39;s a strict improvement for safety — a guard that silently vanishes could mean an unprotected route in production; an app that won&#39;t start at all gets caught in CI, by a human, before it ships. But it&#39;s also a blunt instrument: it treats every scoped global enhancer as an error, full stop, with no room for &quot;this one still mostly works.&quot;\u003C\u002Fp>",{"id":232,"html":233,"type":83},"b40","\u003Cp>A separate fix in the same release — \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F17993\">&quot;fix(core,microservices): run transient global enhancers on controllers&quot;\u003C\u002Fa>\u003C\u002Fstrong> — addressed the \u003Ccode>TRANSIENT\u003C\u002Fcode>-scope side of the same family of bugs, making transient global enhancers actually apply at the controller level instead of being skipped there too.\u003C\u002Fp>",{"id":235,"html":236,"text":236,"type":94,"level":31},"b41","Stage 4: what NestJS 12.2.0 changed for filters specifically",{"id":238,"html":239,"type":83},"b42","\u003Cp>Then \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Freleases\u002Ftag\u002Fv12.2.0\">12.2.0\u003C\u002Fa> carved out an exception for exception filters. Under its &quot;new defaults and behaviour changes,&quot; the release notes say a request- or transient-scoped \u003Ccode>APP_FILTER\u003C\u002Fcode> &quot;now logs a startup warning, as it only applies to exceptions thrown from controllers&quot; — a warning, not the startup failure that 12.1.3 introduced for other scoped global enhancers.\u003C\u002Fp>",{"id":241,"html":242,"type":83},"b43","\u003Cp>That&#39;s a meaningful change in posture. A scoped global guard that sometimes doesn&#39;t run is a security hole, not a guard — failing to boot is the right call there. A scoped global filter is different: it can still genuinely catch and format exceptions thrown inside a normal controller request, the overwhelming majority of what filters exist for. So current NestJS boots your app, logs one explicit warning naming the limitation, and lets the filter do the part of its job it still can.\u003C\u002Fp>",{"id":244,"html":245,"type":83},"b44","\u003Cp>The same release tightened one more related behavior: &quot;a global enhancer that the injector failed to instantiate now fails the application at startup&quot; — not filter-specific, but it closes a related class of silent-failure bugs at the DI level.\u003C\u002Fp>",{"id":247,"html":248,"text":249,"type":94,"level":31},"b45","Stage 5: the gap that&#39;s still there today","Stage 5: the gap that's still there today",{"id":251,"html":252,"type":83},"b46","\u003Cp>Here&#39;s the part worth sitting with: the 12.2.0 warning doesn&#39;t say your scoped filter is fine now. It says, in its own words, that it &quot;only applies to exceptions thrown from controllers.&quot; The docs are explicit that exceptions thrown from middleware bypass method- and controller-level \u003Ccode>@UseFilters()\u003C\u002Fcode> entirely and reach only a truly global filter, because middleware runs before any route (and its handler-level filters) has even been selected.\u003C\u002Fp>",{"id":254,"html":255,"type":83},"b47","\u003Cp>Put those two facts together: on current NestJS, a \u003Ccode>REQUEST\u003C\u002Fcode>- or \u003Ccode>TRANSIENT\u003C\u002Fcode>-scoped \u003Ccode>APP_FILTER\u003C\u002Fcode> still does not catch an exception thrown from middleware. It boots. It logs a warning. It catches everything a developer is most likely to test by hand — a thrown error inside a controller method. The one case it silently drops is the one almost nobody manually tests: an exception from a piece of middleware that runs before the controller is even picked. That&#39;s the honest shape of &quot;fixed&quot; here — loud and correct for the common path, still a quiet gap for the uncommon one, and now at least documented in a boot-time log line instead of nowhere at all.\u003C\u002Fp>",{"id":257,"html":258,"type":83},"b48","\u003C!-- quiz:start -->",{"id":260,"html":261,"text":261,"type":94,"level":31},"b49","🧠 Test yourself",{"id":263,"html":264,"type":83},"b50","\u003Cp>Think it clicked? \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-app-filter-scope\u002Fquiz\">Take the 8-question quiz →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":266,"html":267,"type":83},"b51","\u003Cp>\u003Cem>Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.\u003C\u002Fem>\u003C\u002Fp>",{"id":269,"html":270,"type":83},"b52","\u003C!-- quiz:end -->",{"id":272,"html":273,"text":273,"type":94,"level":31},"b53","Edge cases and gotchas",{"id":275,"type":100,"items":276,"ordered":18},"b54",[277,278,279,280,281],"\u003Cstrong>The \u003Ccode>REQUEST\u003C\u002Fcode> token itself forces scope, full stop.\u003C\u002Fstrong> Injecting \u003Ccode>REQUEST\u003C\u002Fcode> or \u003Ccode>CONTEXT\u003C\u002Fcode> (GraphQL) directly makes the injecting provider request-scoped regardless of any \u003Ccode>scope:\u003C\u002Fcode> you declare.","\u003Cstrong>\u003Ccode>TRANSIENT\u003C\u002Fcode> doesn&#39;t normally bubble, but global registration is the exception.\u003C\u002Fstrong> An ordinary provider that injects a \u003Ccode>TRANSIENT\u003C\u002Fcode> dependency gets its own private copy without becoming transient itself. Global enhancers go through separate instantiation logic, which is why they needed their own dedicated fix (PR #17993).","\u003Cstrong>A test suite usually can&#39;t catch this.\u003C\u002Fstrong> \u003Ccode>Test.createTestingModule({...}).compile()\u003C\u002Fcode> typically never exercises the startup path 12.1.3&#39;s &quot;fail fast&quot; check runs against, so a unit test can pass green while a real \u003Ccode>NestFactory.create()\u003C\u002Fcode> boot would refuse to start.","\u003Cstrong>\u003Ccode>app.useGlobalFilters()\u003C\u002Fcode> never has this problem, by construction.\u003C\u002Fstrong> No DI, so no scope to bubble — at the cost of never being able to inject anything either.","\u003Cstrong>Durable providers don&#39;t rescue this.\u003C\u002Fstrong> \u003Ccode>durable: true\u003C\u002Fcode> changes how a \u003Ccode>REQUEST\u003C\u002Fcode>-scoped provider is reused; it doesn&#39;t turn it back into a true singleton.",{"id":283,"html":284,"text":284,"type":94,"level":31},"b55","Best practices",{"id":286,"type":100,"items":287,"ordered":18},"b56",[288,289,290,291,292],"\u003Cstrong>Read the current user\u002Ftenant\u002Frequest ID without scoping your filter at all.\u003C\u002Fstrong> Reach for \u003Ccode>AsyncLocalStorage\u003C\u002Fcode>, populated once in a small piece of middleware, instead of injecting a \u003Ccode>REQUEST\u003C\u002Fcode>-scoped service into a global filter. The filter stays a true singleton and still gets per-request context.","\u003Cstrong>If a global enhancer truly must be request-scoped, know what you&#39;re giving up.\u003C\u002Fstrong> For a filter, that&#39;s middleware-thrown exceptions. For a guard, interceptor, or pipe, current NestJS won&#39;t let the app start at all.","\u003Cstrong>Never assume a scoped global enhancer &quot;mostly works&quot; without checking the version.\u003C\u002Fstrong> Silently broken (undefined dependencies at runtime), startup failure, or startup warning with reduced coverage — it depends on which NestJS version you&#39;re running, and it changed twice in the 12.x line alone.","\u003Cstrong>Treat the 12.2.0 startup warning as a real signal, not log noise.\u003C\u002Fstrong> It names the exact gap above, not a vague best-practice nudge.","\u003Cstrong>Re-read a point release&#39;s notes before assuming &quot;patch, no risk.&quot;\u003C\u002Fstrong> 12.1.3 and 12.2.0 are both patch versions under semver, and both changed real runtime behavior here.",{"id":294,"html":295,"text":295,"type":94,"level":31},"b57","FAQ",{"id":297,"html":298,"text":299,"type":94,"level":43},"b58","Does \u003Ccode>app.useGlobalFilters()\u003C\u002Fcode> have the same scope problem as \u003Ccode>APP_FILTER\u003C\u002Fcode>?","Does app.useGlobalFilters() have the same scope problem as APPFILTER?",{"id":301,"html":302,"type":83},"b59","\u003Cp>No. \u003Ccode>app.useGlobalFilters()\u003C\u002Fcode> instantiates the filter yourself, outside any module&#39;s DI context, so it can&#39;t inject dependencies at all — and with no injection, there&#39;s nothing to bubble scope from. The tradeoff is that you lose dependency injection entirely, which is the exact capability \u003Ccode>APP_FILTER\u003C\u002Fcode> exists to provide.\u003C\u002Fp>",{"id":304,"html":305,"text":306,"type":94,"level":43},"b60","Will a scoped \u003Ccode>APP_GUARD\u003C\u002Fcode> or \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode> ever silently do nothing on current NestJS?","Will a scoped APPGUARD or APPINTERCEPTOR ever silently do nothing on current NestJS?",{"id":308,"html":309,"type":83},"b61","\u003Cp>Not on 12.1.3 or later. The startup-failure fix in that release applies to request-scoped class global enhancers generally — guards, interceptors, and pipes included — so the broken-instance case for those is closed; you get a boot-time error instead of a provider with an undefined dependency.\u003C\u002Fp>",{"id":311,"html":312,"text":312,"type":94,"level":43},"b62","Why did filters get a warning instead of a startup failure?",{"id":314,"html":315,"type":83},"b63","\u003Cp>NestJS&#39;s own 12.2.0 release notes don&#39;t spell out the reasoning, only the behavior: a scoped \u003Ccode>APP_FILTER\u003C\u002Fcode> &quot;logs a startup warning, as it only applies to exceptions thrown from controllers.&quot; In practice, a scope-limited filter can still correctly handle the overwhelming majority of exceptions — anything thrown during normal request handling — while a scope-limited guard protecting nothing would be a straightforward security gap. Treat the warning as &quot;mostly still correct, with one named gap,&quot; not as &quot;fine.&quot;\u003C\u002Fp>",{"id":317,"html":318,"text":318,"type":94,"level":43},"b64","Is this different between Express and Fastify?",{"id":320,"html":321,"type":83},"b65","\u003Cp>No. Scope resolution and global-enhancer instantiation happen in Nest&#39;s core container, before either \u003Ccode>HttpAdapter\u003C\u002Fcode> is involved, so the behavior described here is identical on both.\u003C\u002Fp>",{"id":323,"html":324,"text":324,"type":94,"level":43},"b66","How do I check which NestJS patch version actually fixed this in my app?",{"id":326,"html":327,"type":83},"b67","\u003Cp>Compare your installed \u003Ccode>@nestjs\u002Fcore\u003C\u002Fcode> version against 12.1.3 and 12.2.0 — \u003Ccode>npm ls @nestjs\u002Fcore\u003C\u002Fcode> locally, or \u003Ccode>npm view @nestjs\u002Fcore dist-tags\u003C\u002Fcode> for what&#39;s current. Below 12.1.3, you have the broken-instance bug — the filter boots and runs, but its injected dependency is \u003Ccode>undefined\u003C\u002Fcode>. At 12.1.3 up to (not including) 12.2.0, a scoped class filter fails your app at boot. At 12.2.0 and later, it boots with a warning and the middleware-exception gap described in Stage 5.\u003C\u002Fp>",{"id":329,"html":330,"text":330,"type":94,"level":31},"b68","Cheat sheet",{"id":332,"head":333,"rows":338,"type":362},"b69",[334,335,336,337],"Situation","NestJS &lt; 12.1.3","NestJS 12.1.3","NestJS 12.2.0+ (current)",[339,344,348,352,355,359],[340,341,342,343],"Scoped (\u003Ccode>REQUEST\u003C\u002Fcode>\u002F\u003Ccode>TRANSIENT\u003C\u002Fcode>) class filter via \u003Ccode>APP_FILTER\u003C\u002Fcode>","Boots with no warning, but built as a broken constructor-never-ran instance — throws when it touches its own missing dependency","App fails to start","Boots, logs a startup warning, catches controller exceptions only",[345,346,342,347],"Scoped class guard\u002Finterceptor\u002Fpipe, globally registered","Boots with no warning, same broken-instance problem — undefined dependencies at runtime","App fails to start (unchanged from 12.1.3)",[349,350,351,351],"Singleton (default-scope) global filter via \u003Ccode>APP_FILTER\u003C\u002Fcode>","Fully global: catches middleware + controller exceptions","Same",[353,354,351,351],"\u003Ccode>app.useGlobalFilters()\u003C\u002Fcode> filter","Always global, never scoped (can&#39;t inject deps)",[356,357,357,358],"Read current user\u002Ftenant in a global filter safely","—","\u003Ccode>AsyncLocalStorage\u003C\u002Fcode>, not an injected \u003Ccode>REQUEST\u003C\u002Fcode>-scoped provider",[360,357,357,361],"Check your exposure","\u003Ccode>npm view @nestjs\u002Fcore dist-tags\u003C\u002Fcode> and compare against 12.1.3 \u002F 12.2.0","table",{"id":364,"html":365,"text":365,"type":94,"level":31},"b70","Key takeaways",{"id":367,"type":100,"items":368,"ordered":18},"b71",[369,370,371,372],"Scope bubbles through constructor injection, and that rule doesn&#39;t stop at the edge of a global enhancer — a filter, guard, interceptor, or pipe inherits \u003Ccode>REQUEST\u003C\u002Fcode>\u002F\u003Ccode>TRANSIENT\u003C\u002Fcode> scope from any dependency it injects, even if no one wrote \u003Ccode>scope:\u003C\u002Fcode> on the enhancer itself.","Before NestJS 12.1.3, that combination wasn&#39;t dropped at startup — it booted silently, then broke the first time it ran: no error, no warning, just a constructor that never executed and a dependency that was \u003Ccode>undefined\u003C\u002Fcode> when the filter tried to use it.","12.1.3 replaced the silent drop with a startup failure for scoped class global enhancers. 12.2.0 then gave exception filters specifically a softer path: boot with a warning, keep working for controller exceptions, and openly admit the one thing it still can&#39;t do — catch exceptions thrown from middleware.","The fix that avoids all three outcomes is the same fix in every case: keep the global enhancer a singleton, and reach for \u003Ccode>AsyncLocalStorage\u003C\u002Fcode> when it needs per-request context.",{"id":374,"html":375,"type":83},"b72","\u003Cp>The team in this article&#39;s opening didn&#39;t lose Sentry coverage the day they upgraded NestJS, and they didn&#39;t lose it the day they wrote the filter either — they lost it the day \u003Ccode>TenantContextService\u003C\u002Fcode> became one of its dependencies, three steps removed from anything that looked like a scope decision. Once &quot;global&quot; and &quot;singleton&quot; are two separate promises in your head, that bug stops being something you discover in production and becomes something you can rule out by reading a constructor.\u003C\u002Fp>",{"id":377,"html":378,"type":83},"b73","\u003Cp>Has a dependency you added somewhere else ever quietly changed the scope of something you thought was a fixed singleton? Drop it in the comments.\u003C\u002Fp>",{"id":380,"html":381,"type":83},"b74","\u003C!-- related:start -->",{"id":383,"html":384,"text":384,"type":94,"level":31},"b75","📚 Read next",{"id":386,"type":100,"items":387,"ordered":18},"b76",[388,389,390],"\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-standard-schema-validation\">NestJS 12: Standard Schema Validation Without class-validator\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-lifecycle-hook-order\">NestJS 12 Lifecycle Hooks: The Order You Can&#39;t Assume\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-validation-error-target\">NestJS 12.2: Why validationError.target Is Now undefined\u003C\u002Fa>",{"id":392,"html":393,"type":83},"b77","\u003C!-- related:end -->",{"id":395,"type":396},"b78","divider",{"id":398,"html":399,"type":83},"b79","\u003Cp>🚀 \u003Cstrong>Want more like this?\u003C\u002Fstrong> Every guide, playground, and quiz lives on \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">bestpractic.org\u003C\u002Fa>\u003C\u002Fstrong> — open it and \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">sign up free\u003C\u002Fa>\u003C\u002Fstrong> so the next one finds you.\u003C\u002Fp>",{"id":401,"html":402,"type":83},"b80","\u003Cp>\u003Cem>Thanks for reading! Let&#39;s stay connected:\u003C\u002Fem>\u003C\u002Fp>",{"id":404,"type":100,"items":405,"ordered":18},"b81",[406,407,408],"⭐ \u003Cstrong>GitHub\u003C\u002Fstrong> — follow me and star the projects: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fparsajiravand\">github.com\u002Fparsajiravand\u003C\u002Fa>","💬 \u003Cstrong>Discord\u003C\u002Fstrong> — join the frontend best-practices community: \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ\">discord.gg\u002Fd9KRhuAwQ\u003C\u002Fa>","📸 \u003Cstrong>Instagram\u003C\u002Fstrong> — frontend best practices, daily: \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F\">@bestpractice___\u003C\u002Fa>","A team registers a global exception filter with `APP_FILTER` so every unhandled error gets the same JSON shape and the same Sentry breadcrumb. It works in every test, every staging deploy, every manual check. Then, weeks later, someone notices a slice of production 500s never reached Sentry at all — same error types, same routes, just silently missing. Nobody touched the filter. Nobody touched the module it lives in.\n\nThe filter was never a reliable singleton at all. The moment its constructor started injecting a request-scoped provider — to log the current tenant, say — Nest quietly turned the *filter itself* request-scoped too. And for every NestJS release before 12.1.3, a `REQUEST`- or `TRANSIENT`-scoped class registered as a global enhancer this way wasn't rejected, wasn't logged, wasn't flagged at boot — Nest built it anyway, as an empty object whose constructor had never run, and handed it real requests to handle. [The dependency it needed just wasn't there](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18104). This article is about that gap, the fix NestJS 12 actually shipped for it (in two separate releases, for two different reasons), and the real-but-narrower hole in exception-filter coverage that remains today.\n\nThis is written against **NestJS 12.5.0** (verified against `@nestjs\u002Fcore`'s npm dist-tags; the 11.x line is on the `legacy` npm tag at `11.2.10`). The specific fixes this article walks through landed in **12.1.3** and **12.2.0** — both are included in 12.5.0 — and everything described is core-container behavior, so it is identical whether your `HttpAdapter` is Express or Fastify.\n\n## What you'll learn\n\nBy the end of this article you'll be able to:\n\n- Explain exactly how a provider's injection scope can propagate into a global guard, interceptor, pipe, or filter without you ever writing `scope:` on it\n- Name the real difference between `app.useGlobalFilters()` and the `APP_FILTER` provider token, and why only one of them can inject dependencies at all\n- State precisely what changed in NestJS 12.1.3 and 12.2.0, and why filters now get different treatment than guards and interceptors\n- Recognize the one combination that still silently loses coverage today, even on a current NestJS install\n- Design a global exception filter that stays reliably global, on purpose\n\n## Who this is for\n\nYou've written a custom exception filter and registered it with `APP_FILTER` from `@nestjs\u002Fcore`. You don't need prior experience with Nest's injection scopes — this article builds that model from the ground up.\n\n## Table of contents\n\n- [The problem: a global filter that wasn't](#the-problem-a-global-filter-that-wasnt)\n- [The mental model: scope bubbles, enhancers inherit it](#the-mental-model-scope-bubbles-enhancers-inherit-it)\n- [Stage 1: two ways to register a filter, one real difference](#stage-1-two-ways-to-register-a-filter-one-real-difference)\n- [Stage 2: how a filter accidentally stops being a singleton](#stage-2-how-a-filter-accidentally-stops-being-a-singleton)\n- [Stage 3: what NestJS 12.1.3 changed](#stage-3-what-nestjs-1213-changed)\n- [Stage 4: what NestJS 12.2.0 changed for filters specifically](#stage-4-what-nestjs-1220-changed-for-filters-specifically)\n- [Stage 5: the gap that's still there today](#stage-5-the-gap-thats-still-there-today)\n- [Edge cases and gotchas](#edge-cases-and-gotchas)\n- [Best practices](#best-practices)\n- [FAQ](#faq)\n- [Cheat sheet](#cheat-sheet)\n\n## The problem: a global filter that wasn't\n\nHere's the filter, trimmed to the part that matters:\n\n```ts\n\u002F\u002F all-exceptions.filter.ts\n@Catch()\n@Injectable()\nexport class AllExceptionsFilter implements ExceptionFilter {\n  constructor(private readonly tenantContext: TenantContextService) {}\n\n  catch(exception: unknown, host: ArgumentsHost) {\n    const ctx = host.switchToHttp();\n    const response = ctx.getResponse();\n    \u002F\u002F tenantContext reads the current request's tenant for the error report\n    reportToSentry(exception, this.tenantContext.tenantId);\n    response.status(500).json({ message: 'Internal server error' });\n  }\n}\n```\n\n```ts\n\u002F\u002F tenant-context.service.ts\n@Injectable({ scope: Scope.REQUEST })\nexport class TenantContextService {\n  constructor(@Inject(REQUEST) private readonly request: Request) {}\n  get tenantId() {\n    return this.request.headers['x-tenant-id'];\n  }\n}\n```\n\n```ts\n\u002F\u002F app.module.ts\n@Module({\n  providers: [\n    TenantContextService,\n    { provide: APP_FILTER, useClass: AllExceptionsFilter },\n  ],\n})\nexport class AppModule {}\n```\n\nNothing here looks wrong. `AllExceptionsFilter` is registered with `APP_FILTER`, which the docs describe as making a filter global \"regardless of the module in which this construction is employed.\" It never calls `scope: Scope.REQUEST` on itself — only `TenantContextService` does that, so it can read the inbound request. But `TenantContextService` is `REQUEST`-scoped, and injecting a `REQUEST`-scoped provider into another provider's constructor makes *that* provider `REQUEST`-scoped too. That's not a bug — it's how Nest's injection scope has always worked. It just means `AllExceptionsFilter` is `REQUEST`-scoped now, whether or not anyone intended it.\n\nOn every NestJS release before 12.1.3, that combination — a `REQUEST`- or `TRANSIENT`-scoped class registered as a global enhancer via `useClass` — booted cleanly, with no startup error and no warning. But it wasn't a quiet skip: Nest still constructed `AllExceptionsFilter`, as a bare instance whose constructor body never ran, and still called `catch()` on that instance for every matching exception. `this.tenantContext` was `undefined` inside that method, so the filter's own body threw the moment it tried to read `tenantId` — no Sentry call went out, because the code never got that far, but it also wasn't a clean, silent handoff to Nest's default handler the way it might look from the outside. ([nestjs\u002Fnest PR #18104](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18104) describes the identical mechanism for a `useClass` global guard, whose `canActivate` would \"run with its dependency undefined, causing a 500 on every request.\")\n\n## The mental model: scope bubbles, enhancers inherit it\n\n**The mental model:** a guard, interceptor, pipe, or filter is a provider like any other, which means it follows the exact same scope-propagation rule every other provider does: if its constructor injects a `REQUEST`-scoped (or `TRANSIENT`-scoped) dependency, the enhancer itself inherits that scope — even though you only wrote `scope:` on the dependency, never on the enhancer. Scope bubbles up through constructor injection; it doesn't stay contained to the provider you explicitly marked.\n\nThat rule is reasonable and documented for ordinary services. What it does *not* come with, anywhere in NestJS's own docs, is a warning that applying it to a **global** enhancer changes what \"global\" means. `APP_FILTER`'s own documentation says it makes a filter global \"regardless of the module\" — true, but silent on scope. The two ideas collide exactly where this bug lives: a provider-level rule (scope bubbling) meets a global-registration guarantee (APP_FILTER), and the guarantee quietly loses.\n\n**Key concept:** \"global\" and \"singleton\" are two different promises. `APP_FILTER` promises your filter is wired into every route. Scope decides whether Nest can actually keep one instance of it alive for the whole app to share — and if it can't, the global promise is the one that breaks, not the scope.\n\n\u003C!-- playground:start -->\n\n## 🎮 Try it yourself\n\n**[▶️ Open the interactive playground →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-app-filter-scope\u002Fplayground)**\n\n_Runs right in your browser — poke at it and watch the concept react live._\n\n\u003C!-- playground:end -->\n\n## Stage 1: two ways to register a filter, one real difference\n\nNestJS gives you two ways to make a filter apply everywhere, and they are not interchangeable:\n\n```ts\n\u002F\u002F main.ts — instance-based, works but can't use DI\nconst app = await NestFactory.create(AppModule);\napp.useGlobalFilters(new AllExceptionsFilter());\nawait app.listen(3000);\n```\n\n```ts\n\u002F\u002F app.module.ts — provider-based, can use DI\n@Module({\n  providers: [{ provide: APP_FILTER, useClass: AllExceptionsFilter }],\n})\nexport class AppModule {}\n```\n\n`app.useGlobalFilters()` instantiates the filter yourself, outside of any module — which means it cannot inject anything, because injection only happens inside Nest's module context. `APP_FILTER` solves exactly that: register the filter as a provider inside any module, and Nest constructs it through the DI container like anything else, dependencies included. That's the whole reason `APP_FILTER` exists. It's also exactly the door this bug walks through — DI means scope, and scope is where things went wrong above.\n\n## Stage 2: how a filter accidentally stops being a singleton\n\nThe dangerous part of this bug isn't that someone deliberately wrote `@Injectable({ scope: Scope.REQUEST })` on a global filter — that would at least be a visible decision to review. It's that scope arrives as a side effect of an unrelated choice three steps away:\n\n1. You need the current user, tenant, or request ID inside your filter.\n2. The idiomatic way to get it is a small `REQUEST`-scoped service that wraps `@Inject(REQUEST)`.\n3. You inject that service into your filter's constructor.\n4. Nest's scope-bubbling rule — the same one that makes a controller request-scoped when it depends on a request-scoped service — makes your filter request-scoped too, silently.\n\nNobody on the team ever typed `scope:` on `AllExceptionsFilter`. The filter's own file never changed. The regression shipped the day `TenantContextService` was added as a dependency — which, if no commit touched `all-exceptions.filter.ts`, made it look like the opposite of the filter's job was the cause.\n\n## Stage 3: what NestJS 12.1.3 changed\n\nThe [12.1.3 release](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Freleases\u002Ftag\u002Fv12.1.3) shipped two separate fixes for this, back to back:\n\n- **[\"fix(core): stop silently dropping request-scoped global enhancers\"](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18066)** — fixed the *other* half of this family of bugs: a scoped global enhancer registered via `useExisting` or `useFactory` (not the `useClass` style this article uses) really was filtered out and skipped entirely, because Nest's old check looked for a `null` instance — something a class provider never has. After this fix, Nest tries to actually use the enhancer instead of discarding it.\n- **[\"fix(core): fail at startup for request-scoped class global enhancers\"](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F18104)** — the fix for `AllExceptionsFilter`'s actual registration style, `useClass`. Before this landed, a scoped class enhancer wasn't dropped at all: Nest built it as a broken, constructor-never-ran instance and called it anyway on real requests. After this, Nest refuses to boot the app instead — an explicit startup error, before a single request can be served.\n\nRead together, these two PRs replace \"silently does nothing\" with \"crashes loudly instead.\" That's a strict improvement for safety — a guard that silently vanishes could mean an unprotected route in production; an app that won't start at all gets caught in CI, by a human, before it ships. But it's also a blunt instrument: it treats every scoped global enhancer as an error, full stop, with no room for \"this one still mostly works.\"\n\nA separate fix in the same release — **[\"fix(core,microservices): run transient global enhancers on controllers\"](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Fpull\u002F17993)** — addressed the `TRANSIENT`-scope side of the same family of bugs, making transient global enhancers actually apply at the controller level instead of being skipped there too.\n\n## Stage 4: what NestJS 12.2.0 changed for filters specifically\n\nThen [12.2.0](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Freleases\u002Ftag\u002Fv12.2.0) carved out an exception for exception filters. Under its \"new defaults and behaviour changes,\" the release notes say a request- or transient-scoped `APP_FILTER` \"now logs a startup warning, as it only applies to exceptions thrown from controllers\" — a warning, not the startup failure that 12.1.3 introduced for other scoped global enhancers.\n\nThat's a meaningful change in posture. A scoped global guard that sometimes doesn't run is a security hole, not a guard — failing to boot is the right call there. A scoped global filter is different: it can still genuinely catch and format exceptions thrown inside a normal controller request, the overwhelming majority of what filters exist for. So current NestJS boots your app, logs one explicit warning naming the limitation, and lets the filter do the part of its job it still can.\n\nThe same release tightened one more related behavior: \"a global enhancer that the injector failed to instantiate now fails the application at startup\" — not filter-specific, but it closes a related class of silent-failure bugs at the DI level.\n\n## Stage 5: the gap that's still there today\n\nHere's the part worth sitting with: the 12.2.0 warning doesn't say your scoped filter is fine now. It says, in its own words, that it \"only applies to exceptions thrown from controllers.\" The docs are explicit that exceptions thrown from middleware bypass method- and controller-level `@UseFilters()` entirely and reach only a truly global filter, because middleware runs before any route (and its handler-level filters) has even been selected.\n\nPut those two facts together: on current NestJS, a `REQUEST`- or `TRANSIENT`-scoped `APP_FILTER` still does not catch an exception thrown from middleware. It boots. It logs a warning. It catches everything a developer is most likely to test by hand — a thrown error inside a controller method. The one case it silently drops is the one almost nobody manually tests: an exception from a piece of middleware that runs before the controller is even picked. That's the honest shape of \"fixed\" here — loud and correct for the common path, still a quiet gap for the uncommon one, and now at least documented in a boot-time log line instead of nowhere at all.\n\n\u003C!-- quiz:start -->\n\n## 🧠 Test yourself\n\nThink it clicked? **[Take the 8-question quiz →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-app-filter-scope\u002Fquiz)**\n\n_Instant feedback, a hint on every question, and an explanation for each answer — right or wrong._\n\n\u003C!-- quiz:end -->\n\n## Edge cases and gotchas\n\n- **The `REQUEST` token itself forces scope, full stop.** Injecting `REQUEST` or `CONTEXT` (GraphQL) directly makes the injecting provider request-scoped regardless of any `scope:` you declare.\n- **`TRANSIENT` doesn't normally bubble, but global registration is the exception.** An ordinary provider that injects a `TRANSIENT` dependency gets its own private copy without becoming transient itself. Global enhancers go through separate instantiation logic, which is why they needed their own dedicated fix (PR #17993).\n- **A test suite usually can't catch this.** `Test.createTestingModule({...}).compile()` typically never exercises the startup path 12.1.3's \"fail fast\" check runs against, so a unit test can pass green while a real `NestFactory.create()` boot would refuse to start.\n- **`app.useGlobalFilters()` never has this problem, by construction.** No DI, so no scope to bubble — at the cost of never being able to inject anything either.\n- **Durable providers don't rescue this.** `durable: true` changes how a `REQUEST`-scoped provider is reused; it doesn't turn it back into a true singleton.\n\n## Best practices\n\n- **Read the current user\u002Ftenant\u002Frequest ID without scoping your filter at all.** Reach for `AsyncLocalStorage`, populated once in a small piece of middleware, instead of injecting a `REQUEST`-scoped service into a global filter. The filter stays a true singleton and still gets per-request context.\n- **If a global enhancer truly must be request-scoped, know what you're giving up.** For a filter, that's middleware-thrown exceptions. For a guard, interceptor, or pipe, current NestJS won't let the app start at all.\n- **Never assume a scoped global enhancer \"mostly works\" without checking the version.** Silently broken (undefined dependencies at runtime), startup failure, or startup warning with reduced coverage — it depends on which NestJS version you're running, and it changed twice in the 12.x line alone.\n- **Treat the 12.2.0 startup warning as a real signal, not log noise.** It names the exact gap above, not a vague best-practice nudge.\n- **Re-read a point release's notes before assuming \"patch, no risk.\"** 12.1.3 and 12.2.0 are both patch versions under semver, and both changed real runtime behavior here.\n\n## FAQ\n\n### Does `app.useGlobalFilters()` have the same scope problem as `APP_FILTER`?\n\nNo. `app.useGlobalFilters()` instantiates the filter yourself, outside any module's DI context, so it can't inject dependencies at all — and with no injection, there's nothing to bubble scope from. The tradeoff is that you lose dependency injection entirely, which is the exact capability `APP_FILTER` exists to provide.\n\n### Will a scoped `APP_GUARD` or `APP_INTERCEPTOR` ever silently do nothing on current NestJS?\n\nNot on 12.1.3 or later. The startup-failure fix in that release applies to request-scoped class global enhancers generally — guards, interceptors, and pipes included — so the broken-instance case for those is closed; you get a boot-time error instead of a provider with an undefined dependency.\n\n### Why did filters get a warning instead of a startup failure?\n\nNestJS's own 12.2.0 release notes don't spell out the reasoning, only the behavior: a scoped `APP_FILTER` \"logs a startup warning, as it only applies to exceptions thrown from controllers.\" In practice, a scope-limited filter can still correctly handle the overwhelming majority of exceptions — anything thrown during normal request handling — while a scope-limited guard protecting nothing would be a straightforward security gap. Treat the warning as \"mostly still correct, with one named gap,\" not as \"fine.\"\n\n### Is this different between Express and Fastify?\n\nNo. Scope resolution and global-enhancer instantiation happen in Nest's core container, before either `HttpAdapter` is involved, so the behavior described here is identical on both.\n\n### How do I check which NestJS patch version actually fixed this in my app?\n\nCompare your installed `@nestjs\u002Fcore` version against 12.1.3 and 12.2.0 — `npm ls @nestjs\u002Fcore` locally, or `npm view @nestjs\u002Fcore dist-tags` for what's current. Below 12.1.3, you have the broken-instance bug — the filter boots and runs, but its injected dependency is `undefined`. At 12.1.3 up to (not including) 12.2.0, a scoped class filter fails your app at boot. At 12.2.0 and later, it boots with a warning and the middleware-exception gap described in Stage 5.\n\n## Cheat sheet\n\n| Situation | NestJS \u003C 12.1.3 | NestJS 12.1.3 | NestJS 12.2.0+ (current) |\n| --- | --- | --- | --- |\n| Scoped (`REQUEST`\u002F`TRANSIENT`) class filter via `APP_FILTER` | Boots with no warning, but built as a broken constructor-never-ran instance — throws when it touches its own missing dependency | App fails to start | Boots, logs a startup warning, catches controller exceptions only |\n| Scoped class guard\u002Finterceptor\u002Fpipe, globally registered | Boots with no warning, same broken-instance problem — undefined dependencies at runtime | App fails to start | App fails to start (unchanged from 12.1.3) |\n| Singleton (default-scope) global filter via `APP_FILTER` | Fully global: catches middleware + controller exceptions | Same | Same |\n| `app.useGlobalFilters()` filter | Always global, never scoped (can't inject deps) | Same | Same |\n| Read current user\u002Ftenant in a global filter safely | — | — | `AsyncLocalStorage`, not an injected `REQUEST`-scoped provider |\n| Check your exposure | — | — | `npm view @nestjs\u002Fcore dist-tags` and compare against 12.1.3 \u002F 12.2.0 |\n\n## Key takeaways\n\n- Scope bubbles through constructor injection, and that rule doesn't stop at the edge of a global enhancer — a filter, guard, interceptor, or pipe inherits `REQUEST`\u002F`TRANSIENT` scope from any dependency it injects, even if no one wrote `scope:` on the enhancer itself.\n- Before NestJS 12.1.3, that combination wasn't dropped at startup — it booted silently, then broke the first time it ran: no error, no warning, just a constructor that never executed and a dependency that was `undefined` when the filter tried to use it.\n- 12.1.3 replaced the silent drop with a startup failure for scoped class global enhancers. 12.2.0 then gave exception filters specifically a softer path: boot with a warning, keep working for controller exceptions, and openly admit the one thing it still can't do — catch exceptions thrown from middleware.\n- The fix that avoids all three outcomes is the same fix in every case: keep the global enhancer a singleton, and reach for `AsyncLocalStorage` when it needs per-request context.\n\nThe team in this article's opening didn't lose Sentry coverage the day they upgraded NestJS, and they didn't lose it the day they wrote the filter either — they lost it the day `TenantContextService` became one of its dependencies, three steps removed from anything that looked like a scope decision. Once \"global\" and \"singleton\" are two separate promises in your head, that bug stops being something you discover in production and becomes something you can rule out by reading a constructor.\n\nHas a dependency you added somewhere else ever quietly changed the scope of something you thought was a fixed singleton? Drop it in the comments.\n\n\u003C!-- related:start -->\n\n## 📚 Read next\n\n- [NestJS 12: Standard Schema Validation Without class-validator](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-standard-schema-validation)\n- [NestJS 12 Lifecycle Hooks: The Order You Can't Assume](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-lifecycle-hook-order)\n- [NestJS 12.2: Why validationError.target Is Now undefined](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-validation-error-target)\n\n\u003C!-- related:end -->\n\n---\n\n🚀 **Want more like this?** Every guide, playground, and quiz lives on **[bestpractic.org](https:\u002F\u002Fbestpractic.org\u002F)** — open it and **[sign up free](https:\u002F\u002Fbestpractic.org\u002F)** so the next one finds you.\n\n*Thanks for reading! Let's stay connected:*\n\n- ⭐ **GitHub** — follow me and star the projects: [github.com\u002Fparsajiravand](https:\u002F\u002Fgithub.com\u002Fparsajiravand)\n- 💬 **Discord** — join the frontend best-practices community: [discord.gg\u002Fd9KRhuAwQ](https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ)\n- 📸 **Instagram** — frontend best practices, daily: [@bestpractice___](https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F)",{"title":47,"canonical":411,"description":49},"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-app-filter-scope","01a18121-99fa-77f0-83de-6177016be135",{"name":414,"part":415,"total":415,"items":416},"NestJS Deep Dive",10,[417,422,427,431,435,439,443,447,451,456],{"slug":418,"title":419,"publishedAt":420,"readingMinutes":421},"nestjs-weekly-request-lifecycle","NestJS Request Lifecycle Explained (with Cheat Sheet)","2026-08-28T17:01:41.941Z",13,{"slug":423,"title":424,"publishedAt":425,"readingMinutes":426},"nestjs-weekly-dependency-injection-providers-scopes","NestJS Dependency Injection Explained (with Cheat Sheet)","2026-09-13T08:40:18.434Z",14,{"slug":428,"title":429,"publishedAt":430,"readingMinutes":51},"nestjs-weekly-module-encapsulation-exports","NestJS Module Encapsulation Explained (with Cheat Sheet)","2026-09-13T08:40:50.070Z",{"slug":432,"title":433,"publishedAt":434,"readingMinutes":421},"nestjs-weekly-testing-provider-overrides","NestJS Testing Module: Provider Overrides (with Cheat Sheet)","2026-09-18T17:23:03.842Z",{"slug":436,"title":437,"publishedAt":438,"readingMinutes":426},"nestjs-weekly-guards-canactivate-reflector","NestJS Guards: CanActivate, ExecutionContext & Reflector","2026-09-26T12:25:39.001Z",{"slug":440,"title":441,"publishedAt":442,"readingMinutes":426},"nestjs-weekly-standard-schema-validation","NestJS 12: Standard Schema Validation Without class-validator","2026-10-02T06:11:02.679Z",{"slug":444,"title":445,"publishedAt":446,"readingMinutes":421},"nestjs-weekly-lifecycle-hook-order","NestJS 12 Lifecycle Hooks: The Order You Can't Assume","2026-10-23T06:10:32.276Z",{"slug":448,"title":449,"publishedAt":450,"readingMinutes":51},"nestjs-weekly-optional-not-inherited","NestJS 12: @Optional() Not Inherited by a Redeclared Constructor","2026-10-30T05:36:46.066Z",{"slug":452,"title":453,"publishedAt":454,"readingMinutes":455},"nestjs-weekly-validation-error-target","NestJS 12.2: Why validationError.target Is Now undefined","2026-11-06T06:10:24.442Z",12,{"slug":46,"title":47,"publishedAt":52,"readingMinutes":51},{"id":458,"locked":18},"01a18121-9a6b-77de-978f-4cb78e7db803",[460],{"id":461,"slug":46,"title":462,"_count":463},"01a18121-9a8c-77fb-a763-a65e31fa44ab","NestJS APP_FILTER Scope: Test Yourself",{"questions":464},8,[466],{"locale":13,"slug":46},{"id":461,"slug":46,"title":462,"_count":468,"questionCount":464},{"questions":464},[470,474,478,482,484,488,492,493,497,500,504,506],{"slug":471,"name":472,"articles":473},"webdev","Webdev",178,{"slug":475,"name":476,"articles":477},"javascript","Javascript",142,{"slug":479,"name":480,"articles":481},"frontend","Frontend",99,{"slug":72,"name":73,"articles":483},74,{"slug":485,"name":486,"articles":487},"css","Css",54,{"slug":489,"name":490,"articles":491},"performance","Performance",31,{"slug":66,"name":67,"articles":53},{"slug":494,"name":495,"articles":496},"react","React",20,{"slug":498,"name":499,"articles":496},"programming","Programming",{"slug":501,"name":502,"articles":503},"browser","Browser",18,{"slug":69,"name":70,"articles":505},17,{"slug":507,"name":508,"articles":455},"html","Html",[],{"items":511,"meta":614},[512,524,539,554,569,584,599],{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":513,"likeCount":19,"commentCount":19,"author":514,"vertical":515,"topic":516,"tags":517,"_count":522,"playground":523,"hasQuiz":17,"hasPlayground":17},27,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[518,519,520,521],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":46},{"id":525,"slug":452,"title":453,"subtitle":48,"excerpt":526,"coverUrl":527,"locale":13,"readingMinutes":455,"publishedAt":454,"viewCount":528,"likeCount":19,"commentCount":19,"author":529,"vertical":530,"topic":531,"tags":532,"_count":537,"playground":538,"hasQuiz":17,"hasPlayground":17},"01a15d15-305d-77da-a29a-d16a7df8ed3f","NestJS 12.2 defaults validationError.target to false, so a custom exceptionFactory reading error.target now gets undefined. The fix, plus a cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-validation-error-target.png",297,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[533,534,535,536],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":452},{"id":540,"slug":448,"title":449,"subtitle":48,"excerpt":541,"coverUrl":542,"locale":13,"readingMinutes":51,"publishedAt":450,"viewCount":543,"likeCount":19,"commentCount":19,"author":544,"vertical":545,"topic":546,"tags":547,"_count":552,"playground":553,"hasQuiz":17,"hasPlayground":17},"01a11150-cc6d-7657-8fe5-ae7578a8058b","NestJS 12 stops inheriting @Optional() once a subclass redeclares its own constructor — not when it has none at all. Cheat sheet included.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-optional-not-inherited.png",381,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[548,549,550,551],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":448},{"id":555,"slug":444,"title":445,"subtitle":48,"excerpt":556,"coverUrl":557,"locale":13,"readingMinutes":421,"publishedAt":446,"viewCount":558,"likeCount":19,"commentCount":19,"author":559,"vertical":560,"topic":561,"tags":562,"_count":567,"playground":568,"hasQuiz":17,"hasPlayground":17},"01a1358a-158e-70df-8178-64af7e9a5932","NestJS 12 changed how lifecycle hooks are ordered. Learn what onModuleInit and onApplicationBootstrap really guarantee, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-lifecycle-hook-order.png",411,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[563,564,565,566],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":444},{"id":570,"slug":440,"title":441,"subtitle":48,"excerpt":571,"coverUrl":572,"locale":13,"readingMinutes":426,"publishedAt":442,"viewCount":573,"likeCount":19,"commentCount":19,"author":574,"vertical":575,"topic":576,"tags":577,"_count":582,"playground":583,"hasQuiz":17,"hasPlayground":17},"01a0f0ef-ccc8-73e8-9b53-e6ce4c7fe9ff","NestJS 12 adds Standard Schema validation for @Body(), @Query(), and @Param() — validate with Zod or Valibot, no class-validator DTO. Cheat sheet included.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-standard-schema-validation.png",730,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[578,579,580,581],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":440},{"id":585,"slug":436,"title":437,"subtitle":48,"excerpt":586,"coverUrl":587,"locale":13,"readingMinutes":426,"publishedAt":438,"viewCount":588,"likeCount":19,"commentCount":19,"author":589,"vertical":590,"topic":591,"tags":592,"_count":597,"playground":598,"hasQuiz":17,"hasPlayground":17},"01a0ddae-48ac-73c6-bdee-1420cdfb17c2","A complete guide to NestJS guards: how CanActivate, ExecutionContext, and the Reflector decide who reaches your route handler, with a cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-guards-canactivate-reflector.png",867,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[593,594,595,596],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":436},{"id":600,"slug":432,"title":433,"subtitle":48,"excerpt":601,"coverUrl":602,"locale":13,"readingMinutes":421,"publishedAt":434,"viewCount":603,"likeCount":19,"commentCount":19,"author":604,"vertical":605,"topic":606,"tags":607,"_count":612,"playground":613,"hasQuiz":17,"hasPlayground":17},"01a0a630-c856-72d3-bae5-3339e38a8564","A complete guide to NestJS's testing module: how overrideProvider really wires mocks, why get() fails on scoped providers, and using useMocker.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-testing-provider-overrides.png",820,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[608,609,610,611],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":432},{"page":75,"perPage":615,"total":415,"totalPages":31},7,"\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003Cdiv>13\u003C\u002Fdiv>\u003Cdiv>14\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F all-exceptions.filter.ts\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Catch\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n@\u003Cspan class=\"shj-class\">Injectable\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AllExceptionsFilter\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">implements\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExceptionFilter\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">constructor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">private\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">readonly\u003C\u002Fspan> tenantContext\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">TenantContextService\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\n  \u003Cspan class=\"shj-kwd\">catch\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>exception\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> unknown\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> host\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ArgumentsHost\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> ctx \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> host\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">switchToHttp\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> response \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> ctx\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">getResponse\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-cmnt\">\u002F\u002F tenantContext reads the current request's tenant for the error report\n\u003C\u002Fspan>    \u003Cspan class=\"shj-func\">reportToSentry\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>exception\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">this\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>tenantContext\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>tenantId\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    response\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">status\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-num\">500\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">json\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> message\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-str\">'Internal server error'\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F tenant-context.service.ts\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Injectable\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> scope\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Scope\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-num\">REQUEST\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">TenantContextService\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">constructor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Inject\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-num\">REQUEST\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">private\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">readonly\u003C\u002Fspan> request\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Request\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">get\u003C\u002Fspan> \u003Cspan class=\"shj-func\">tenantId\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">this\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>request\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>headers\u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\u003Cspan class=\"shj-str\">'x-tenant-id'\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F app.module.ts\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Module\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  providers\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\n    \u003Cspan class=\"shj-class\">TenantContextService\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_FILTER\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useClass\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AllExceptionsFilter\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n  \u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AppModule\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F main.ts — instance-based, works but can't use DI\n\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> app \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">await\u003C\u002Fspan> \u003Cspan class=\"shj-class\">NestFactory\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">create\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">AppModule\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\napp\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">useGlobalFilters\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AllExceptionsFilter\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-kwd\">await\u003C\u002Fspan> app\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">listen\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-num\">3000\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F app.module.ts — provider-based, can use DI\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Module\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  providers\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_FILTER\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useClass\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AllExceptionsFilter\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AppModule\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>",{"locked":18,"total":19,"comments":622},[]]