Skip to content

Commit a8a5dfe

Browse files
authored
feat: log CONNECT error response body (#139)
Connected to apify/apify-proxy#823. On an internal proxy error (such as trying to access an invalid or unresolvable URL - e.g. `https://example.comundefined`), the proxy returns a response with the error message. `got-scraping` only logs the length of the message, which is quite useless for most applications.
1 parent 6c6bf55 commit a8a5dfe

File tree

5 files changed

+117
-109
lines changed

5 files changed

+117
-109
lines changed

package-lock.json

+108-106
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"types": "./dist/index.d.ts",
1717
"type": "module",
1818
"dependencies": {
19-
"got": "^13.0.0",
19+
"got": "^14.2.1",
2020
"header-generator": "^2.1.41",
2121
"http2-wrapper": "^2.2.0",
2222
"mimic-response": "^4.0.0",

src/hooks/proxy.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ export async function proxyHook(options: Options): Promise<void> {
2424
}
2525
}
2626

27+
export class ProxyError extends Error {}
28+
2729
function validateProxyProtocol(protocol: string) {
2830
const isSupported = protocol === 'http:' || protocol === 'https:';
2931

3032
if (!isSupported) {
31-
throw new Error(`Proxy URL protocol "${protocol}" is not supported. Please use HTTP or HTTPS.`);
33+
throw new ProxyError(`Proxy URL protocol "${protocol}" is not supported. Please use HTTP or HTTPS.`);
3234
}
3335
}
3436

src/resolve-protocol.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { URL } from 'node:url';
44
import { type Headers } from 'got';
55
import { auto, type ResolveProtocolConnectFunction, type ResolveProtocolFunction } from 'http2-wrapper';
66
import QuickLRU from 'quick-lru';
7+
import { ProxyError } from './hooks/proxy.js';
78

89
const connect = async (proxyUrl: string, options: tls.ConnectionOptions, callback: () => void) => new Promise<TLSSocket>((resolve, reject) => {
910
let host = `${options.host}:${options.port}`;
@@ -42,8 +43,10 @@ const connect = async (proxyUrl: string, options: tls.ConnectionOptions, callbac
4243

4344
request.once('connect', (response, socket, head) => {
4445
if (response.statusCode !== 200 || head.length > 0) {
45-
reject(new Error(`Proxy responded with ${response.statusCode} ${response.statusMessage}: ${head.length} bytes`));
46+
reject(new ProxyError(`Proxy responded with ${response.statusCode} ${response.statusMessage}: ${head.length} bytes.
4647
48+
Below is the first 100 bytes of the proxy response body:
49+
${head.toString('utf8', 0, 100)}`, { cause: head.toString('utf8') }));
4750
socket.destroy();
4851
return;
4952
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"target": "ES2022",
77
"verbatimModuleSyntax": true,
88
"incremental": false,
9+
"lib": ["DOM", "ESNext"],
910
},
1011
"include": [
1112
"src"

0 commit comments

Comments
 (0)