Establish an error-handling standard that prevents crashes/hangs, preserves root errors, and makes recovery bounded and verifiable.
Guidelines:
catch (err: unknown) and narrow before reading fields.controller.error(err) (not enqueue/continue) and perform deterministic teardown/abort.Example (SSE transform fail propagation + streaming semantics):
return new TransformStream({
transform(chunk, controller) {
try {
// parse/process...
} catch (err) {
// Propagate and abort the SSE pipeline
controller.error(err);
}
}
});
Example (guarded retry with non-replayable body check):
const bodyUnknown = options.body as unknown;
const isBodyStream =
bodyUnknown && typeof bodyUnknown === 'object' &&
(typeof (bodyUnknown as any).getReader === 'function');
const maxAttempts = isBodyStream ? 1 : 2;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
try {
return await undiciDirect(input, { ...options, dispatcher });
} catch (e) {
const code = (e as { code?: string }).code;
if (code === 'ECONNREFUSED' || String(e).includes('fetch failed')) continue;
throw e;
}
}
Enter the URL of a public GitHub repository