Unverified Commit b9ca95c6 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Don't swallow strings thrown in Server Components (#92592)

parent 904905cc
...@@ -60,11 +60,6 @@ export function createReactServerErrorHandler( ...@@ -60,11 +60,6 @@ export function createReactServerErrorHandler(
spanToRecordOn?: any spanToRecordOn?: any
): RSCErrorHandler { ): RSCErrorHandler {
return (thrownValue: unknown) => { return (thrownValue: unknown) => {
if (typeof thrownValue === 'string') {
// TODO-APP: look at using webcrypto instead. Requires a promise to be awaited.
return stringHash(thrownValue).toString()
}
// If the response was closed, we don't need to log the error. // If the response was closed, we don't need to log the error.
if (isAbortError(thrownValue)) return if (isAbortError(thrownValue)) return
...@@ -103,9 +98,12 @@ export function createReactServerErrorHandler( ...@@ -103,9 +98,12 @@ export function createReactServerErrorHandler(
// but has a digest from other means. Keep the error as-is. // but has a digest from other means. Keep the error as-is.
} }
} else { } else {
err.digest = createDigestWithErrorCode( // TODO-APP: look at using webcrypto instead of string-hash. Requires a promise to be awaited.
err.digest =
typeof thrownValue === 'string'
? stringHash(thrownValue).toString()
: createDigestWithErrorCode(
err, err,
// TODO-APP: look at using webcrypto instead. Requires a promise to be awaited.
stringHash(err.message + (err.stack || '')).toString() stringHash(err.message + (err.stack || '')).toString()
) )
} }
......
...@@ -94,6 +94,7 @@ describe('app-dir - errors', () => { ...@@ -94,6 +94,7 @@ describe('app-dir - errors', () => {
}) })
it('should trigger error component when undefined is thrown during server components rendering', async () => { it('should trigger error component when undefined is thrown during server components rendering', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/server-component/throw-undefined') const browser = await next.browser('/server-component/throw-undefined')
expect( expect(
...@@ -107,16 +108,28 @@ describe('app-dir - errors', () => { ...@@ -107,16 +108,28 @@ describe('app-dir - errors', () => {
await browser.waitForElementByCss('#error-boundary-digest').text() await browser.waitForElementByCss('#error-boundary-digest').text()
// Digest of the error message should be stable. // Digest of the error message should be stable.
).not.toBe('') ).not.toBe('')
expect(stripAnsi(next.cliOutput)).toEqual( const cleanCliOutput = stripAnsi(
next.cliOutput.slice(outputIndex)
).replaceAll(/digest: '\d+(@E\d+)'/g, "digest: '<digest>$1'")
if (isNextDev) {
expect(cleanCliOutput).toEqual(
expect.stringMatching( expect.stringMatching(
isNextDev /Error: An undefined error was thrown.*digest: '<digest>@E98'/s
? /Error: An undefined error was thrown.*digest: '\d+@E\d+'/s
: /Error: undefined.*digest: '\d+@E\d+'/s
) )
) )
} else {
expect(cleanCliOutput).toMatchInlineSnapshot(`
"⨯ Error: undefined
at stringify (<anonymous>) {
digest: '<digest>@E394'
}
"
`)
}
}) })
it('should trigger error component when null is thrown during server components rendering', async () => { it('should trigger error component when null is thrown during server components rendering', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/server-component/throw-null') const browser = await next.browser('/server-component/throw-null')
expect( expect(
...@@ -130,16 +143,28 @@ describe('app-dir - errors', () => { ...@@ -130,16 +143,28 @@ describe('app-dir - errors', () => {
await browser.waitForElementByCss('#error-boundary-digest').text() await browser.waitForElementByCss('#error-boundary-digest').text()
// Digest of the error message should be stable. // Digest of the error message should be stable.
).not.toBe('') ).not.toBe('')
expect(stripAnsi(next.cliOutput)).toEqual( const cleanCliOutput = stripAnsi(
next.cliOutput.slice(outputIndex)
).replaceAll(/digest: '\d+(@E\d+)'/g, "digest: '<digest>$1'")
if (isNextDev) {
expect(cleanCliOutput).toEqual(
expect.stringMatching( expect.stringMatching(
isNextDev /Error: A null error was thrown.*digest: '<digest>@E336'/s
? /Error: A null error was thrown.*digest: '\d+@E\d+'/s
: /Error: null.*digest: '\d+@E\d+'/s
) )
) )
} else {
expect(cleanCliOutput).toMatchInlineSnapshot(`
"⨯ Error: null
at stringify (<anonymous>) {
digest: '<digest>@E394'
}
"
`)
}
}) })
it('should trigger error component when a string is thrown during server components rendering', async () => { it('should trigger error component when a string is thrown during server components rendering', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/server-component/throw-string') const browser = await next.browser('/server-component/throw-string')
expect( expect(
...@@ -153,13 +178,22 @@ describe('app-dir - errors', () => { ...@@ -153,13 +178,22 @@ describe('app-dir - errors', () => {
await browser.waitForElementByCss('#error-boundary-digest').text() await browser.waitForElementByCss('#error-boundary-digest').text()
// Digest of the error message should be stable. // Digest of the error message should be stable.
).not.toBe('') ).not.toBe('')
expect(stripAnsi(next.cliOutput)).toEqual( const cleanCliOutput = stripAnsi(
expect.stringMatching( next.cliOutput.slice(outputIndex)
isNextDev ).replaceAll(/digest: '\d+'/g, "digest: '<digest>'")
? /Error: this is a test.*digest: '\d+'/s if (isNextDev) {
: /Error: An error occurred in the Server Components render.*digest: '\d+'/s expect(cleanCliOutput).toEqual(
) expect.stringMatching(/Error: this is a test.*digest: '<digest>'/s)
) )
} else {
expect(cleanCliOutput).toMatchInlineSnapshot(`
"⨯ Error: this is a test
at stringify (<anonymous>) {
digest: '<digest>'
}
"
`)
}
}) })
it('should use default error boundary for prod and overlay for dev when no error component specified', async () => { it('should use default error boundary for prod and overlay for dev when no error component specified', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment