Skip to content

Commit

Permalink
feat: throw ProxyError with cause
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin committed May 16, 2024
1 parent 30d4924 commit c9610ec
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
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
6 changes: 3 additions & 3 deletions 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,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;
}
Expand Down

0 comments on commit c9610ec

Please sign in to comment.