{"record":{"id":"b7c3ad447893cbf5","repo":"honojs/hono","slug":"context-is-not-available","errorCode":null,"errorMessage":"Context is not available","messagePattern":"Context is not available","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/middleware/context-storage/index.ts","lineNumber":56,"sourceCode":" * const getMessage = () => {\n *   return getContext<Env>().var.message\n * }\n * ```\n */\nexport const contextStorage = (): MiddlewareHandler => {\n  return async function contextStorage(c, next) {\n    await asyncLocalStorage.run(c, next)\n  }\n}\n\nexport const tryGetContext = <E extends Env = Env>(): Context<E> | undefined => {\n  return asyncLocalStorage.getStore() as Context<E> | undefined\n}\n\nexport const getContext = <E extends Env = Env>(): Context<E> => {\n  const context = tryGetContext<E>()\n  if (!context) {\n    throw new Error('Context is not available')\n  }\n  return context\n}\n","sourceCodeStart":38,"sourceCodeEnd":60,"githubUrl":"https://github.com/honojs/hono/blob/e2740d5a1bd0b4254e517e3af8b60789284bc7bd/src/middleware/context-storage/index.ts#L38-L60","documentation":"This error is thrown by getContext() from hono/context-storage when called outside the AsyncLocalStorage context established by the contextStorage() middleware. The middleware stores the request Context in an AsyncLocalStorage; getContext() retrieves it and throws if the store is empty because no request scope is active. tryGetContext() is the non-throwing variant that returns undefined instead.","triggerScenarios":"Calling getContext() in code reached outside a request (module init, scheduled job, CLI startup, WebSocket disconnect handler after the request completed); calling it inside a callback that escaped the async chain (e.g. setTimeout/setInterval not awaited within the request, event emitter handlers registered earlier); forgetting to register app.use(contextStorage()) before the code runs.","commonSituations":"Background tasks kicked off during a request that outlive it, queue consumers sharing code with request handlers, forgetting the contextStorage middleware in tests, or calling getContext in a top-level await during bootstrap.","solutions":["Ensure app.use(contextStorage()) is applied before any handler that uses getContext()","Move logic that needs the Context inside the request flow, or pass the Context/values explicitly to async callbacks","Use tryGetContext() and handle undefined when the call site may be outside a request","In tests, wrap calls in a request through the app (or the storage run) rather than calling helpers directly"],"exampleFix":"// before\nconst getUser = () => {\n  const c = getContext() // throws in cron job\n  return c.get('user')\n}\n\n// after\nconst getUser = () => {\n  const c = tryGetContext()\n  return c?.get('user') // undefined outside a request\n}","handlingStrategy":"type-guard","validationCode":"import { tryGetContext } from 'hono/context-storage'\nconst maybeCtx = tryGetContext()\nif (!maybeCtx) {\n  // outside a request: use explicit arguments instead\n}","typeGuard":"import { tryGetContext, type Env } from 'hono/context-storage'\nconst inRequestScope = (): boolean => tryGetContext() !== undefined\nconst getContextSafe = <E extends Env = Env>() => {\n  const c = tryGetContext<E>()\n  if (!c) throw new Error('Context is not available')\n  return c\n}","tryCatchPattern":"try {\n  const c = getContext()\n} catch (err) {\n  if (err instanceof Error && err.message === 'Context is not available') {\n    // fallback: pass values explicitly\n  }\n}","preventionTips":["Always register contextStorage() middleware before handlers that use getContext()","Prefer tryGetContext() in any code that might run outside a request","Pass Context explicitly into setTimeout/queue/event callbacks instead of relying on ALS","In tests, invoke helpers through app.request() so the storage is populated"],"tags":["context-storage","async-local-storage","request-scope","middleware"],"backgroundTag":"no-active-request-context","analyzedSha":"e2740d5a1bd0b4254e517e3af8b60789284bc7bd","analyzedAt":"2026-08-28T10:18:08.750Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}