Skip to content

Commit 9d0645b

Browse files
committed
Improve error handling
1 parent 5b2a30c commit 9d0645b

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/api.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import express, { Request, Response } from 'express';
2-
import { request as httpsRequest } from 'https';
3-
import { request as httpRequest } from 'http';
2+
import {
3+
request as httpsRequest,
4+
RequestOptions as HttpsRequestOptions,
5+
} from 'https';
6+
import {
7+
request as httpRequest,
8+
RequestOptions as HttpRequestOptions,
9+
} from 'http';
410
import { PassThrough } from 'stream';
511
import {
612
formatRequestBody,
@@ -22,8 +28,9 @@ export function startServer({ cliOptions }: { cliOptions: CliOptions }) {
2228
}
2329

2430
// Setup options for the outbound request
25-
const options = {
31+
const options: HttpRequestOptions | HttpsRequestOptions = {
2632
method: req.method,
33+
// Headers: override the host header for the target.
2734
headers: { ...req.headers, host: targetUrl.host },
2835
};
2936

@@ -87,15 +94,28 @@ export function startServer({ cliOptions }: { cliOptions: CliOptions }) {
8794

8895
app.all('*', (req: Request, res: Response) => {
8996
console.log(`Hitting catch all route: ${req.method} ${req.path}`);
97+
98+
const targetUrlParam = req.query.target_url as string;
99+
const targetUrl = parseTargetUrl(targetUrlParam);
100+
if (targetUrl) {
101+
const options: HttpRequestOptions | HttpsRequestOptions = {
102+
method: req.method,
103+
// Headers: override the host header for the target.
104+
headers: { ...req.headers, host: targetUrl.host },
105+
};
106+
console.log('options', options);
107+
}
108+
109+
// Setup options for the outbound request
110+
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
111+
console.log('Full URL:', fullUrl);
112+
console.log('Full hostname:', req.hostname);
113+
console.log('Subdomains:', req.subdomains);
114+
90115
res.send('Noting to see here. Try POST /chat/completions');
91116
});
92117

93118
app.listen(PORT, () => {
94119
console.log(`Server running on port ${PORT}`);
95-
console.log(`Try sending a cURL request:\n`);
96-
console.log(`
97-
curl -X POST \\
98-
http://localhost:${PORT}/?target_url=https://jsonplaceholder.typicode.com/posts \\
99-
-d '{"title": "foo", "body": "bar", "userId": 1}'`);
100120
});
101121
}

src/formatters.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import express from 'express';
23
import { omit } from 'lodash';
34
import OpenAI from 'openai';
@@ -14,7 +15,7 @@ export function parseTargetUrl(targetUrlParam: string | undefined) {
1415
const decodedUrl = decodeURIComponent(targetUrlParam);
1516
try {
1617
return new URL(decodedUrl);
17-
} catch (err) {
18+
} catch (e) {
1819
console.log(`target_url query parameter is invalid: "${decodedUrl}"`);
1920
return;
2021
}
@@ -97,12 +98,15 @@ export function formatRequestBody({
9798
if (cliOptions.tools === 'none') {
9899
return JSON.stringify(omit(parsed, 'tools'), null, 2);
99100
} else if (cliOptions.tools === 'name') {
100-
const toolNames = parsed.tools.map((tool) => tool.function.name); // only show tool names
101+
const toolNames = parsed.tools.map(
102+
(tool) => tool.name ?? tool?.function?.name,
103+
); // only show tool names
101104
const output = { ...parsed, tools: toolNames };
102105
return JSON.stringify(output, null, 2);
103106
}
104107
return JSON.stringify(parsed, null, 2);
105-
} catch (e) {
108+
} catch (e: any) {
109+
console.error(`Error parsing request data: ${e.message}\n`);
106110
return requestData;
107111
}
108112
}

0 commit comments

Comments
 (0)