Files
pikasTech-agentrun/src/common/errors.ts
T
2026-05-29 10:52:41 +08:00

29 lines
936 B
TypeScript

import type { FailureKind, JsonRecord } from "./types.js";
export class AgentRunError extends Error {
readonly failureKind: FailureKind;
readonly details: JsonRecord | null;
readonly httpStatus: number;
constructor(failureKind: FailureKind, message: string, options: { httpStatus?: number; details?: JsonRecord } = {}) {
super(message);
this.name = "AgentRunError";
this.failureKind = failureKind;
this.httpStatus = options.httpStatus ?? 400;
this.details = options.details ?? null;
}
}
export function errorToJson(error: unknown): JsonRecord {
if (error instanceof AgentRunError) {
return {
name: error.name,
failureKind: error.failureKind,
message: error.message,
details: error.details,
};
}
if (error instanceof Error) return { name: error.name, message: error.message, stack: error.stack ?? null };
return { name: "UnknownError", message: String(error) };
}