Skip to content

Commit

Permalink
Unhandled errors and rejections opens as minimized in app dir error o…
Browse files Browse the repository at this point in the history
…verlay (#43844)

Updated version of the reverted #43511

Unhandled errors that did not occur during React rendering (those errors are caught in `getDerivedStateFromError` in the Error Overlay) should be opened in the minimized toast state instead of fullscreen. For example if they occur in event handlers or setTimeout. Errors that breaks the app, such as uncaught render errors or build errors, still opens up in fullscreen mode.

The added test make sure the errors opens up as minimized, but if there's a breaking error it should "win" and open up in fullscreen. The updated tests either throw errors inside an event handler or a setTimeout, or the error is handled in a custom error boundary - which means the app don't break.

Closes NEXT-128

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
hanneslund committed Dec 8, 2022
1 parent c341c76 commit 98cb254
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 18 deletions.
Expand Up @@ -76,9 +76,9 @@ class ReactDevOverlay extends React.PureComponent<
) : hasBuildError ? (
<BuildError message={state.buildError!} />
) : hasRuntimeErrors ? (
<Errors errors={state.errors} />
<Errors initialDisplayState="minimized" errors={state.errors} />
) : reactError ? (
<Errors errors={[reactError]} />
<Errors initialDisplayState="fullscreen" errors={[reactError]} />
) : undefined}
</ShadowPortal>
) : undefined}
Expand Down
Expand Up @@ -24,10 +24,15 @@ export type SupportedErrorEvent = {
id: number
event: UnhandledErrorAction | UnhandledRejectionAction
}
export type ErrorsProps = { errors: SupportedErrorEvent[] }
export type ErrorsProps = {
errors: SupportedErrorEvent[]
initialDisplayState: DisplayState
}

type ReadyErrorEvent = ReadyRuntimeError

type DisplayState = 'minimized' | 'fullscreen' | 'hidden'

function getErrorSignature(ev: SupportedErrorEvent): string {
const { event } = ev
switch (event.type) {
Expand Down Expand Up @@ -73,7 +78,10 @@ const HotlinkedText: React.FC<{
)
}

export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
export const Errors: React.FC<ErrorsProps> = function Errors({
errors,
initialDisplayState,
}) {
const [lookups, setLookups] = React.useState(
{} as { [eventId: string]: ReadyErrorEvent }
)
Expand Down Expand Up @@ -137,9 +145,8 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
}
}, [nextError])

const [displayState, setDisplayState] = React.useState<
'minimized' | 'fullscreen' | 'hidden'
>('fullscreen')
const [displayState, setDisplayState] =
React.useState<DisplayState>(initialDisplayState)
const [activeIdx, setActiveIndex] = React.useState<number>(0)
const previous = React.useCallback((e?: MouseEvent | TouchEvent) => {
e?.preventDefault()
Expand Down
90 changes: 79 additions & 11 deletions test/development/acceptance-app/ReactRefreshLogBox.test.ts
Expand Up @@ -49,7 +49,7 @@ describe('ReactRefreshLogBox app', () => {
)
await session.evaluate(() => document.querySelector('a').click())

expect(await session.hasRedbox(true)).toBe(true)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxSource()).toMatchSnapshot()

await cleanup()
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('ReactRefreshLogBox app', () => {
)

await new Promise((resolve) => setTimeout(resolve, 1000))
expect(await session.hasRedbox(true)).toBe(true)
await session.waitForAndOpenRuntimeError()
if (process.platform === 'win32') {
expect(await session.getRedboxSource()).toMatchSnapshot()
} else {
Expand Down Expand Up @@ -568,7 +568,7 @@ describe('ReactRefreshLogBox app', () => {
`export default function FunctionDefault() { throw new Error('no'); }`
)

expect(await session.hasRedbox(true)).toBe(true)
await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxSource()).toMatchSnapshot()
expect(
await session.evaluate(() => document.querySelector('h2').textContent)
Expand Down Expand Up @@ -770,9 +770,8 @@ describe('ReactRefreshLogBox app', () => {
`
)

expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
await session.waitForAndOpenRuntimeError()

const header = await session.getRedboxDescription()
expect(header).toMatchSnapshot()
Expand Down Expand Up @@ -816,9 +815,8 @@ describe('ReactRefreshLogBox app', () => {
`
)

expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
await session.waitForAndOpenRuntimeError()

const header2 = await session.getRedboxDescription()
expect(header2).toMatchSnapshot()
Expand Down Expand Up @@ -862,9 +860,8 @@ describe('ReactRefreshLogBox app', () => {
`
)

expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
await session.waitForAndOpenRuntimeError()

const header3 = await session.getRedboxDescription()
expect(header3).toMatchSnapshot()
Expand Down Expand Up @@ -908,9 +905,8 @@ describe('ReactRefreshLogBox app', () => {
`
)

expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
await session.waitForAndOpenRuntimeError()

const header4 = await session.getRedboxDescription()
expect(header4).toMatchInlineSnapshot(
Expand Down Expand Up @@ -1085,4 +1081,76 @@ describe('ReactRefreshLogBox app', () => {

await cleanup()
})

test('Unhandled errors and rejections opens up in the minimized state', async () => {
const { session, browser, cleanup } = await sandbox(next)

const file = `
export default function Index() {
//
setTimeout(() => {
throw new Error('Unhandled error')
}, 0)
setTimeout(() => {
Promise.reject(new Error('Undhandled rejection'))
}, 0)
return (
<>
<button
id="unhandled-error"
onClick={() => {
throw new Error('Unhandled error')
}}
>
Unhandled error
</button>
<button
id="unhandled-rejection"
onClick={() => {
Promise.reject(new Error('Undhandled rejection'))
}}
>
Unhandled rejection
</button>
</>
)
}
`

await session.patch('index.js', file)

// Unhandled error and rejection in setTimeout
expect(
await browser.waitForElementByCss('.nextjs-toast-errors').text()
).toBe('2 errors')

// Unhandled error in event handler
await browser.elementById('unhandled-error').click()
await check(
() => browser.elementByCss('.nextjs-toast-errors').text(),
/3 errors/
)

// Unhandled rejection in event handler
await browser.elementById('unhandled-rejection').click()
await check(
() => browser.elementByCss('.nextjs-toast-errors').text(),
/4 errors/
)
expect(await session.hasRedbox()).toBe(false)

// Add Component error
await session.patch(
'index.js',
file.replace(
'//',
"if (typeof window !== 'undefined') throw new Error('Component error')"
)
)

// Render error should "win" and show up in fullscreen
expect(await session.hasRedbox(true)).toBe(true)

await cleanup()
})
})
3 changes: 3 additions & 0 deletions test/development/acceptance-app/helpers.ts
Expand Up @@ -112,6 +112,9 @@ export async function sandbox(
}
return source
},
async waitForAndOpenRuntimeError() {
return browser.waitForElementByCss('[data-nextjs-toast]').click()
},
},
async cleanup() {
await browser.close()
Expand Down

0 comments on commit 98cb254

Please sign in to comment.