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

Fix presentation when onerror receives an event without error #74643

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ function onUnhandledError(event: WindowEventMap['error']): void | boolean {
event.preventDefault()
return false
}
handleClientError(event.error, [])
// When there's an error property present, we log the error to error overlay.
// Otherwise we don't do anything as it's not logging in the console either.
if (event.error) {
handleClientError(event.error, [])
huozhi marked this conversation as resolved.
Show resolved Hide resolved
}
}

function onUnhandledRejection(ev: WindowEventMap['unhandledrejection']): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'

export default function Page() {
return (
<button
onClick={() => {
// Create an ErrorEvent with only a message
const errorEvent = new ErrorEvent('error', {
message: 'dummy error message', // Message for the event
// Omit the `error` property to ensure it is not included
})

// Dispatch the event
window.dispatchEvent(errorEvent)
}}
>
click to trigger error event
</button>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
getRedboxTotalErrorCount,
openRedbox,
hasRedboxCallStack,
assertNoRedbox,
assertNoConsoleErrors,
} from 'next-test-utils'

async function getRedboxResult(browser: any) {
Expand Down Expand Up @@ -315,4 +317,12 @@ describe('app-dir - capture-console-error', () => {
`)
}
})

it('should display the error message in error event when event.error is not present', async () => {
const browser = await next.browser('/browser/error-event')
await browser.elementByCss('button').click()

await assertNoRedbox(browser)
await assertNoConsoleErrors(browser)
})
})
15 changes: 15 additions & 0 deletions test/lib/next-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1545,3 +1545,18 @@ export function createNowRouteMatches(

return urlSearchParams
}

export async function assertNoConsoleErrors(browser: BrowserInterface) {
const logs = await browser.log()
const warningsAndErrors = logs.filter((log) => {
return (
log.source === 'warning' ||
(log.source === 'error' &&
// These are expected when we visit 404 pages.
log.message !==
'Failed to load resource: the server responded with a status of 404 (Not Found)')
)
})

expect(warningsAndErrors).toEqual([])
}
Loading