The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import type { APIError } from "better-auth"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 4 | |
| 5 | /** |
| 6 | * better-call stows the response headers accumulated by the endpoint |
| 7 | * (including `set-cookie` written before the failure) on the thrown |
| 8 | * `APIError` under this well-known symbol. It is registered with |
| 9 | * `Symbol.for`, so we can reference it without depending on `better-call`. |
| 10 | */ |
| 11 | const kAPIErrorHeaders = Symbol.for("better-call:api-error-headers"); |
| 12 | |
| 13 | /** |
| 14 | * Structural `APIError` detection. Deliberately not `instanceof` — the |
| 15 | * error may originate from a different module instance (bundled better-auth |
| 16 | * vs peer) or realm. |
| 17 | */ |
| 18 | export const isAPIErrorLike = (error: unknown): error is APIError => |
| 19 | typeof error === "object" && |
| 20 | error !== null && |
| 21 | error instanceof Error && |
| 22 | error.name === "APIError" && |
| 23 | typeof (error as APIError).statusCode === "number"; |
| 24 | |
| 25 | /** |
| 26 | * Merge an `APIError`'s visible `headers` with the hidden response headers |
| 27 | * better-call carries on {@link kAPIErrorHeaders}. `set-cookie` values are |
| 28 | * appended (never collapsed) so session cookies written before the failure |
| 29 | * survive. |
| 30 | */ |
| 31 | export const mergeAPIErrorHeaders = (error: APIError): Headers => { |
| 32 | const merged = new Headers(error.headers as HeadersInit | undefined); |
| 33 | const hidden = (error as unknown as Record<symbol, unknown>)[kAPIErrorHeaders]; |
| 34 | if (hidden instanceof Headers) { |
| 35 | const cookies = typeof hidden.getSetCookie === "function" ? hidden.getSetCookie() : []; |
| 36 | hidden.forEach((value, key) => { |
| 37 | if (key.toLowerCase() !== "set-cookie") { |
| 38 | merged.set(key, value); |
| 39 | } |
| 40 | }); |
| 41 | for (const cookie of cookies) { |
| 42 | merged.append("set-cookie", cookie); |
| 43 | } |
| 44 | } |
| 45 | return merged; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Typed failure of an effectified `auth.api.*` call. |
| 50 | * |
| 51 | * Better Auth throws `APIError` from `better-call`; this error carries the |
| 52 | * same information as first-class fields: |
| 53 | * |
| 54 | * - `status` — the better-call status KEY, e.g. `"UNAUTHORIZED"` (a string) |
| 55 | * - `statusCode` — the numeric HTTP status, e.g. `401` |
| 56 | * - `body` — the JSON error body (`message`, `code`, ...) |
| 57 | * - `headers` — merged response headers, INCLUDING the `set-cookie` |
| 58 | * headers better-call hides on a symbol property of the thrown error |
| 59 | * |
| 60 | * Match on `statusCode` (or `body.code`) — `status` is a string key, not a |
| 61 | * number. |
| 62 | */ |
| 63 | export class BetterAuthApiError extends Data.TaggedError("BetterAuthApiError")<{ |
| 64 | readonly status: string; |
| 65 | readonly statusCode: number; |
| 66 | readonly body: ({ message?: string; code?: string } & Record<string, unknown>) | undefined; |
| 67 | readonly headers: Headers; |
| 68 | readonly cause: APIError; |
| 69 | }> { |
| 70 | static fromAPIError(error: APIError): BetterAuthApiError { |
| 71 | return new BetterAuthApiError({ |
| 72 | status: String(error.status), |
| 73 | statusCode: error.statusCode, |
| 74 | body: error.body as BetterAuthApiError["body"], |
| 75 | headers: mergeAPIErrorHeaders(error), |
| 76 | cause: error, |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | override get message(): string { |
| 81 | return this.body?.message ?? `Better Auth API error (${this.statusCode})`; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Render the error as an HTTP response (status + JSON body + merged |
| 86 | * headers) — for handlers that want to pass the failure through to the |
| 87 | * client exactly as Better Auth would have. |
| 88 | */ |
| 89 | toResponse(): HttpServerResponse.HttpServerResponse { |
| 90 | return HttpServerResponse.fromWeb( |
| 91 | new Response(JSON.stringify(this.body ?? {}), { |
| 92 | status: this.statusCode, |
| 93 | headers: withJsonContentType(this.headers), |
| 94 | }), |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | const withJsonContentType = (headers: Headers): Headers => { |
| 100 | const out = new Headers(headers); |
| 101 | if (!out.has("content-type")) { |
| 102 | out.set("content-type", "application/json"); |
| 103 | } |
| 104 | return out; |
| 105 | }; |
| 106 | |
| 107 | /** Failure of the deploy-time schema migration. */ |
| 108 | export class BetterAuthMigrationError extends Data.TaggedError("BetterAuthMigrationError")<{ |
| 109 | readonly message: string; |
| 110 | readonly cause?: unknown; |
| 111 | }> {} |
| 112 | |
| 113 | /** Failure of a `secondaryStorage` operation. */ |
| 114 | export class BetterAuthStorageError extends Data.TaggedError("BetterAuthStorageError")<{ |
| 115 | readonly message: string; |
| 116 | readonly cause?: unknown; |
| 117 | }> {} |