Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON.stringify generic errors #31866

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/client/index.tsx
Expand Up @@ -339,7 +339,7 @@ export async function initNext(opts: { webpackHMR?: any } = {}) {
}
} catch (error) {
// This catches errors like throwing in the top level of a module
initialErr = isError(error) ? error : new Error(error + '')
initialErr = isError(error) ? error : new Error(JSON.stringify(error))
}

if (process.env.NODE_ENV === 'development') {
Expand Down
8 changes: 4 additions & 4 deletions packages/next/server/base-server.ts
Expand Up @@ -2292,7 +2292,7 @@ export default abstract class Server {
}
}
} catch (error) {
const err = isError(error) ? error : error ? new Error(error + '') : null
const err = isError(error) ? error : error ? new Error(JSON.stringify(error)) : null
if (err instanceof NoFallbackError && bubbleNoFallback) {
throw err
}
Expand All @@ -2313,7 +2313,7 @@ export default abstract class Server {
if (isError(err)) err.page = page
throw err
}
this.logError(err || new Error(error + ''))
this.logError(err || new Error(JSON.stringify(error)))
}
return response
}
Expand Down Expand Up @@ -2433,11 +2433,11 @@ export default abstract class Server {
const renderToHtmlError = isError(error)
? error
: error
? new Error(error + '')
? new Error(JSON.stringify(error))
: null
const isWrappedError = renderToHtmlError instanceof WrappedBuildError
if (!isWrappedError) {
this.logError(renderToHtmlError || new Error(error + ''))
this.logError(renderToHtmlError || new Error(JSON.stringify(error)))
}
res.statusCode = 500
const fallbackComponents = await this.getFallbackErrorComponents()
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/dev/hot-reloader.ts
Expand Up @@ -251,7 +251,7 @@ export default class HotReloader {
} catch (error) {
await renderScriptError(
pageBundleRes,
isError(error) ? error : new Error(error + '')
isError(error) ? error : new Error(JSON.stringify(error))
)
return { finished: true }
}
Expand Down
8 changes: 6 additions & 2 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -538,7 +538,7 @@ export default class DevServer extends Server {
return result
} catch (error) {
this.logErrorWithOriginalStack(error, undefined, 'client')
const err = isError(error) ? error : new Error(error + '')
const err = isError(error) ? error : new Error(JSON.stringify(error))
;(err as any).middleware = true
const { request, response, parsedUrl } = params
this.renderError(err, request, response, parsedUrl.pathname)
Expand Down Expand Up @@ -591,7 +591,11 @@ export default class DevServer extends Server {
return await super.run(req, res, parsedUrl)
} catch (error) {
res.statusCode = 500
const err = isError(error) ? error : error ? new Error(error + '') : null
const err = isError(error)
? error
: error
? new Error(JSON.stringify(error))
: null
try {
this.logErrorWithOriginalStack(err).catch(() => {})
return await this.renderError(err, req, res, pathname!, {
Expand Down
23 changes: 23 additions & 0 deletions test/development/acceptance/ReactRefreshLogBox.test.ts
Expand Up @@ -903,4 +903,27 @@ describe('ReactRefreshLogBox', () => {

await cleanup()
})

test('custom error prints JSON as string', async () => {
const { session, cleanup } = await sandbox(next)

await session.patch(
'index.js',
`
export default () => {
throw {'a': 1, 'b': 'x'};
return (
<div>hello</div>
)
}
`
)

expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxDescription()).toMatchInlineSnapshot(
`"Error: {\\"a\\":1,\\"b\\":\\"x\\"}"`
)

await cleanup()
})
})