-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (multi-domain) handle uncaught errors in the spec bridge (#19976)
- Loading branch information
Showing
6 changed files
with
153 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { $Cy } from '../cypress/cy' | ||
import $errUtils, { ErrorFromProjectRejectionEvent } from '../cypress/error_utils' | ||
|
||
export const handleErrorEvent = (cy: $Cy, frameType: 'spec' | 'app') => { | ||
return (handlerType: string) => { | ||
return (event) => { | ||
const { originalErr, err, promise } = $errUtils.errorFromUncaughtEvent(handlerType, event) as ErrorFromProjectRejectionEvent | ||
const handled = cy.onUncaughtException({ | ||
err, | ||
promise, | ||
handlerType, | ||
frameType, | ||
}) | ||
|
||
$errUtils.logError(Cypress, handlerType, originalErr, handled) | ||
|
||
if (!handled) { | ||
// if unhandled, fail the current command to fail the current test in the primary domain | ||
// a current command may not exist if an error occurs in the spec bridge after the test is over | ||
const command = cy.state('current') | ||
const log = command?.getLastLog() | ||
|
||
if (log) log.error(err) | ||
} | ||
|
||
// return undefined so the browser does its default | ||
// uncaught exception behavior (logging to console) | ||
return undefined | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { $Cy } from '../cypress/cy' | ||
import { handleErrorEvent } from './errors' | ||
|
||
export const handleSpecWindowEvents = (cy: $Cy) => { | ||
const handleWindowErrorEvent = handleErrorEvent(cy, 'spec')('error') | ||
const handleWindowUnhandledRejectionEvent = handleErrorEvent(cy, 'spec')('unhandledrejection') | ||
|
||
const handleUnload = () => { | ||
window.removeEventListener('unload', handleUnload) | ||
window.removeEventListener('error', handleWindowErrorEvent) | ||
window.removeEventListener('unhandledrejection', handleWindowUnhandledRejectionEvent) | ||
} | ||
|
||
window.addEventListener('unload', handleUnload) | ||
window.addEventListener('error', handleWindowErrorEvent) | ||
window.addEventListener('unhandledrejection', handleWindowUnhandledRejectionEvent) | ||
} |