-
Notifications
You must be signed in to change notification settings - Fork 781
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
Core: New error
event for bailing on uncaught errors
#1638
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,55 +1,51 @@ | ||
import config from "./config"; | ||
import ProcessingQueue from "./processing-queue"; | ||
import { globalSuite } from "../core"; | ||
import { sourceFromStacktrace } from "./stacktrace"; | ||
import { extend, errorString } from "./utilities"; | ||
import Logger from "../logger"; | ||
import { test } from "../test"; | ||
import { errorString } from "./utilities"; | ||
import { emit } from "../events"; | ||
|
||
/** | ||
* Handle a global error that should result in a failed test run. | ||
* | ||
* Summary: | ||
* | ||
* - If there is a current test, it becomes a failed assertion. | ||
* - If there is a current module, it becomes a failed test (and bypassing filters). | ||
* Note that if we're before any other test or module, it will naturally | ||
* become a global test. | ||
* - If the overall test run has ended, the error is printed to `console.warn()`. | ||
* - If we're strictly inside a test (or one if its module hooks), the exception | ||
* becomes a failed assertion. | ||
* | ||
* This has the important side-effect that uncaught exceptions (such as | ||
* calling an undefined function) during a "todo" test do NOT result in | ||
* a failed test run. | ||
* | ||
* - If we're anywhere outside a test (be it in early event callbacks, or | ||
* internally between tests, or somewhere after "runEnd" if the process is | ||
* still alive for some reason), then send an "error" event to the reporters. | ||
* | ||
* @since 2.17.0 | ||
* @param {Error|any} error | ||
*/ | ||
export default function onUncaughtException( error ) { | ||
const message = errorString( error ); | ||
|
||
// We could let callers specify an extra offset to add to the number passed to | ||
// sourceFromStacktrace, in case they are a wrapper further away from the error | ||
// handler, and thus reduce some noise in the stack trace. However, we're not | ||
// doing this right now because it would almost never be used in practice given | ||
// the vast majority of error values will be an Error object, and thus have a | ||
// clean stack trace already. | ||
const source = error.stack || sourceFromStacktrace( 2 ); | ||
|
||
if ( config.current ) { | ||
config.current.assert.pushResult( { | ||
result: false, | ||
message: `global failure: ${message}`, | ||
source | ||
message: `global failure: ${errorString( error )}`, | ||
|
||
// We could let callers specify an offset to subtract a number of frames via | ||
// sourceFromStacktrace, in case they are a wrapper further away from the error | ||
// handler, and thus reduce some noise in the stack trace. However, we're not | ||
// doing this right now because it would almost never be used in practice given | ||
// the vast majority of error values will be Error objects, and thus have their | ||
// own stack trace already. | ||
source: ( error && error.stack ) || sourceFromStacktrace( 2 ) | ||
} ); | ||
} else if ( !ProcessingQueue.finished ) { | ||
test( "global failure", extend( function( assert ) { | ||
assert.pushResult( { result: false, message, source } ); | ||
}, { validTest: true } ) ); | ||
} else { | ||
|
||
// TODO: Once supported in js-reporters and QUnit, use a TAP "bail" event. | ||
// The CLI runner can use this to ensure a non-zero exit code, even if | ||
// emitted after "runEnd" and before the process exits. | ||
// The HTML Reporter can use this to renmder it on the page in a test-like | ||
// block for easy discovery. | ||
// | ||
// Avoid printing "Error: foo" twice if the environment's native stack trace | ||
// already includes that in its format. | ||
Logger.warn( source.indexOf( source ) !== -1 ? source : `${message}\n${source}` ); | ||
// The "error" event was added in QUnit 2.17. | ||
// Increase "bad assertion" stats despite no longer pushing an assertion in this case. | ||
// This ensures "runEnd" and "QUnit.done()" handlers behave as expected, since the "bad" | ||
// count is typically how reporters decide on the boolean outcome of the test run. | ||
globalSuite.globalFailureCount++; | ||
config.stats.bad++; | ||
config.stats.all++; | ||
emit( "error", error ); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uncaught errors now increment these. As errors can happen before/during
begin()
, I had to move this toconfig.js
as the increment would fail otherwise with the stats object being undefined. We can move more of this over, but that's for another day.