{"version":3,"file":"policyEngineCliError-BnBghLd1.mjs","names":[],"sources":["../src/commands/policy/helpers/policyEngineCliError.ts"],"sourcesContent":["/** Whether policy commands should include technical error details in output. */\nlet policyEngineCliDebugEnabled = false;\n\n/**\n * Enables or disables technical Policy Engine CLI error output for the current command run.\n *\n * @param enabled - When true, API causes and stack traces are included in failures\n */\nexport function setPolicyEngineCliDebug(enabled: boolean): void {\n  policyEngineCliDebugEnabled = enabled;\n}\n\n/**\n * Returns true when `--debug` was passed to a policy command.\n *\n * @returns Whether debug output is enabled\n */\nexport function isPolicyEngineCliDebugEnabled(): boolean {\n  return policyEngineCliDebugEnabled;\n}\n\n/**\n * User-facing Policy Engine CLI error without a stack trace by default.\n */\nexport class PolicyEngineCliError extends Error {\n  /**\n   * Creates a policy CLI error suitable for end-user output.\n   *\n   * @param message - User-readable error text\n   * @param options - Optional underlying failure\n   */\n  constructor(\n    message: string,\n    options?: {\n      /** Original failure, typically a got HTTPError */\n      cause?: unknown;\n    },\n  ) {\n    super(message, { cause: options?.cause });\n    this.name = 'PolicyEngineCliError';\n  }\n}\n\n/**\n * Formats a Policy Engine CLI error for terminal output.\n *\n * @param error - Thrown policy CLI error\n * @returns Message only by default; technical details when debug mode is enabled\n */\nexport function formatPolicyEngineCliErrorForTerminal(error: PolicyEngineCliError): string {\n  if (!isPolicyEngineCliDebugEnabled()) {\n    return error.message;\n  }\n\n  const parts = [error.message, '', 'Debug details:'];\n  const cause = error.cause;\n\n  if (cause instanceof Error) {\n    parts.push(cause.stack ?? cause.message);\n  } else if (cause !== undefined) {\n    parts.push(String(cause));\n  } else {\n    parts.push('No underlying cause was recorded.');\n  }\n\n  return parts.join('\\n');\n}\n\n/**\n * Formats thrown values for Stricli, suppressing stacks for policy CLI errors unless debug is enabled.\n *\n * @param exc - Thrown value from a command\n * @returns Terminal-safe error text\n */\nexport function formatPolicyEngineCliException(exc: unknown): string | undefined {\n  if (exc instanceof PolicyEngineCliError) {\n    return formatPolicyEngineCliErrorForTerminal(exc);\n  }\n\n  return undefined;\n}\n\n/**\n * Formats any thrown command value for terminal output.\n *\n * `PolicyEngineCliError` values are formatted by `formatPolicyEngineCliException`.\n * Other errors (e.g. bundle-not-found, missing OPA binary, local OPA failures)\n * are plain `Error`s -- return only their message by default so operators don't\n * see a minified stack trace, and surface the stack only when a policy command\n * opted in via `--debug`.\n *\n * @param exc - Thrown value from a command\n * @returns Terminal-safe error text\n */\nexport function formatCliException(exc: unknown): string {\n  const policyMessage = formatPolicyEngineCliException(exc);\n  if (policyMessage) {\n    return policyMessage;\n  }\n\n  if (exc instanceof Error) {\n    return isPolicyEngineCliDebugEnabled() ? (exc.stack ?? String(exc)) : exc.message;\n  }\n\n  return String(exc);\n}\n"],"mappings":"AACA,IAAI,EAA8B,GAOlC,SAAgB,EAAwB,EAAwB,CAC9D,EAA8B,EAQhC,SAAgB,GAAyC,CACvD,OAAO,EAMT,IAAa,EAAb,cAA0C,KAAM,CAO9C,YACE,EACA,EAIA,CACA,MAAM,EAAS,CAAE,MAAO,GAAS,MAAO,CAAC,CACzC,KAAK,KAAO,yBAUhB,SAAgB,EAAsC,EAAqC,CACzF,GAAI,CAAC,GAA+B,CAClC,OAAO,EAAM,QAGf,IAAM,EAAQ,CAAC,EAAM,QAAS,GAAI,iBAAiB,CAC7C,EAAQ,EAAM,MAUpB,OARI,aAAiB,MACnB,EAAM,KAAK,EAAM,OAAS,EAAM,QAAQ,CAC/B,IAAU,IAAA,GAGnB,EAAM,KAAK,oCAAoC,CAF/C,EAAM,KAAK,OAAO,EAAM,CAAC,CAKpB,EAAM,KAAK;EAAK,CASzB,SAAgB,EAA+B,EAAkC,CAC/E,GAAI,aAAe,EACjB,OAAO,EAAsC,EAAI,CAkBrD,SAAgB,EAAmB,EAAsB,CAUvD,OATsB,EAA+B,EACjD,GAIA,aAAe,MACV,GAA+B,CAAI,EAAI,OAAS,OAAO,EAAI,CAAI,EAAI,QAGrE,OAAO,EAAI"}