From c9610eca9907ef50a06a34e9b9b59414a274d216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20B=C3=A4r?= Date: Thu, 16 May 2024 13:47:22 +0200 Subject: [PATCH] feat: throw ProxyError with `cause` --- src/hooks/proxy.ts | 4 +++- src/resolve-protocol.ts | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hooks/proxy.ts b/src/hooks/proxy.ts index e16f98a..3ca15bd 100644 --- a/src/hooks/proxy.ts +++ b/src/hooks/proxy.ts @@ -24,11 +24,13 @@ export async function proxyHook(options: Options): Promise { } } +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.`); } } diff --git a/src/resolve-protocol.ts b/src/resolve-protocol.ts index 8410ab2..c6c5bab 100644 --- a/src/resolve-protocol.ts +++ b/src/resolve-protocol.ts @@ -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((resolve, reject) => { let host = `${options.host}:${options.port}`; @@ -42,11 +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)} -`)); +${head.toString('utf8', 0, 100)}`, { cause: head.toString('utf8') })); socket.destroy(); return; }