From edcc1f97ba0fff800e11399e0df99fcbf3da0ceb Mon Sep 17 00:00:00 2001 From: Mike Roest Date: Thu, 19 Oct 2023 13:11:40 -0600 Subject: [PATCH 1/5] include error conversion like superagent --- src/index.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 1c9ec9a..36a673a 100644 --- a/src/index.js +++ b/src/index.js @@ -90,13 +90,37 @@ function callback(err, res) { return req._retry(); }, delay); } - const fn = this._callback; this.clearTimeout(); + if (!err) { + try { + if (!this._isResponseOK(res)) { + let message = 'Unsuccessful HTTP response'; + if (res) { + message = http.STATUS_CODES[res.status] || message; + } + + err = new Error(message); + err.status = res ? res.status : undefined; + } + } catch (e) { + error = e; + error.status = error.status || (res ? res.status : undefined); + } + } + if (!err) { + return fn(null, res); + } + + err.response = res; + + if (err) { if (this._maxRetries) err.retries = this._retries - 1; - this.emit("error", err); + if (err && this.listeners('error').length > 0) { + this.emit('error', err); + } } fn(err, res); From 125bb127af9b84c0a9bf11258cdcfcce99a41a13 Mon Sep 17 00:00:00 2001 From: Mike Roest Date: Thu, 19 Oct 2023 13:14:54 -0600 Subject: [PATCH 2/5] include http --- src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 36a673a..eec0a3c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +const http = require('http'); /** * Add to the request prototype. */ @@ -105,8 +106,8 @@ function callback(err, res) { err.status = res ? res.status : undefined; } } catch (e) { - error = e; - error.status = error.status || (res ? res.status : undefined); + err = e; + err.status = err.status || (res ? res.status : undefined); } } if (!err) { From dea91bfda31367ce31e90325bf25142eaca4bfeb Mon Sep 17 00:00:00 2001 From: Mike Roest Date: Thu, 19 Oct 2023 13:34:31 -0600 Subject: [PATCH 3/5] fix lint and update tests for new behaviour --- src/index.js | 9 ++++----- tests/test.js | 11 ++++++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index eec0a3c..fdfb9d0 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -const http = require('http'); +const http = require("http"); /** * Add to the request prototype. */ @@ -97,7 +97,7 @@ function callback(err, res) { if (!err) { try { if (!this._isResponseOK(res)) { - let message = 'Unsuccessful HTTP response'; + let message = "Unsuccessful HTTP response"; if (res) { message = http.STATUS_CODES[res.status] || message; } @@ -116,11 +116,10 @@ function callback(err, res) { err.response = res; - if (err) { if (this._maxRetries) err.retries = this._retries - 1; - if (err && this.listeners('error').length > 0) { - this.emit('error', err); + if (err && this.listeners("error").length > 0) { + this.emit("error", err); } } diff --git a/tests/test.js b/tests/test.js index 104b46f..dfe782b 100644 --- a/tests/test.js +++ b/tests/test.js @@ -89,7 +89,9 @@ describe("superagent-retry-delay", function () { .end(function (err, res) { res.status.should.eql(404); requests.should.eql(3); - done(err); + err.response.status.should.eql(404); + err.message.should.eql("Not Found"); + done(); }); }); @@ -100,7 +102,9 @@ describe("superagent-retry-delay", function () { .end(function (err, res) { res.status.should.eql(404); requests.should.eql(3); - done(err); + err.response.status.should.eql(404); + err.message.should.eql("Not Found"); + done(); }); }); @@ -428,7 +432,8 @@ describe("superagent-retry-delay", function () { .end(function (err, res) { res.text.should.eql("Misdirected Request"); requests.should.eql(2); - done(err); + err.message.should.eql("Misdirected Request"); + done(); }); }); From 0bcd3b2485145ca5a65336c9782bd78aa852c0ec Mon Sep 17 00:00:00 2001 From: Mike Roest Date: Mon, 23 Oct 2023 07:45:42 -0600 Subject: [PATCH 4/5] Add tests for new functionality --- tests/test.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/test.js b/tests/test.js index dfe782b..58feef1 100644 --- a/tests/test.js +++ b/tests/test.js @@ -11,6 +11,61 @@ const http = require("http"); http.globalAgent.maxSockets = 2000; describe("superagent-retry-delay", function () { + describe("error synthesizing", function () { + let requests = 0; + const port = 10410; + const app = express(); + let server; + before(function (done) { + app.get("/", function (req, res, next) { + requests++; + res.sendStatus(404); + }); + + server = app.listen(port, done); + }); + + afterEach(function () { + requests = 0; + }); + + it("emits error when listener is attached", function (done) { + let received = false; + agent + .get("http://localhost:" + port) + .on('error', (err) => { + received = true; + }) + .end(function (err, res) { + res.text.should.eql("Not Found"); + err.response.status.should.eql(404); + err.status.should.eql(404); + err.message.should.eql("Not Found"); + requests.should.eql(1); + received.should.eql(true); + done(); + }); + }); + + it("catches errors in the _isResponseOK and returns", function (done) { + const oldHandler = agent.Request.prototype._isResponseOK; + agent.Request.prototype._isResponseOK = (res) => { throw new Error("_isResponseOK callback error");}; + agent + .get("http://localhost:" + port) + .end(function (err, res) { + res.text.should.eql("Not Found"); + err.response.status.should.eql(404); + err.message.should.eql("_isResponseOK callback error"); + requests.should.eql(1); + agent.Request.prototype._isResponseOK = oldHandler; + done(); + }); + }); + + after(function (done) { + server.close(done); + }); + }); describe("not-errors", function () { let requests = 0; const port = 10410; From 01926c9312f07470b9f97b01b4b811c60bb8d1c5 Mon Sep 17 00:00:00 2001 From: Mike Roest Date: Mon, 23 Oct 2023 07:47:31 -0600 Subject: [PATCH 5/5] lint --- tests/test.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/test.js b/tests/test.js index 58feef1..7927488 100644 --- a/tests/test.js +++ b/tests/test.js @@ -33,11 +33,11 @@ describe("superagent-retry-delay", function () { let received = false; agent .get("http://localhost:" + port) - .on('error', (err) => { + .on("error", (err) => { received = true; }) .end(function (err, res) { - res.text.should.eql("Not Found"); + res.text.should.eql("Not Found"); err.response.status.should.eql(404); err.status.should.eql(404); err.message.should.eql("Not Found"); @@ -49,17 +49,17 @@ describe("superagent-retry-delay", function () { it("catches errors in the _isResponseOK and returns", function (done) { const oldHandler = agent.Request.prototype._isResponseOK; - agent.Request.prototype._isResponseOK = (res) => { throw new Error("_isResponseOK callback error");}; - agent - .get("http://localhost:" + port) - .end(function (err, res) { - res.text.should.eql("Not Found"); - err.response.status.should.eql(404); - err.message.should.eql("_isResponseOK callback error"); - requests.should.eql(1); - agent.Request.prototype._isResponseOK = oldHandler; - done(); - }); + agent.Request.prototype._isResponseOK = (res) => { + throw new Error("_isResponseOK callback error"); + }; + agent.get("http://localhost:" + port).end(function (err, res) { + res.text.should.eql("Not Found"); + err.response.status.should.eql(404); + err.message.should.eql("_isResponseOK callback error"); + requests.should.eql(1); + agent.Request.prototype._isResponseOK = oldHandler; + done(); + }); }); after(function (done) {