Skip to content

Commit

Permalink
Merge pull request #91 from Financial-Times/FTDCS-213-throw-errors-in…
Browse files Browse the repository at this point in the history
…-getdata

Listen to error event and throw errors from getData
  • Loading branch information
alexmuller committed Jun 29, 2022
2 parents 7ace8dc + 066ee9d commit 0d578c8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ parseData: function (data) {

* `stop()` - Stops polling

* `getData()` - Returns the last set of data retrieved from the server (post-processed if `parseData` function exists)
* `getData()` - Returns the last set of data retrieved from the server (post-processed if `parseData` function exists). This will throw an `HttpError` if the most recent fetch received an error.

#### Events

Expand Down
11 changes: 11 additions & 0 deletions src/poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ module.exports = EventEmitter => {
if (config.autostart) {
this.start ({initialRequest: true});
}

// We must listen to the error event to prevent throwing when we receive an HTTP error
// https://nodejs.org/docs/latest-v16.x/api/events.html#error-events
this.on('error', (error) => {
this.error = error;
});
}

isRunning () {
Expand Down Expand Up @@ -83,6 +89,7 @@ module.exports = EventEmitter => {
.then ((response) => {
const latency = new Date () - time;
if (response.ok) {
this.error = undefined;
this.emit ('ok', response, latency);
} else {
throw new errors.HttpError({url:this.url, method:this.options.method || 'GET', response});
Expand All @@ -103,6 +110,10 @@ module.exports = EventEmitter => {
}

getData () {
if (this.error) {
throw this.error;
}

return this.data;
}
};
Expand Down
53 changes: 53 additions & 0 deletions tests/poller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,59 @@ describe ('Poller', function () {

});

it ('Should throw from getData when fetch has received an HTTP error', function (done) {

const ft = nock ('http://example.com')
.get ('/')
.reply (503, {});

const p = new Poller({
url: 'http://example.com'
});

p.fetch ();

setTimeout (function () {
expect (ft.isDone ()).to.be.true; // ensure Nock has been used
expect (function () {
p.getData ();
}).to.throw ('HTTP Error 503 Service Unavailable');
done ();
}, 10);

});

it ('Should return data from getData when fetch has received an HTTP error followed by a success', function (done) {

const ft = nock ('http://example.com');

const p = new Poller({
url: 'http://example.com'
});

ft.get ('/').reply (503, '<h1>error</h1>');

p.fetch ();

setTimeout (function () {
expect (ft.isDone ()).to.be.true; // ensure Nock has been used
expect (function () {
p.getData ();
}).to.throw ('HTTP Error 503 Service Unavailable');

ft.get ('/').reply (200, '<h1>website</h1>')

p.fetch()

setTimeout (function () {
expect (p.getData ()).to.equal ('<h1>website</h1>');
done ();
}, 10);

}, 10);

});

it ('Should annotate the polling response with latency information', function (done) {

const ft = nock ('http://example.com')
Expand Down

0 comments on commit 0d578c8

Please sign in to comment.