Better reporting of unhandled promise rejections with truthy but non-Error reasons on Node

[#179227413]
This commit is contained in:
Steve Gravrock
2021-08-14 14:01:15 -07:00
parent e72d161fab
commit dd8a65cb60
3 changed files with 120 additions and 74 deletions

View File

@@ -4333,16 +4333,22 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
function taggedOnError(error) {
var substituteMsg;
if (error) {
if (j$.isError_(error)) {
error.jasmineMessage = jasmineMessage + ': ' + error;
} else {
if (error) {
substituteMsg = jasmineMessage + ': ' + error;
} else {
substituteMsg = jasmineMessage + ' with no error or message';
}
if (errorType === 'unhandledRejection') {
substituteMsg +=
'\n' +
'(Tip: to get a useful stack trace, use ' +
'Promise.reject(new Error(...)) instead of Promise.reject().)';
'Promise.reject(new Error(...)) instead of Promise.reject(' +
(error ? '...' : '') +
').)';
}
error = new Error(substituteMsg);

View File

@@ -170,7 +170,8 @@ describe('GlobalErrors', function() {
);
});
it('reports unhandled promise rejections in node.js', function() {
describe('Reporting unhandled promise rejections in node.js', function() {
it('reports rejections with `Error` reasons', function() {
var fakeGlobal = {
process: {
on: jasmine.createSpy('process.on'),
@@ -218,7 +219,39 @@ describe('GlobalErrors', function() {
);
});
it('reports unhandled promise rejections in node.js when no error is provided', function() {
it('reports rejections with non-`Error` reasons', function() {
var fakeGlobal = {
process: {
on: jasmine.createSpy('process.on'),
removeListener: function() {},
listeners: function() {
return [];
},
removeAllListeners: function() {}
}
},
handler = jasmine.createSpy('errorHandler'),
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.install();
errors.pushListener(handler);
expect(fakeGlobal.process.on.calls.argsFor(1)[0]).toEqual(
'unhandledRejection'
);
var addedListener = fakeGlobal.process.on.calls.argsFor(1)[1];
addedListener(17);
expect(handler).toHaveBeenCalledWith(
new Error(
'Unhandled promise rejection: 17\n' +
'(Tip: to get a useful stack trace, use ' +
'Promise.reject(new Error(...)) instead of Promise.reject(...).)'
)
);
});
it('reports rejections with no reason provided', function() {
var fakeGlobal = {
process: {
on: jasmine.createSpy('process.on'),
@@ -249,6 +282,7 @@ describe('GlobalErrors', function() {
)
);
});
});
describe('Reporting unhandled promise rejections in the browser', function() {
it('subscribes and unsubscribes from the unhandledrejection event', function() {

View File

@@ -19,16 +19,22 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
function taggedOnError(error) {
var substituteMsg;
if (error) {
if (j$.isError_(error)) {
error.jasmineMessage = jasmineMessage + ': ' + error;
} else {
if (error) {
substituteMsg = jasmineMessage + ': ' + error;
} else {
substituteMsg = jasmineMessage + ' with no error or message';
}
if (errorType === 'unhandledRejection') {
substituteMsg +=
'\n' +
'(Tip: to get a useful stack trace, use ' +
'Promise.reject(new Error(...)) instead of Promise.reject().)';
'Promise.reject(new Error(...)) instead of Promise.reject(' +
(error ? '...' : '') +
').)';
}
error = new Error(substituteMsg);