histoire
Version:
A fast story book powered by Vite
31 lines (30 loc) • 1.32 kB
JavaScript
import { HISTOIRE_ERROR_CODES, HistoireSdkError } from '@histoire/protocol';
import { hasUnconfirmedCleanup } from '../runtime/cleanup.js';
import { ExecutionError } from '../runtime/execution-types.js';
/** Maps domain errors once while preserving quarantine markers for project close. */
export function toSdkExecutionError(error) {
if (hasUnconfirmedCleanup(error) || error instanceof HistoireSdkError)
return error;
if (error instanceof ExecutionError)
return new HistoireSdkError(error.code === 'UNAVAILABLE' ? 'CAPABILITY_UNAVAILABLE' : error.code === 'CANCELLED' ? 'CANCELLED' : 'QUEUE_FULL', error.message);
const input = error;
if (HISTOIRE_ERROR_CODES.includes(input?.code))
return new HistoireSdkError(input.code, input.message ?? 'Histoire operation failed');
return error;
}
/** Cancellation settles only after canonical scheduler has joined owned cleanup. */
export async function awaitSdkExecution(handle, signal) {
const cancel = () => handle.cancel();
signal?.addEventListener('abort', cancel, { once: true });
if (signal?.aborted)
cancel();
try {
return await handle.result;
}
catch (error) {
throw toSdkExecutionError(error);
}
finally {
signal?.removeEventListener('abort', cancel);
}
}