Skip to content

Emit errors when request is aborted #778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ interface HttpRequest {
// - in browsers: a TypedArray, a DataView a Blob, or null.
// - in Node.js: a Buffer, a ReadableStream, or null.
send(body: any): Promise<HttpResponse>;

// Aborts the request if it's currently in progress. If so, `send` should reject
// with an error of type DOMException, with name AbortError.
abort(): Promise<void>;

// Return an environment specific object, e.g. the XMLHttpRequest object in browsers.
Expand Down
1 change: 1 addition & 0 deletions lib/browser/FetchHttpStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class FetchRequest implements HttpRequest {
}

abort(): Promise<void> {
// Note: When abort() is called, the fetch() promise rejects with an Error of type DOMException, with name AbortError.
this._controller.abort()
return Promise.resolve()
}
Expand Down
5 changes: 5 additions & 0 deletions lib/browser/XHRHttpStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ class XHRRequest implements HttpRequest {
reject(err)
}

this._xhr.onabort = () => {
reject(new DOMException('Request was aborted', 'AbortError'))
}

this._xhr.send(body)
})
}

abort(): Promise<void> {
// Note: Calling abort() triggers the `abort` event, but no `error` event.
this._xhr.abort()
return Promise.resolve()
}
Expand Down
4 changes: 3 additions & 1 deletion lib/node/NodeHttpStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ class Request implements HttpRequest {
}

abort() {
if (this._request != null) this._request.abort()
// Note: The destroy() method will trigger an `error` event with the provided error.
if (this._request != null)
this._request.destroy(new DOMException('Request was aborted', 'AbortError'))
return Promise.resolve()
}

Expand Down
3 changes: 3 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export interface HttpRequest {
setProgressHandler(handler: HttpProgressHandler): void
// TODO: Should this be something like { value: unknown, size: number }?
send(body?: SliceType): Promise<HttpResponse>

// Aborts the request if it's currently in progress. If so, `send` should reject
// with an error of type DOMException, with name AbortError.
abort(): Promise<void>

// Return an environment specific object, e.g. the XMLHttpRequest object in browsers.
Expand Down
Loading