{"version":3,"file":"errors-DW5YLbOP.cjs","names":[],"sources":["../../src/errors.ts"],"sourcesContent":["/**\n * Represents a failed action result. Returned by `fail()` from server actions.\n *\n * The `__nix_js_action_failure` marker is set on the instance so the server can\n * detect it even when the value crosses a bundling boundary (e.g. the CLI is\n * bundled separately from the user's action modules).\n */\nexport class ActionFailure<TData = unknown> {\n  readonly __nix_js_action_failure = true;\n  constructor(\n    public status: number,\n    public data: TData,\n  ) {}\n}\n\n/**\n * Represents a redirect returned by a server action. Returned by `redirect()`.\n */\nexport class RedirectResponse {\n  readonly __nix_js_action_redirect = true;\n  constructor(\n    public status: number,\n    public location: string,\n  ) {}\n}\n\n/**\n * Helper to return a validation/error response from a server action.\n *\n * Both argument orders are accepted:\n *\n * ```ts\n * return fail(400, { email: \"Invalid email\" });\n * return fail({ email: \"Invalid email\" }, 400);\n * return fail({ email: \"Invalid email\" }); // defaults to status 400\n * ```\n */\nexport function fail<TData>(\n  statusOrData: number | TData,\n  dataOrStatus?: TData | number,\n): ActionFailure<unknown> {\n  if (typeof statusOrData === \"number\") {\n    return new ActionFailure(statusOrData, dataOrStatus as TData);\n  }\n  return new ActionFailure((dataOrStatus as number) ?? 400, statusOrData);\n}\n\n/**\n * Helper to return a redirect from a server action.\n *\n * Both argument orders are accepted:\n *\n * ```ts\n * return redirect(303, \"/login\");\n * return redirect(\"/login\"); // defaults to status 303\n * ```\n */\nexport function redirect(\n  statusOrLocation: number | string,\n  locationOrStatus?: string | number,\n): RedirectResponse {\n  if (typeof statusOrLocation === \"number\") {\n    return new RedirectResponse(statusOrLocation, locationOrStatus as string);\n  }\n  return new RedirectResponse((locationOrStatus as number) ?? 303, statusOrLocation);\n}\n\n/**\n * Type guard for action failures. Uses the marker field so it works across\n * bundling boundaries where `instanceof` fails.\n */\nexport function isActionFailure(value: unknown): value is ActionFailure {\n  return (\n    typeof value === \"object\" &&\n    value !== null &&\n    (value as { __nix_js_action_failure?: unknown }).__nix_js_action_failure === true\n  );\n}\n\n/**\n * Type guard for redirects. Uses the marker field so it works across bundling\n * boundaries where `instanceof` fails.\n */\nexport function isRedirectResponse(value: unknown): value is RedirectResponse {\n  return (\n    typeof value === \"object\" &&\n    value !== null &&\n    (value as { __nix_js_action_redirect?: unknown }).__nix_js_action_redirect === true\n  );\n}\n\n// --- Public error sanitization (production-safe 500 responses) ---\n\n/**\n * A stable, publicly safe error description. Never includes stacks, internal\n * paths or messages that could leak secrets or filesystem details.\n */\nexport interface PublicErrorInfo {\n  /** Stable machine-readable code for the response body. */\n  code: string;\n  /** Stable public message. In non-production this may include the raw message. */\n  message: string;\n  status: number;\n}\n\n/**\n * Maps an arbitrary thrown value to a public-safe error info. By default the\n * public message is generic; `includeDetail` (dev/verbose mode) appends the\n * original `Error.message` for local debugging.\n */\nexport function toPublicErrorInfo(error: unknown, options: { includeDetail?: boolean } = {}): PublicErrorInfo {\n  const code = \"INTERNAL_SERVER_ERROR\";\n  const status = 500;\n  if (options.includeDetail && error instanceof Error && error.message) {\n    return { code, status, message: error.message };\n  }\n  return { code, status, message: \"Internal Server Error\" };\n}\n\n/**\n * Builds a production-safe JSON error Response. Logs the raw error separately\n * (never reflected in the response body) and keeps the request id header.\n */\nexport function publicErrorResponse(\n  error: unknown,\n  options: { includeDetail?: boolean; requestId?: string } = {},\n): Response {\n  const info = toPublicErrorInfo(error, options);\n  const headers: Record<string, string> = {\n    \"Content-Type\": \"application/json; charset=utf-8\",\n    \"Cache-Control\": \"no-store\",\n  };\n  if (options.requestId) headers[\"X-Request-Id\"] = options.requestId;\n  return new Response(JSON.stringify({ error: info }), {\n    status: info.status,\n    headers,\n  });\n}\n\n/** True when the error should be re-thrown as control flow instead of a 500. */\nexport function isFirstClassResponse(error: unknown): error is Response {\n  return typeof Response !== \"undefined\" && error instanceof Response;\n}\n"],"mappings":"iUAOa,EAAb,KAA4C,CAGjC,OACA,KAHT,wBAAmC,GACnC,YACE,EACA,EACA,CAFO,KAAA,OAAA,EACA,KAAA,KAAA,CACN,CACL,EAKa,EAAb,KAA8B,CAGnB,OACA,SAHT,yBAAoC,GACpC,YACE,EACA,EACA,CAFO,KAAA,OAAA,EACA,KAAA,SAAA,CACN,CACL,EAaA,SAAgB,EACd,EACA,EACwB,CAIxB,OAHI,OAAO,GAAiB,SACnB,IAAI,EAAc,EAAc,CAAqB,EAEvD,IAAI,EAAe,GAA2B,IAAK,CAAY,CACxE,CAYA,SAAgB,EACd,EACA,EACkB,CAIlB,OAHI,OAAO,GAAqB,SACvB,IAAI,EAAiB,EAAkB,CAA0B,EAEnE,IAAI,EAAkB,GAA+B,IAAK,CAAgB,CACnF,CAMA,SAAgB,EAAgB,EAAwC,CACtE,OACE,OAAO,GAAU,YACjB,GACC,EAAgD,0BAA4B,EAEjF,CAMA,SAAgB,EAAmB,EAA2C,CAC5E,OACE,OAAO,GAAU,YACjB,GACC,EAAiD,2BAA6B,EAEnF,CAqBA,SAAgB,EAAkB,EAAgB,EAAuC,CAAC,EAAoB,CAC5G,IAAM,EAAO,wBAKb,OAHI,EAAQ,eAAiB,aAAiB,OAAS,EAAM,QACpD,CAAE,OAAM,WAAQ,QAAS,EAAM,OAAQ,EAEzC,CAAE,OAAM,WAAQ,QAAS,uBAAwB,CAC1D,CAMA,SAAgB,EACd,EACA,EAA2D,CAAC,EAClD,CACV,IAAM,EAAO,EAAkB,EAAO,CAAO,EACvC,EAAkC,CACtC,eAAgB,kCAChB,gBAAiB,UACnB,EAEA,OADI,EAAQ,YAAW,EAAQ,gBAAkB,EAAQ,WAClD,IAAI,SAAS,KAAK,UAAU,CAAE,MAAO,CAAK,CAAC,EAAG,CACnD,OAAQ,EAAK,OACb,SACF,CAAC,CACH"}