Skip to content

Commit

Permalink
fix(server): set reason in ServerAdapterRequestAbortSignal to get…
Browse files Browse the repository at this point in the history
… a proper error message when aborted (#1218)
  • Loading branch information
ardatan authored Mar 21, 2024
1 parent dfb4290 commit 1443f93
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-months-hunt.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions packages/server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -563,7 +564,7 @@ export function handleAbortSignalAndPromiseResponse(
if (isPromise(response$) && abortSignal) {
const deferred$ = createDeferredPromise<Response>();
abortSignal.addEventListener('abort', function abortSignalFetchErrorHandler() {
deferred$.reject(new DOMException('Aborted', 'AbortError'));
deferred$.reject(abortSignal.reason);
});
response$
.then(function fetchSuccessHandler(res) {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/test/adapter.fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 1443f93

Please sign in to comment.