dn-ioc

14 · Reference

Errors & Diagnostics

When wiring goes sideways, dn-ioc fails fast with plain, predictable Error messages rather than cryptic internal stack frames or silent undefined values. Every verbatim runtime message the kernel can produce is cataloged below, along with evaluation order, propagation rules, and concrete fixes.

unbound-token.ts
1export const databaseUrlToken = token<string>('DatabaseUrl')23export const unboundApp = bootstrapApp(({ inject }) => {4  inject(databaseUrlToken)5})67try {8  await unboundApp.start()9} catch (error) {10  console.error((error as Error).message) // No provider for token: DatabaseUrl11}

Verbatim Error Message Catalog

dn-ioc throws eight standard Error messages plus two AggregateError wrappers when multiple failures collide during teardown:

Verbatim Error.messageThrown / rejected byCause
No provider for token: Nameinject()token('Name') was injected, but neither the active scope nor any ancestor scope bound it via provideFor(). Prints <anonymous> if the token has no description.
Circular dependency detected: A -> B -> Ainject()Sync or pending async providers depend on each other in a loop. The path starts at the key closing the cycle (A -> A for self-injection) and prints <anonymous> for unnamed keys.
Invalid inject key receivedinject(), provideFor()Key is not a genuine Token or Ref—for example null, a primitive, a ProviderDef, a ProviderBundle, or a shallow clone ({ ...myToken }).
Invalid provider input receivedbootstrapApp() (root) or inject() (nested)An entry in providers or bundleProviders() (once installed) is not a Ref, ProviderDef, ProviderBundle, or array of them (for example, passing a bare Token without provideFor()).
Cannot inject after the app has stoppedinject()inject() was called during or after app.stop()—including inside an onDispose hook, a detached async continuation, or against an already-cached key.
onDispose can only be called while the factory is runningonDispose()onDispose() was called after its factory returned synchronously, threw, or settled its returned Promise (for example inside setTimeout, a request handler, or another cleanup hook).
Cannot register cleanup after the app has stoppedonDispose()An async factory was still suspended across an await when app.stop() ran, then woke up and called onDispose().
Cannot start an app that has been stoppedapp.start() (rejects)app.start() was called after app.stop() had already started or completed.
App stop failed (AggregateError)app.stop() (rejects)Two or more onDispose hooks threw or rejected during teardown (error.errors holds each failure in LIFO execution order).
App startup and stop failed (AggregateError)app.start() (rejects)app.start() failed and its automatic stop() rollback also failed (error.errors holds [startupError, cleanupError]).

Precedence & Timing Nuances

A few subtle ordering guarantees in the kernel are worth knowing when debugging edge cases:

  1. Stopped check runs first in inject(): Cannot inject after the app has stopped is checked before key validation, cycle detection, scope lookup, or cache hits. Even injecting an already-cached instance or an invalid key after app.stop() throws Cannot inject after the app has stopped.
  2. Root vs nested Invalid provider input received: Root providers passed to bootstrapApp() are flattened and validated synchronously before an App handle exists. Nested { providers } on provide() or provideFor() are validated lazily inside inject() the first time that provider's private child subtree is resolved.
  3. Active-window check precedes stopped check in onDispose(): Once a factory finishes (returns, throws, or settles its Promise), its onDispose handle is permanently deactivated and always throws onDispose can only be called while the factory is running—even if the app stops afterward.
  4. Stopped check precedes start() idempotence: While an app is unstarted or running, app.start() caches and returns the same Promise. Once app.stop() is called, app.start() checks the stopped state ahead of that cache and rejects with Cannot start an app that has been stopped instead of replaying a stale resolved run.
dispose.ts
1export const storeRef = provide(() => ({ closed: false }))23export const serviceRef = provide(({ inject, onDispose }) => {4  const store = inject(storeRef)5  onDispose(async () => {6    store.closed = true7  })8  return {9    read: () => inject(storeRef).closed,10  }11})1213export let service!: { read: () => boolean }1415export const disposeApp = bootstrapApp(({ inject }) => {16  service = inject(serviceRef)17})1819await disposeApp.start()20console.log(service.read()) // false (deferred inject works while running)21await disposeApp.stop()22// service.read() -> throws "Cannot inject after the app has stopped"

Failure Propagation Matrix

Where an error lands—and what happens to cached instances and open resources—depends on which phase threw it:

Failure scenarioHow it surfacesCache & app state afterward
Invalid entry in root bootstrapApp(..., { providers })Throws synchronously from bootstrapApp()No App handle is created.
Invalid entry in nested { providers }Throws synchronously from inject() on first subtree resolutionSubtree is not created; propagates to caller (rolls back start() if during boot).
Sync factory throws inside inject()Throws synchronously from inject()Nothing is cached; a later inject() while running re-runs the factory.
Async factory rejects inside inject()Returned shared Promise rejectsPending cache entry is evicted; a later inject() while running retries fresh.
Root callback (or an awaited startup Promise) failsapp.start() runs await app.stop(), then rejectsApp is permanently stopped; single error rethrown unchanged, or AggregateError if cleanup also failed.
Unawaited background Promise rejectsRejects on that Promise directly (start() ignores it)App stays running; failed async provider is evicted from cache.
One onDispose hook fails during app.stop()Remaining hooks still run; app.stop() rejects with that error unchangedApp stays permanently stopped.
Multiple onDispose hooks fail during app.stop()All hooks run; app.stop() rejects with AggregateError (App stop failed)App stays permanently stopped.

Quick Fix Recipes

  • No provider for token: Name: Bind the token with provideFor(myToken, ...) in your root providers or infraProviders bundle. If this happened in an integration test, check that you didn't forget infraProviders; if in a child subtree, check Scopes & Subtrees to verify the consumer isn't owned by a parent scope that lacks the binding.
  • Circular dependency detected: A -> B -> A: Extract the shared state or helper that both A and B need into a third provider C (A -> C and B -> C). If <anonymous> appears in the chain, assign the inline arrow factory to a named const (const MyService = ({ inject }: Context) => { ... }; provide(MyService)) to pinpoint the exact culprit.
  • Invalid provider input received: Check your providers: [...] arrays and bundleProviders(...) calls for a bare token() passed directly instead of provideFor(myToken, factory), an undefined import from a circular ES module, or a cloned object.
  • Invalid inject key received: Make sure you passed a Token or Ref—not a ProviderDef returned by provideFor() or a ProviderBundle returned by bundleProviders()—into inject() or provideFor().
  • Cannot inject after the app has stopped: Move const dep = inject(depRef) to the top of your factory before onDispose(() => { ... }) so teardown hooks close over resolved values instead of calling inject() during shutdown (see Lifecycle & Cleanup). If a request handler threw this during graceful drain, stop accepting incoming requests before calling app.stop().
  • onDispose can only be called while the factory is running: Register cleanup synchronously at the top of your factory—or before your async factory's returned Promise resolves—never inside a timer callback, event listener, or lazy method.
  • Cannot register cleanup after the app has stopped: Your app called stop() while an async factory was still mid-flight across an await. Register onDispose() before yielding when possible, or pass an AbortSignal so in-flight setup aborts cleanly when shutdown starts (see Async Providers).
  • Cannot start an app that has been stopped: A stopped App is single-use because its cleanup hooks have already released its resources. Call bootstrapApp(...) again to construct a fresh App instance—just like per-test setups do in Testing & Mocking.