Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion apps/site/app/[locale]/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import GlowingBackdropLayout from '#site/layouts/GlowingBackdrop';

import type { FC } from 'react';

const ErrorPage: FC<{ error: Error }> = () => {
type ErrorPageProps = {
error: Error & { digest?: string };
};

const ErrorPage: FC<ErrorPageProps> = ({ error }) => {
const t = useTranslations();

return (
Expand All @@ -22,6 +26,19 @@ const ErrorPage: FC<{ error: Error }> = () => {
{t('layouts.error.internalServerError.description')}
</p>

{(error.message || error.digest) && (
<details className="max-w-2xl rounded-lg border border-neutral-300 bg-neutral-950/90 px-4 py-3 text-left text-neutral-50">
<summary className="cursor-pointer font-medium">
{t('layouts.error.details')}
</summary>

<pre className="mt-3 overflow-x-auto font-mono text-xs leading-6 break-words whitespace-pre-wrap">
{error.message}
{error.digest && `digest: ${error.digest}`}
</pre>
</details>
)}

<Button href="/">{t('layouts.error.backToHome')}</Button>
</GlowingBackdropLayout>
);
Expand Down
77 changes: 77 additions & 0 deletions apps/site/tests/errorPage.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';

import { render, screen } from '@testing-library/react';

describe('ErrorPage', () => {
const setupErrorPage = async (t, showErrorDetails, suffix = '') => {
t.mock.module('#site/components/Common/Button', {
defaultExport: ({ children, href }) => <a href={href}>{children}</a>,
});

t.mock.module('#site/layouts/GlowingBackdrop', {
defaultExport: ({ children }) => <main>{children}</main>,
});

t.mock.module('#site/next.constants.mjs', {
namedExports: {
SHOW_ERROR_DETAILS: showErrorDetails,
},
});

return import(`../app/[locale]/error.tsx${suffix}`);
};

it('renders technical details in preview environments', async t => {
const { default: ErrorPage } = await setupErrorPage(t, true);

render(
<ErrorPage
error={Object.assign(new Error('Preview deployment failed'), {
digest: 'abc123',
})}
/>
);

assert.equal(
screen.getByRole('heading').textContent,
'layouts.error.internalServerError.title'
);
assert.equal(
screen.getByRole('link').textContent,
'layouts.error.backToHome'
);
assert.equal(
screen.getByText('layouts.error.details').textContent,
'layouts.error.details'
);
assert.match(
screen.getByText(/Preview deployment failed/).textContent,
/Preview deployment failed/
);
assert.match(
screen.getByText(/digest: abc123/i).textContent,
/digest: abc123/i
);
});

it('hides technical details when the flag is disabled', async t => {
const { default: ErrorPage } = await setupErrorPage(
t,
false,
'?show-error-details-disabled'
);

render(
<ErrorPage
error={Object.assign(new Error('Production should stay generic'), {
digest: 'hidden123',
})}
/>
);

assert.equal(screen.queryByText('layouts.error.details'), null);
assert.equal(screen.queryByText(/Production should stay generic/), null);
assert.equal(screen.queryByText(/digest: hidden123/i), null);
});
});
1 change: 1 addition & 0 deletions packages/i18n/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
"title": "Internal Server Error",
"description": "This page has thrown a non-recoverable error."
},
"details": "Details",
"backToHome": "Back to Home"
},
"download": {
Expand Down