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

@@ -1,27 +1,9 @@
import { GSDError, exitCodeFor } from './errors.js';
import { GSDToolsError } from './gsd-tools-error.js';
import { errorMessage, toFailureSignal } from './query-failure-classification.js';
import { toToolsErrorFromUnknown } from './query-tools-error-factory.js';
import type { GSDToolsError } from './gsd-tools-error.js';
/**
* Module owning projection of internal errors to GSDToolsError contract.
*/
export function toGSDToolsError(command: string, args: string[], err: unknown): GSDToolsError {
if (err instanceof GSDError) {
return GSDToolsError.failure(
err.message,
command,
args,
exitCodeFor(err.classification),
'',
{ cause: err },
);
}
const msg = errorMessage(err);
const signal = toFailureSignal(err);
if (signal.kind === 'timeout') {
return GSDToolsError.timeout(msg, command, args, '', signal.timeoutMs, err instanceof Error ? { cause: err } : undefined);
}
return GSDToolsError.failure(msg, command, args, 1, '', err instanceof Error ? { cause: err } : undefined);
return toToolsErrorFromUnknown(command, args, err);
}