errors.ts•1.07 kB
export type AppErrorCode =
| 'INVALID_INPUT'
| 'CONFIG_MISSING'
| 'UPSTREAM_HTTP_ERROR'
| 'UNKNOWN';
export class AppError extends Error {
code: AppErrorCode;
status?: number;
details?: Record<string, unknown>;
constructor(code: AppErrorCode, message: string, opts?: { status?: number; details?: Record<string, unknown> }) {
super(message);
this.name = 'AppError';
this.code = code;
this.status = opts?.status;
this.details = opts?.details;
}
}
export function toMcpError(err: unknown) {
if (err instanceof AppError) {
return { code: err.code, message: sanitize(err.message), status: err.status, details: err.details };
}
const msg = err instanceof Error ? err.message : String(err);
return { code: 'UNKNOWN' as const, message: sanitize(msg) };
}
export function sanitize(input: string): string {
const redactions = [process.env.GITLAB_TOKEN, process.env.JIRA_TOKEN].filter(Boolean) as string[];
let out = input;
for (const secret of redactions) {
out = out.split(secret).join('***');
}
return out;
}