Skip to content

Commit

Permalink
Print status message when proxy server rejects
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Nov 15, 2022
1 parent 51eeaf5 commit 7a9a46a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion source/proxies/h1-over-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const createConnection = (self, options, callback) => {
const statusCode = headers[':status'];

if (statusCode !== 200) {
callback(new UnexpectedStatusCodeError(statusCode));
callback(new UnexpectedStatusCodeError(statusCode, ''));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion source/proxies/h2-over-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getStream = request => new Promise((resolve, reject) => {
socket.unshift(head);

request.off('error', reject);
resolve([socket, response.statusCode]);
resolve([socket, response.statusCode, response.statusMessage]);
};

request.once('error', reject);
Expand Down
2 changes: 1 addition & 1 deletion source/proxies/h2-over-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Http2OverHttp2 extends Http2OverHttpX {
const stream = await globalAgent.request(proxyOptions.url, proxyOptions, headers);
const statusCode = await getStatusCode(stream);

return [stream, statusCode];
return [stream, statusCode, ''];
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/proxies/h2-over-hx.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Http2OverHttpX extends Agent {
async createConnection(origin, options) {
const authority = `${origin.hostname}:${origin.port || 443}`;

const [stream, statusCode] = await this._getProxyStream(authority);
const [stream, statusCode, statusMessage] = await this._getProxyStream(authority);
if (statusCode !== 200) {
throw new UnexpectedStatusCodeError(statusCode);
throw new UnexpectedStatusCodeError(statusCode, statusMessage);
}

if (this.proxyOptions.raw) {
Expand Down
5 changes: 3 additions & 2 deletions source/proxies/unexpected-status-code-error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

class UnexpectedStatusCodeError extends Error {
constructor(statusCode) {
super(`The proxy server rejected the request with status code ${statusCode}`);
constructor(statusCode, statusMessage = '') {
super(`The proxy server rejected the request with status code ${statusCode} (${statusMessage || 'empty status message'})`);
this.statusCode = statusCode;
this.statusMessage = statusMessage;
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ test.serial('HTTPS over HTTP/2 - 403', wrapper, async (t, server) => {
request.end();

const error = await pEvent(request, 'error');
t.is(error.message, 'The proxy server rejected the request with status code 403');
t.is(error.message, 'The proxy server rejected the request with status code 403 (empty status message)');

http2.globalAgent.destroy();
});
Expand Down Expand Up @@ -362,7 +362,7 @@ test.serial('HTTP over HTTP/2 - 403', async t => {
request.end();

const error = await pEvent(request, 'error');
t.is(error.message, 'The proxy server rejected the request with status code 403');
t.is(error.message, 'The proxy server rejected the request with status code 403 (empty status message)');

http2.globalAgent.destroy();
});
Expand Down Expand Up @@ -534,7 +534,7 @@ test.serial('HTTP/2 over HTTPS - 403', wrapper, async (t, server) => {
request.end();

const error = await pEvent(request, 'error');
t.is(error.message, 'The proxy server rejected the request with status code 403');
t.is(error.message, 'The proxy server rejected the request with status code 403 (Forbidden)');

agent.destroy();
http2.globalAgent.destroy();
Expand Down Expand Up @@ -700,7 +700,7 @@ test.serial('HTTP/2 over HTTP - 403', wrapper, async (t, server) => {
request.end();

const error = await pEvent(request, 'error');
t.is(error.message, 'The proxy server rejected the request with status code 403');
t.is(error.message, 'The proxy server rejected the request with status code 403 (Forbidden)');

agent.destroy();
http2.globalAgent.destroy();
Expand Down Expand Up @@ -868,7 +868,7 @@ test.serial('HTTP/2 over HTTP/2 - 403', wrapper, async (t, server) => {
request.end();

const error = await pEvent(request, 'error');
t.is(error.message, 'The proxy server rejected the request with status code 403');
t.is(error.message, 'The proxy server rejected the request with status code 403 (empty status message)');

http2.globalAgent.destroy();
});
Expand Down

0 comments on commit 7a9a46a

Please sign in to comment.