forked from TimBeyer/cls-bluebird
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd6dabd
commit 2157441
Showing
5 changed files
with
79 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* cls-bluebird tests | ||
* Add handler for unhandled rejections. | ||
* Registers handler for unhanded rejections and adds mocha `after` block to throw if any encountered. | ||
*/ | ||
|
||
/* global after */ | ||
|
||
// Exports | ||
|
||
var unhandledRejectionHandlerApplied = false; | ||
|
||
module.exports = function() { | ||
// Exit if handler already registered | ||
if (unhandledRejectionHandlerApplied) return; | ||
|
||
// Add handler | ||
var uncaughtErrors = [], | ||
uncaughtPromises = [], | ||
errorId = 1; | ||
|
||
process.on('unhandledRejection', function(err, promise) { | ||
console.log('Uncaught rejection (ID ' + errorId + '): ' + (err instanceof Error ? err.stack : err)); | ||
err.__id = errorId; | ||
errorId++; | ||
|
||
uncaughtErrors.push(err); | ||
uncaughtPromises.push(promise); | ||
}); | ||
|
||
process.on('rejectionHandled', function(promise) { | ||
var index = uncaughtPromises.indexOf(promise); | ||
if (index !== -1) { | ||
var err = uncaughtErrors[index]; | ||
console.log('Handled rejection ID ' + err.__id); | ||
} | ||
}); | ||
|
||
after(function() { | ||
if (uncaughtErrors.length) throw new Error('Uncaught rejections (' + uncaughtErrors.length + ')'); | ||
}); | ||
|
||
unhandledRejectionHandlerApplied = true; | ||
}; |
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