Skip to content

Commit

Permalink
Merge pull request #14 from repl-mike-roest/fix-errors-on-callback
Browse files Browse the repository at this point in the history
Mirror superagent behaviour in call back for creating errors
  • Loading branch information
luispabon authored Nov 2, 2023
2 parents 9b3dd9e + 01926c9 commit 35f82c5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
28 changes: 26 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const http = require("http");
/**
* Add to the request prototype.
*/
Expand Down Expand Up @@ -90,13 +91,36 @@ 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) {
err = e;
err.status = err.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);
Expand Down
66 changes: 63 additions & 3 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,7 +144,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();
});
});

Expand All @@ -100,7 +157,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();
});
});

Expand Down Expand Up @@ -428,7 +487,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();
});
});

Expand Down

0 comments on commit 35f82c5

Please sign in to comment.