aps-agent/server/app/errors.ts

19 lines
876 B
TypeScript
Raw Permalink Normal View History

/** 应用层错误:入口层据此转成协议里的 ErrorEnvelope(见 docs/spec/protocol.md 第 8 节) */
export class AppError extends Error {
readonly code: string;
readonly status: number;
readonly retryable: boolean;
constructor(code: string, message: string, options: { status?: number; retryable?: boolean } = {}) {
super(message);
this.name = 'AppError';
this.code = code;
this.status = options.status ?? 400;
this.retryable = options.retryable ?? false;
}
}
export const badRequest = (message: string): AppError => new AppError('invalid_request', message, { status: 400 });
export const notFound = (what: string): AppError => new AppError('not_found', `${what} 不存在`, { status: 404 });
export const conflict = (message: string, retryable = true): AppError =>
new AppError('conflict', message, { status: 409, retryable });