diff --git a/.changeset/sharp-months-hunt.md b/.changeset/sharp-months-hunt.md new file mode 100644 index 00000000000..f30c3d88b70 --- /dev/null +++ b/.changeset/sharp-months-hunt.md @@ -0,0 +1,5 @@ +--- +"@whatwg-node/server": patch +--- + +Set \`reason\` in \`ServerAdapterRequestAbortSignal\` to get a proper error when the request got aborted by the client diff --git a/packages/server/src/utils.ts b/packages/server/src/utils.ts index 3d3a270440c..4e68e25a4a3 100644 --- a/packages/server/src/utils.ts +++ b/packages/server/src/utils.ts @@ -90,11 +90,12 @@ export class ServerAdapterRequestAbortSignal extends EventTarget implements Abor throwIfAborted(): void { if (this.aborted) { - throw new DOMException('Aborted', 'AbortError'); + throw this.reason; } } sendAbort() { + this.reason = new DOMException('This operation was aborted', 'AbortError'); this.aborted = true; this.dispatchEvent(new Event('abort')); } @@ -563,7 +564,7 @@ export function handleAbortSignalAndPromiseResponse( if (isPromise(response$) && abortSignal) { const deferred$ = createDeferredPromise(); abortSignal.addEventListener('abort', function abortSignalFetchErrorHandler() { - deferred$.reject(new DOMException('Aborted', 'AbortError')); + deferred$.reject(abortSignal.reason); }); response$ .then(function fetchSuccessHandler(res) { diff --git a/packages/server/test/adapter.fetch.spec.ts b/packages/server/test/adapter.fetch.spec.ts index e5c3733c39c..04fda5a031c 100644 --- a/packages/server/test/adapter.fetch.spec.ts +++ b/packages/server/test/adapter.fetch.spec.ts @@ -223,6 +223,6 @@ describe('adapter.fetch', () => { const signal = controller.signal; const promise = adapter.fetch('http://localhost', { signal }); controller.abort(); - await expect(promise).rejects.toThrow('Aborted'); + await expect(promise).rejects.toThrow('This operation was aborted'); }); });