Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror superagent behaviour in call back for creating errors #14

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
repl-mike-roest marked this conversation as resolved.
Show resolved Hide resolved
}
}
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);
repl-mike-roest marked this conversation as resolved.
Show resolved Hide resolved
}
}

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