Skip to content

Commit

Permalink
feat: log CONNECT error response body (#139)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
barjin authored May 22, 2024
1 parent 6c6bf55 commit a8a5dfe
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 109 deletions.
214 changes: 108 additions & 106 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"types": "./dist/index.d.ts",
"type": "module",
"dependencies": {
"got": "^13.0.0",
"got": "^14.2.1",
"header-generator": "^2.1.41",
"http2-wrapper": "^2.2.0",
"mimic-response": "^4.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ export async function proxyHook(options: Options): Promise<void> {
}
}

export class ProxyError extends Error {}

function validateProxyProtocol(protocol: string) {
const isSupported = protocol === 'http:' || protocol === 'https:';

if (!isSupported) {
throw new Error(`Proxy URL protocol "${protocol}" is not supported. Please use HTTP or HTTPS.`);
throw new ProxyError(`Proxy URL protocol "${protocol}" is not supported. Please use HTTP or HTTPS.`);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/resolve-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { URL } from 'node:url';
import { type Headers } from 'got';
import { auto, type ResolveProtocolConnectFunction, type ResolveProtocolFunction } from 'http2-wrapper';
import QuickLRU from 'quick-lru';
import { ProxyError } from './hooks/proxy.js';

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

request.once('connect', (response, socket, head) => {
if (response.statusCode !== 200 || head.length > 0) {
reject(new Error(`Proxy responded with ${response.statusCode} ${response.statusMessage}: ${head.length} bytes`));
reject(new ProxyError(`Proxy responded with ${response.statusCode} ${response.statusMessage}: ${head.length} bytes.
Below is the first 100 bytes of the proxy response body:
${head.toString('utf8', 0, 100)}`, { cause: head.toString('utf8') }));
socket.destroy();
return;
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"target": "ES2022",
"verbatimModuleSyntax": true,
"incremental": false,
"lib": ["DOM", "ESNext"],
},
"include": [
"src"
Expand Down

0 comments on commit a8a5dfe

Please sign in to comment.