refactor: centralize query tools error construction in factory

This commit is contained in:
Tom Boucher
2026-05-04 20:09:20 -04:00
parent abf7779088
commit 9a469fa05c
4 changed files with 64 additions and 26 deletions

View File

@@ -0,0 +1,31 @@
import { GSDError, exitCodeFor } from './errors.js';
import { GSDToolsError } from './gsd-tools-error.js';
import { errorMessage, toFailureSignal } from './query-failure-classification.js';
export function timeoutToolsError(message: string, command: string, args: string[], stderr = '', timeoutMs?: number): GSDToolsError {
return GSDToolsError.timeout(message, command, args, stderr, timeoutMs);
}
export function failureToolsError(
message: string,
command: string,
args: string[],
exitCode: number | null,
stderr = '',
cause?: unknown,
): GSDToolsError {
return GSDToolsError.failure(message, command, args, exitCode, stderr, cause === undefined ? undefined : { cause });
}
export function toToolsErrorFromUnknown(command: string, args: string[], err: unknown): GSDToolsError {
if (err instanceof GSDError) {
return failureToolsError(err.message, command, args, exitCodeFor(err.classification), '', err);
}
const msg = errorMessage(err);
const signal = toFailureSignal(err);
if (signal.kind === 'timeout') {
return timeoutToolsError(msg, command, args, '', signal.timeoutMs);
}
return failureToolsError(msg, command, args, 1, '', err instanceof Error ? err : undefined);
}