Files
frontend/src/shared/api/problem-details.ts
T
codex-bot 32e8fe9402
Frontend verify / commits (push) Skipped
Frontend verify / security (push) Successful in 9s
Frontend verify / verify (push) Failing after 6m30s
refactor: derive api error codes from contract (WealthLedger/backend#117) (#123)
2026-10-28 17:31:22 +03:00

45 lines
1.4 KiB
TypeScript

import type { components } from "./generated/schema";
enum ApiErrorCode {
BankDependentRefunds = "BANK_DEPENDENT_REFUNDS",
BankIntentConflict = "BANK_INTENT_CONFLICT",
BankNotFound = "BANK_NOT_FOUND",
BankRefundLimit = "BANK_REFUND_LIMIT",
BankVersionConflict = "BANK_VERSION_CONFLICT",
BankWriteConflict = "BANK_WRITE_CONFLICT",
BondTickerAmbiguous = "BOND_TICKER_AMBIGUOUS",
InvalidOperationsCursor = "INVALID_OPERATIONS_CURSOR",
InvalidTbankToken = "INVALID_TBANK_TOKEN",
NotFound = "NOT_FOUND",
StockTickerAmbiguous = "STOCK_TICKER_AMBIGUOUS",
TbankNotConnected = "TBANK_NOT_CONNECTED",
TbankUnavailable = "TBANK_UNAVAILABLE",
Unauthorized = "UNAUTHORIZED",
}
/**
Compile-time drift guard: collapses to `never` when any enum member is absent
from the generated ProblemDetails contract, so every call site fails to build.
*/
type ContractApiErrorCode
= Exclude<
`${ApiErrorCode}`,
components["schemas"]["ProblemDetailsDto"]["code"]
> extends never ? ApiErrorCode : never;
function apiErrorCode(error: unknown): string | undefined {
return typeof error === "object" && error !== null && "code" in error
? String(error.code)
: undefined;
}
// Incoming codes are an open wire string; unknown values retain their fallback.
function isApiErrorCode(
value: unknown,
expected: ContractApiErrorCode,
): boolean {
return value === expected;
}
export { ApiErrorCode, apiErrorCode, isApiErrorCode };