refactor(errors): migrate decisions to {code,msg}

This commit is contained in:
zetaloop
2026-02-28 07:21:51 +08:00
parent 4e2ee5be54
commit cc24a0cbc3
23 changed files with 157 additions and 165 deletions
+20
View File
@@ -0,0 +1,20 @@
export type ApiError = { code: number; msg: string }
export type ApiDecision = { ok: true } | { ok: false; error: ApiError }
export function isApiError(value: unknown): value is ApiError {
if (typeof value !== "object" || value === null) return false
const v = value as { code?: unknown; msg?: unknown }
return typeof v.code === "number" && typeof v.msg === "string"
}
export function toApiError(err: unknown): ApiError {
if (isApiError(err)) return err
if (err instanceof Error) return { code: 500, msg: err.message }
if (typeof err === "string") return { code: 500, msg: err }
return { code: 500, msg: "Unknown error" }
}
export type ApiResult<T> = { ok: true; data: T } | { ok: false; error: ApiError }