refactor: centralize query tools error construction in factory
This commit is contained in:
31
sdk/src/query-tools-error-factory.ts
Normal file
31
sdk/src/query-tools-error-factory.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user