Skip to content

Commit

Permalink
Add better error for null case as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 11, 2022
1 parent 2104fcf commit c0dae27
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
18 changes: 15 additions & 3 deletions errors/threw-undefined.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# Threw undefined in render
# Threw `undefined`/`null`

#### Why This Error Occurred

Somewhere in your code you `throw` an `undefined` value. Since this isn't a valid error there isn't a stack trace. We show this error instead to let you know what to look for.
Somewhere in your code you `throw` an `undefined` or `null` value. Since this isn't a valid error there isn't a stack trace. We show this error instead to let you know what to look for.

```js
function getData() {
let error
throw error
}

function Page() {
const error = data?.error || null
throw error
}
```
#### Possible Ways to Fix It
Look in your pages and find where an error could be throwing `undefined`
Look in your pages and find where an error could be throwing `undefined` or `null` values and ensure `new Error()` is used instead.
7 changes: 7 additions & 0 deletions packages/next/lib/is-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export function getProperError(err: unknown): Error {
'see here for more info: https://nextjs.org/docs/messages/threw-undefined'
)
}

if (err === null) {
return new Error(
'A null error was thrown, ' +
'see here for more info: https://nextjs.org/docs/messages/threw-undefined'
)
}
}

return new Error(isPlainObject(err) ? JSON.stringify(err) : err + '')
Expand Down
28 changes: 28 additions & 0 deletions test/development/acceptance/ReactRefreshLogBox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,34 @@ describe('ReactRefreshLogBox', () => {
`"Error: string error"`
)

// fix previous error
await session.patch(
'index.js',
`
export default () => {
return (
<div>hello</div>
)
}
`
)
expect(await session.hasRedbox(false)).toBe(false)
await session.patch(
'index.js',
`
export default () => {
throw null
return (
<div>hello</div>
)
}
`
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxDescription()).toContain(
`Error: A null error was thrown`
)

await cleanup()
})
})

0 comments on commit c0dae27

Please sign in to comment.