{"version":3,"file":"install-error-handlers.js","names":[],"sources":["esm/cli/install-error-handlers.js"],"sourcesContent":["let userPrepare;\nlet depth = 0;\nconst restoreTrail = [];\nfunction defaultFormat(error, stack) {\n  const name = error?.name ?? \"Error\";\n  const message = error?.message ?? \"\";\n  const head = `${name}: ${message}`;\n  try {\n    const lines = stack.map((frame) => {\n      try {\n        return `    at ${frame}`;\n      } catch {\n        return \"    at <frame>\";\n      }\n    });\n    return `${head}\n${lines.join(\"\\n\")}`;\n  } catch {\n    return head;\n  }\n}\nfunction guardedPrepare(error, stack) {\n  if (depth > 0) return defaultFormat(error, stack);\n  if (typeof userPrepare !== \"function\") return defaultFormat(error, stack);\n  depth++;\n  try {\n    return userPrepare(error, stack);\n  } catch {\n    return defaultFormat(error, stack);\n  } finally {\n    depth--;\n  }\n}\nlet installed = false;\nfunction installPrepareStackTraceGuard() {\n  if (installed) return;\n  installed = true;\n  const existing = Error.prepareStackTrace;\n  if (typeof existing === \"function\" && existing !== guardedPrepare) {\n    userPrepare = existing;\n  }\n  Object.defineProperty(Error, \"prepareStackTrace\", {\n    configurable: true,\n    get() {\n      return guardedPrepare;\n    },\n    set(value) {\n      if (value === guardedPrepare) {\n        if (restoreTrail.length > 0) {\n          userPrepare = restoreTrail.pop();\n        }\n        return;\n      }\n      restoreTrail.push(userPrepare);\n      userPrepare = typeof value === \"function\" ? value : void 0;\n    }\n  });\n}\nfunction formatErrorSafely(err) {\n  const prevPrepare = Error.prepareStackTrace;\n  Error.prepareStackTrace = void 0;\n  try {\n    if (err instanceof Error) {\n      let stack = \"\";\n      try {\n        stack = err.stack ?? \"\";\n      } catch {\n      }\n      if (stack) return stack;\n      const name = err.name || \"Error\";\n      const message = err.message || \"\";\n      return `${name}: ${message}`;\n    }\n    return String(err);\n  } catch {\n    try {\n      return String(err);\n    } catch {\n      return \"<unprintable error>\";\n    }\n  } finally {\n    Error.prepareStackTrace = prevPrepare;\n  }\n}\nfunction installSafeUncaughtHandler(label) {\n  installPrepareStackTraceGuard();\n  process.on(\"uncaughtException\", (err) => {\n    try {\n      const formatted = formatErrorSafely(err);\n      process.stderr.write(`[${label}] uncaught exception\n${formatted}\n`);\n    } catch {\n    }\n    process.exit(1);\n  });\n  process.on(\"unhandledRejection\", (reason) => {\n    try {\n      const formatted = formatErrorSafely(reason);\n      process.stderr.write(`[${label}] unhandled rejection\n${formatted}\n`);\n    } catch {\n    }\n  });\n}\nexport {\n  formatErrorSafely,\n  installPrepareStackTraceGuard,\n  installSafeUncaughtHandler\n};\n//# sourceMappingURL=install-error-handlers.js.map\n"],"mappings":";AAAA,IAAI;AACJ,IAAI,QAAQ;AACZ,MAAM,eAAe,CAAC;AACtB,SAAS,cAAc,OAAO,OAAO;CACnC,MAAM,OAAO,OAAO,QAAQ;CAC5B,MAAM,UAAU,OAAO,WAAW;CAClC,MAAM,OAAO,GAAG,KAAK,IAAI;CACzB,IAAI;EACF,MAAM,QAAQ,MAAM,KAAK,UAAU;GACjC,IAAI;IACF,OAAO,UAAU;GACnB,QAAQ;IACN,OAAO;GACT;EACF,CAAC;EACD,OAAO,GAAG,KAAK;EACjB,MAAM,KAAK,IAAI;CACf,QAAQ;EACN,OAAO;CACT;AACF;AACA,SAAS,eAAe,OAAO,OAAO;CACpC,IAAI,QAAQ,GAAG,OAAO,cAAc,OAAO,KAAK;CAChD,IAAI,OAAO,gBAAgB,YAAY,OAAO,cAAc,OAAO,KAAK;CACxE;CACA,IAAI;EACF,OAAO,YAAY,OAAO,KAAK;CACjC,QAAQ;EACN,OAAO,cAAc,OAAO,KAAK;CACnC,UAAU;EACR;CACF;AACF;AACA,IAAI,YAAY;AAChB,SAAS,gCAAgC;CACvC,IAAI,WAAW;CACf,YAAY;CACZ,MAAM,WAAW,MAAM;CACvB,IAAI,OAAO,aAAa,cAAc,aAAa,gBAAgB;EACjE,cAAc;CAChB;CACA,OAAO,eAAe,OAAO,qBAAqB;EAChD,cAAc;EACd,MAAM;GACJ,OAAO;EACT;EACA,IAAI,OAAO;G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