Skip to content

Commit

Permalink
feat: special handling for AbortError (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcataford committed Jul 13, 2022
1 parent f845b7c commit db79164
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -419,6 +419,8 @@ If an error occurs, the promise is rejected with an `error` object containing 3
- `error.request` The request options such as `method`, `url` and `data`
- `error.response` The http response object with `url`, `headers`, and `data`

If the error is due to an `AbortSignal` being used, the resulting `AbortError` is bubbled up to the caller.

## `request.defaults()`

Override or set default options. Example:
Expand Down
1 change: 1 addition & 0 deletions src/fetch-wrapper.ts
Expand Up @@ -127,6 +127,7 @@ export default function fetchWrapper(
})
.catch((error) => {
if (error instanceof RequestError) throw error;
else if (error.name === "AbortError") throw error;

throw new RequestError(error.message, 500, {
request: requestOptions,
Expand Down
47 changes: 47 additions & 0 deletions test/request.test.ts
Expand Up @@ -1011,4 +1011,51 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
expect(mock.done()).toBe(true);
});
});

it("bubbles up AbortError if the request is aborted", () => {
// AbortSignal and AbortController do not exist on
// Node < 15. The main parts of their API have been
// reproduced in the mocks below.
class AbortSignal {
abort = () => {
const e = new Error("");
e.name = "AbortError";
throw e;
};

addEventListener = () => {};
}

class AbortController {
abort = () => {
this.signal.abort();
};
signal = new AbortSignal();
}
const abortController = new AbortController();
const mock = fetchMock.sandbox().post(
"https://api.github.com/repos/octokit-fixture-org/release-assets/releases/tags/v1.0.0",
new Promise(() => {
abortController.abort();
})
);

return request("POST /repos/{owner}/{repo}/releases/tags/{tag}", {
owner: "octokit-fixture-org",
repo: "release-assets",
tag: "v1.0.0",
request: {
fetch: mock,
signal: abortController.signal,
},
headers: {
"content-type": "text/plain",
},
data: stringToArrayBuffer("Hello, world!\n"),
name: "test-upload.txt",
label: "test",
}).catch((error) => {
expect(error.name).toEqual("AbortError");
});
});
});

0 comments on commit db79164

Please sign in to comment.