Merge branch 'print_exception_properties' of https://github.com/jbunton-atlassian/jasmine into jbunton-atlassian-print_exception_properties

- Merges #1516 from @jbunton-atlassian
This commit is contained in:
Gregg Van Hove
2018-02-26 17:47:39 -08:00
3 changed files with 69 additions and 2 deletions

View File

@@ -2498,12 +2498,16 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) {
var stackTrace = new j$.StackTrace(error.stack);
var lines = filterJasmine(stackTrace);
var result = '';
if (stackTrace.message) {
lines.unshift(stackTrace.message);
}
return lines.join('\n');
result += formatProperties(error);
result += lines.join('\n');
return result;
};
function filterJasmine(stackTrace) {
@@ -2520,6 +2524,30 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) {
return result;
}
function formatProperties(error) {
if (!(error instanceof Object)) {
return;
}
var ignored = ['name', 'message', 'stack', 'fileName', 'sourceURL', 'line', 'lineNumber'];
var result = {};
var empty = true;
for (var prop in error) {
if (ignored.includes(prop)) {
continue;
}
result[prop] = error[prop];
empty = false;
}
if (!empty) {
return 'error properties: ' + j$.pp(result) + '\n';
}
return '';
}
}
return ExceptionFormatter;

View File

@@ -116,5 +116,16 @@ describe("ExceptionFormatter", function() {
it("returns null if no Error provided", function() {
expect(new jasmineUnderTest.ExceptionFormatter().stack()).toBeNull();
});
it("includes error properties in stack", function() {
var error;
try { throw new Error("an error") } catch(e) { error = e; }
error.someProperty = 'hello there';
var result = new jasmineUnderTest.ExceptionFormatter().stack(error);
expect(result).toMatch(/error properties:.*someProperty.*hello there/);
});
});
});

View File

@@ -29,12 +29,16 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) {
var stackTrace = new j$.StackTrace(error.stack);
var lines = filterJasmine(stackTrace);
var result = '';
if (stackTrace.message) {
lines.unshift(stackTrace.message);
}
return lines.join('\n');
result += formatProperties(error);
result += lines.join('\n');
return result;
};
function filterJasmine(stackTrace) {
@@ -51,6 +55,30 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) {
return result;
}
function formatProperties(error) {
if (!(error instanceof Object)) {
return;
}
var ignored = ['name', 'message', 'stack', 'fileName', 'sourceURL', 'line', 'lineNumber'];
var result = {};
var empty = true;
for (var prop in error) {
if (ignored.includes(prop)) {
continue;
}
result[prop] = error[prop];
empty = false;
}
if (!empty) {
return 'error properties: ' + j$.pp(result) + '\n';
}
return '';
}
}
return ExceptionFormatter;