26 lines
1019 B
TypeScript
26 lines
1019 B
TypeScript
|
|
/**
|
|||
|
|
* Provider 错误分类(实施设计 5:统一映射为六类)。
|
|||
|
|
*
|
|||
|
|
* Provider 抛出的错误会被 Core 归一化成这六种之一;界面按 kind 决定提示与是否可重试,
|
|||
|
|
* 不用话术圆场(实施设计 2.5「失败显式」)。
|
|||
|
|
*/
|
|||
|
|
export const ERROR_KINDS = ['invalid_argument', 'forbidden', 'conflict', 'rate_limited', 'unavailable', 'unknown_result'] as const;
|
|||
|
|
|
|||
|
|
export type ErrorKind = (typeof ERROR_KINDS)[number];
|
|||
|
|
|
|||
|
|
export function isErrorKind(value: unknown): value is ErrorKind {
|
|||
|
|
return typeof value === 'string' && (ERROR_KINDS as readonly string[]).includes(value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** Provider 应抛这个错误,而不是裸 Error——裸 Error 会被 Core 归为 unknown_result */
|
|||
|
|
export class ProviderError extends Error {
|
|||
|
|
readonly kind: ErrorKind;
|
|||
|
|
readonly detail?: unknown;
|
|||
|
|
|
|||
|
|
constructor(kind: ErrorKind, message: string, detail?: unknown) {
|
|||
|
|
super(message);
|
|||
|
|
this.name = 'ProviderError';
|
|||
|
|
this.kind = kind;
|
|||
|
|
if (detail !== undefined) this.detail = detail;
|
|||
|
|
}
|
|||
|
|
}
|