Skip to content

Commit

Permalink
Simplify _processResponse error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Dec 30, 2023
1 parent 3d42aec commit 72bc2a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
41 changes: 14 additions & 27 deletions index.js
Expand Up @@ -27,7 +27,8 @@ var RedirectionError = createErrorType(
);
var TooManyRedirectsError = createErrorType(
"ERR_FR_TOO_MANY_REDIRECTS",
"Maximum number of redirects exceeded"
"Maximum number of redirects exceeded",
RedirectionError
);
var MaxBodyLengthExceededError = createErrorType(
"ERR_FR_MAX_BODY_LENGTH_EXCEEDED",
Expand Down Expand Up @@ -62,7 +63,13 @@ function RedirectableRequest(options, responseCallback) {
// React to responses of native requests
var self = this;
this._onNativeResponse = function (response) {
self._processResponse(response);
try {
self._processResponse(response);
}
catch (cause) {
self.emit("error", cause instanceof RedirectionError ?
cause : new RedirectionError({ cause: cause }));
}
};

// Perform the first request
Expand Down Expand Up @@ -280,8 +287,7 @@ RedirectableRequest.prototype._performRequest = function () {
var protocol = this._options.protocol;
var nativeProtocol = this._options.nativeProtocols[protocol];
if (!nativeProtocol) {
this.emit("error", new TypeError("Unsupported protocol " + protocol));
return;
throw new TypeError("Unsupported protocol " + protocol);
}

// If specified, use the agent corresponding to the protocol
Expand Down Expand Up @@ -380,8 +386,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
// RFC7231§6.4: A client SHOULD detect and intervene
// in cyclical redirections (i.e., "infinite" redirection loops).
if (++this._redirectCount > this._options.maxRedirects) {
this.emit("error", new TooManyRedirectsError());
return;
throw new TooManyRedirectsError();
}

// Store the request headers if applicable
Expand Down Expand Up @@ -421,14 +426,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
url.format(Object.assign(currentUrlParts, { host: currentHost }));

// Determine the URL of the redirection
var redirectUrl;
try {
redirectUrl = url.resolve(currentUrl, location);
}
catch (cause) {
this.emit("error", new RedirectionError({ cause: cause }));
return;
}
var redirectUrl = url.resolve(currentUrl, location);

// Create the redirected request
debug("redirecting to", redirectUrl);
Expand Down Expand Up @@ -456,23 +454,12 @@ RedirectableRequest.prototype._processResponse = function (response) {
method: method,
headers: requestHeaders,
};
try {
beforeRedirect(this._options, responseDetails, requestDetails);
}
catch (err) {
this.emit("error", err);
return;
}
beforeRedirect(this._options, responseDetails, requestDetails);
this._sanitizeOptions(this._options);
}

// Perform the redirected request
try {
this._performRequest();
}
catch (cause) {
this.emit("error", new RedirectionError({ cause: cause }));
}
this._performRequest();
};

// Wraps the key/value object of protocols with redirect functionality
Expand Down
12 changes: 9 additions & 3 deletions test/test.js
Expand Up @@ -984,8 +984,12 @@ describe("follow-redirects", function () {
}))
.catch(function (error) {
assert(error instanceof Error);
assert(error instanceof TypeError);
assert.equal(error.message, "Unsupported protocol about:");
assert.equal(error.message, "Redirected request failed: Unsupported protocol about:");

var cause = error.cause;
assert(cause instanceof Error);
assert(cause instanceof TypeError);
assert.equal(cause.message, "Unsupported protocol about:");
});
});
});
Expand Down Expand Up @@ -2153,7 +2157,9 @@ describe("follow-redirects", function () {
.catch(function (error) {
assert(!redirected);
assert(error instanceof Error);
assert.equal(error.message, "no redirects!");
assert.equal(error.message, "Redirected request failed: no redirects!");
assert(error.cause instanceof Error);
assert.equal(error.cause.message, "no redirects!");
});
});

Expand Down

0 comments on commit 72bc2a4

Please sign in to comment.