Skip to content

Commit 50f885a

Browse files
authored
feat: add link to logs on error page (#1070)
1 parent 66c6fd2 commit 50f885a

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/pages/500.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import PageContainer from '../components/PageContainer';
66

77
import { pages } from '../util/constants';
88

9-
export default () => (
9+
export default ({ reqId }: { reqId: string }) => (
1010
<>
1111
<PageContainer>
1212
<div style={{ textAlign: 'center', marginBottom: '2rem' }}>
@@ -19,7 +19,14 @@ export default () => (
1919
python/curl/etc.
2020
</p>
2121
<p>
22-
<a href={pages.index}>Go Home You</a>
22+
<a href={pages.index}>Go Home</a>
23+
</p>
24+
<p>
25+
<a
26+
href={`https://vercel.com/packagephobia/packagephobia/logs?requestIds=${reqId}`}
27+
>
28+
View Logs
29+
</a>
2330
</p>
2431
</div>
2532

src/pages/_document.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export async function renderPage(
129129
pathname: string,
130130
query: ParsedUrlQuery,
131131
tmpDir: string,
132-
gaId = '',
132+
gaId: string,
133+
reqId: string,
133134
) {
134135
res.statusCode = getStatusCode(pathname);
135136
const force = query.force === '1';
@@ -181,7 +182,15 @@ export async function renderPage(
181182
</script>
182183
${OctocatCorner()}
183184
<div id="${containerId}">`);
184-
const factory = await routePage(pathname, inputStr, pkgVersions, manifest, force, tmpDir);
185+
const factory = await routePage(
186+
pathname,
187+
inputStr,
188+
pkgVersions,
189+
manifest,
190+
force,
191+
tmpDir,
192+
reqId,
193+
);
185194
const stream = renderToStaticNodeStream(factory);
186195
stream.pipe(res, { end: false });
187196
stream.on('end', () => {
@@ -219,6 +228,7 @@ async function routePage(
219228
manifest: NpmManifest | null,
220229
force: boolean,
221230
tmpDir: string,
231+
reqId: string,
222232
) {
223233
try {
224234
switch (pathname) {
@@ -244,7 +254,7 @@ async function routePage(
244254
return <NotFound resource={err.resource} />;
245255
}
246256
console.error('Unexpected Error Occurred...', err);
247-
return <ServerError />;
257+
return <ServerError reqId={reqId} />;
248258
}
249259
}
250260

src/server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export async function handler(req: IncomingMessage, res: ServerResponse) {
3232
const userAgent = headers['user-agent'] || '';
3333
console.log(`${method} ${headers.host}${url}`);
3434
console.log(`user-agent: ${userAgent}`);
35+
const reqId =
36+
String(headers['x-vercel-id'] || '')
37+
.split(':')
38+
.pop() || '';
3539
if (
3640
!userAgent ||
3741
userAgent.startsWith('node') ||
@@ -112,7 +116,7 @@ export async function handler(req: IncomingMessage, res: ServerResponse) {
112116
return res.end();
113117
} catch (e) {
114118
res.setHeader('Content-Type', mimeType('*.html'));
115-
return renderPage(res, pages.parseFailure, query, TMPDIR, GA_ID);
119+
return renderPage(res, pages.parseFailure, query, TMPDIR, GA_ID, reqId);
116120
}
117121
});
118122
} else {
@@ -121,7 +125,7 @@ export async function handler(req: IncomingMessage, res: ServerResponse) {
121125
typeof query.p === 'string' && parsePackageString(query.p).version !== null;
122126
res.setHeader('Content-Type', mimeType('*.html'));
123127
res.setHeader('Cache-Control', cacheControl(isProd, isIndex || hasVersion ? 31 : 0));
124-
renderPage(res, pathname, query, TMPDIR, GA_ID);
128+
renderPage(res, pathname, query, TMPDIR, GA_ID, reqId);
125129
}
126130
} catch (e) {
127131
console.error(e);

0 commit comments

Comments
 (0)