{"version":3,"file":"error-hooks-Wis5htWb.mjs","names":[],"sources":["../src/tako/error-hooks.ts"],"sourcesContent":["import { createLogger } from \"../logger\";\n\nlet installed = false;\nlet uncaughtHandler: ((err: unknown) => void) | undefined;\nlet rejectionHandler: ((reason: unknown) => void) | undefined;\n\n/**\n * Install runtime-level listeners that capture uncaught errors and unhandled\n * promise rejections and emit them as single structured log events via the\n * Tako SDK logger. The full stack travels in the `msg` field so downstream\n * renderers see one event per error instead of one event per stack frame line.\n *\n * On `uncaughtException`: logs then schedules `process.exit(1)` via\n * `setImmediate` so the stdout pipe has a tick to flush.\n * On `unhandledRejection`: logs but does not exit.\n *\n * Idempotent: calling twice registers listeners only once.\n *\n * @param scope - Log scope used on emitted lines (e.g. `\"app\"`, `\"worker\"`).\n */\nexport function installErrorHooks(scope: string): void {\n  if (installed) return;\n  installed = true;\n  const log = createLogger(scope);\n\n  uncaughtHandler = (err: unknown) => {\n    const { msg, fieldError } = describeError(err);\n    log.error(msg, { error: fieldError, kind: \"uncaughtException\" });\n    setImmediate(() => process.exit(1));\n  };\n  rejectionHandler = (reason: unknown) => {\n    const { msg, fieldError } = describeError(reason);\n    log.error(msg, { error: fieldError, kind: \"unhandledRejection\" });\n  };\n\n  process.on(\"uncaughtException\", uncaughtHandler);\n  process.on(\"unhandledRejection\", rejectionHandler);\n}\n\nfunction describeError(value: unknown): { msg: string; fieldError: unknown } {\n  if (value instanceof Error) {\n    const msg =\n      value.stack && value.stack.length > 0 ? value.stack : `${value.name}: ${value.message}`;\n    return { msg, fieldError: value };\n  }\n  return { msg: String(value), fieldError: value };\n}\n\n/** @internal Reset module state between tests. Do not call from user code. */\nexport function resetErrorHooksForTests(): void {\n  if (uncaughtHandler) process.off(\"uncaughtException\", uncaughtHandler);\n  if (rejectionHandler) process.off(\"unhandledRejection\", rejectionHandler);\n  uncaughtHandler = undefined;\n  rejectionHandler = undefined;\n  installed = false;\n}\n"],"mappings":";;;AAEA,IAAI,YAAY;AAChB,IAAI;AACJ,IAAI;;;;;;;;;;;;;;;AAgBJ,SAAgB,kBAAkB,OAAqB;CACrD,IAAI,WAAW;CACf,YAAY;CACZ,MAAM,MAAM,aAAa,MAAM;CAE/B,mBAAmB,QAAiB;EAClC,MAAM,EAAE,KAAK,eAAe,cAAc,IAAI;EAC9C,IAAI,MAAM,KAAK;GAAE,OAAO;GAAY,MAAM;GAAqB,CAAC;EAChE,mBAAmB,QAAQ,KAAK,EAAE,CAAC;;CAErC,oBAAoB,WAAoB;EACtC,MAAM,EAAE,KAAK,eAAe,cAAc,OAAO;EACjD,IAAI,MAAM,KAAK;GAAE,OAAO;GAAY,MAAM;GAAsB,CAAC;;CAGnE,QAAQ,GAAG,qBAAqB,gBAAgB;CAChD,QAAQ,GAAG,sBAAsB,iBAAiB;;AAGpD,SAAS,cAAc,OAAsD;CAC3E,IAAI,iBAAiB,OAGnB,OAAO;EAAE,KADP,MAAM,SAAS,MAAM,MAAM,SAAS,IAAI,MAAM,QAAQ,GAAG,MAAM,KAAK,IAAI,MAAM;EAClE,YAAY;EAAO;CAEnC,OAAO;EAAE,KAAK,OAAO,MAAM;EAAE,YAAY;EAAO"}