Browse Source

Expose CancelError (#378)

no-retry
Damien Lebrun 7 years ago
committed by Sindre Sorhus
parent
commit
b55f79e99d
  1. 2
      index.js
  2. 20
      readme.md
  3. 32
      test/cancel.js

2
index.js

@ -640,4 +640,6 @@ got.UnsupportedProtocolError = class extends StdError {
}
};
got.CancelError = PCancelable.CancelError;
module.exports = got;

20
readme.md

@ -277,6 +277,10 @@ When server redirects you more than 10 times. Includes a `redirectUrls` property
When given an unsupported protocol.
#### got.CancelError
When the request is aborted with `.cancel()`.
## Aborting the request
@ -296,6 +300,22 @@ request.catch(err => {
request.cancel();
```
Or
```js
const request = got(url, options);
request.catch(err => {
if (err instanceof got.CancelError) {
// Handle cancelation
}
// Handle other errors
});
request.cancel();
```
## Proxies

32
test/cancel.js

@ -85,3 +85,35 @@ test('cancel immediately', async t => {
await t.throws(p);
await t.notThrows(aborted, 'Request finished instead of aborting.');
});
test('recover from cancelation using cancelable promise attribute', async t => {
// Canceled before connection started
const p = got('http://example.com');
const recover = p.catch(err => {
if (p.canceled) {
return;
}
throw err;
});
p.cancel();
await t.notThrows(recover);
});
test('recover from cancellation using error instance', async t => {
// Canceled before connection started
const p = got('http://example.com');
const recover = p.catch(err => {
if (err instanceof got.CancelError) {
return;
}
throw err;
});
p.cancel();
await t.notThrows(recover);
});

Loading…
Cancel
Save