Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: app dir error-global component (#44066)
## Feature NEXT-54 When there's an error in the one of the root level pages, there's no way to handle it. The team discussed this and decided there should be a global error boundary to pick up anything not handled further down in the tree. It can be called `global-error.js`. Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
- Loading branch information
1 parent
20997f8
commit 595ecdb
Showing
10 changed files
with
172 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import path from 'path' | ||
import { getRedboxHeader, hasRedbox } from 'next-test-utils' | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import webdriver from 'next-webdriver' | ||
|
||
describe('app dir - global error', () => { | ||
let next: NextInstance | ||
const isDev = (global as any).isNextDev | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: new FileRef(path.join(__dirname, './global-error')), | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('should trigger error component when an error happens during rendering', async () => { | ||
const browser = await webdriver(next.url, '/throw') | ||
await browser | ||
.waitForElementByCss('#error-trigger-button') | ||
.elementByCss('#error-trigger-button') | ||
.click() | ||
|
||
if (isDev) { | ||
expect(await hasRedbox(browser)).toBe(true) | ||
expect(await getRedboxHeader(browser)).toMatch(/Error: Client error/) | ||
} else { | ||
await browser | ||
expect(await browser.elementByCss('#error').text()).toBe( | ||
'Error message: Client error' | ||
) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use client' | ||
|
||
export default function GlobalError({ error }) { | ||
return ( | ||
<html> | ||
<head></head> | ||
<body> | ||
<div id="error">{`Error message: ${error?.message}`}</div> | ||
</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<html> | ||
<head></head> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
|
||
export default function Page() { | ||
const [clicked, setClicked] = useState(false) | ||
if (clicked) { | ||
throw new Error('Client error') | ||
} | ||
return ( | ||
<button | ||
id="error-trigger-button" | ||
onClick={() => { | ||
setClicked(true) | ||
}} | ||
> | ||
Trigger Error! | ||
</button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
experimental: { appDir: true }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters