Skip to content

Commit

Permalink
Abort error (#440)
Browse files Browse the repository at this point in the history
Abort error
  • Loading branch information
wheresrhys authored Jul 21, 2019
2 parents 6b445a9 + 6b1e78a commit 2378427
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 51 deletions.
66 changes: 28 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"node": ">=4.0.0"
},
"devDependencies": {
"abort-controller": "^3.0.0",
"babel-cli": "^6.1.2",
"babel-loader": "^7.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
Expand Down
15 changes: 14 additions & 1 deletion src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ const responseBuilder = require('./response-builder');
const requestUtils = require('./request-utils');
const FetchMock = {};

// see https://heycam.github.io/webidl/#aborterror for the standardised interface
// Note that this differs slightly from node-fetch
class AbortError extends Error {
constructor() {
super(...arguments);
this.name = 'AbortError';
this.message = 'The operation was aborted.';

// Do not include this class in the stacktrace
Error.captureStackTrace(this, this.constructor);
}
}

const resolve = async (
{ response, responseIsFetch = false },
url,
Expand Down Expand Up @@ -49,7 +62,7 @@ FetchMock.fetchHandler = function(url, options, request) {
return new this.config.Promise((res, rej) => {
if (options && options.signal) {
const abort = () => {
rej(new Error(`URL '${url}' aborted.`));
rej(new AbortError());
done();
};
if (options.signal.aborted) {
Expand Down
2 changes: 1 addition & 1 deletion test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('request types that only work in the browser', function() {
});
});

require('./runner')(fetchMock, window, window.fetch);
require('./runner')(fetchMock, window, window.fetch, window.AbortController);

describe('no real fetch', function() {
it('should cope when there is no global fetch defined', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/runner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const chai = require('chai');
const expect = chai.expect;

module.exports = (fetchMock, theGlobal, fetch) => {
module.exports = (fetchMock, theGlobal, fetch, AbortController) => {
describe('fetch-mock', () => {
it('exports itself as a property', () => {
expect(fetchMock.fetchMock).to.equal(fetchMock);
Expand All @@ -16,6 +16,6 @@ module.exports = (fetchMock, theGlobal, fetch) => {
require('./specs/repeat.test')(fetchMock);
require('./specs/custom-implementations.test')(fetchMock);
require('./specs/options.test')(fetchMock, theGlobal, fetch);
require('./specs/abortable.test')(fetchMock, fetch);
require('./specs/abortable.test')(fetchMock, AbortController);
});
};
7 changes: 6 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ describe('nodejs tests', () => {
server.close();
});

require('./runner')(fetchMock, global, require('node-fetch'));
require('./runner')(
fetchMock,
global,
require('node-fetch'),
require('abort-controller')
);

describe('support for nodejs body types', () => {
afterEach(() => fetchMock.reset());
Expand Down
14 changes: 6 additions & 8 deletions test/specs/abortable.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const chai = require('chai');
const expect = chai.expect;

module.exports = fetchMock => {
if (typeof AbortController === 'undefined') {
return;
}

describe('abortable fetch', () => {
module.exports = (fetchMock, AbortController) => {
(AbortController ? describe : describe.skip)('abortable fetch', () => {
let fm;
beforeEach(() => {
fm = fetchMock.createInstance();
Expand All @@ -29,7 +25,8 @@ module.exports = fetchMock => {
signal: controller.signal
});
} catch (error) {
expect(error.message).to.equal("URL 'http://it.at.there/' aborted.");
expect(error.name).to.equal('AbortError');
expect(error.message).to.equal('The operation was aborted.');
}
});

Expand All @@ -44,7 +41,8 @@ module.exports = fetchMock => {
signal: controller.signal
});
} catch (error) {
expect(error.message).to.equal("URL 'http://it.at.there/' aborted.");
expect(error.name).to.equal('AbortError');
expect(error.message).to.equal('The operation was aborted.');
}
});

Expand Down

0 comments on commit 2378427

Please sign in to comment.