Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: improve long-stack-trace stack format detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Mar 17, 2017
1 parent eeaab91 commit 6010557
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
20 changes: 12 additions & 8 deletions lib/zone-spec/long-stack-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const IGNORE_FRAMES: {[k: string]: true} = {};
const creationTrace = '__creationTrace__';
const ERROR_TAG = 'STACKTRACE TRACKING';
const SEP_TAG = '__SEP_TAG__';
let sepTemplate = '';
let sepTemplate: string = SEP_TAG + '@[native]';

class LongStackTrace {
error: Error = getStacktrace();
Expand Down Expand Up @@ -137,19 +137,23 @@ function computeIgnoreFrames() {
const frames2 = frames[1];
for (let i = 0; i < frames1.length; i++) {
const frame1 = frames1[i];
const frame2 = frames2[i];
if (!sepTemplate && frame1.indexOf(ERROR_TAG) == -1) {
sepTemplate = frame1.replace(/^(\s*(at)?\s*)([\w\/\<]+)/, '$1' + SEP_TAG);
if (frame1.indexOf(ERROR_TAG) == -1) {
let match = frame1.match(/^\s*at\s+/);
if (match) {
sepTemplate = match[0] + SEP_TAG + ' (http://localhost)';
break;
}
}
}

for (let i = 0; i < frames1.length; i++) {
const frame1 = frames1[i];
const frame2 = frames2[i];
if (frame1 === frame2) {
IGNORE_FRAMES[frame1] = true;
} else {
break;
}
}
if (!sepTemplate) {
// If we could not find it default to this text.
sepTemplate = SEP_TAG + '@[native code]';
}
}
computeIgnoreFrames();
30 changes: 17 additions & 13 deletions test/zone-spec/long-stack-trace-zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ describe('longStackTraceZone', function() {
log = [];
});

function expectElapsed(stack: string, expectedCount: number) {
try {
let actualCount = stack.split('_Elapsed_').length;
if (actualCount !== expectedCount) {
expect(actualCount).toEqual(expectedCount);
console.log(stack);
}
} catch (e) {
expect(e).toBe(null);
}
}

it('should produce long stack traces', function(done) {
lstz.run(function() {
setTimeout(function() {
setTimeout(function() {
setTimeout(function() {
try {
expect(log[0].stack.split('Elapsed').length).toBe(3);
done();
} catch (e) {
expect(e).toBe(null);
}
expectElapsed(log[0].stack, 3);
done();
}, 0);
throw new Error('Hello');
}, 0);
Expand Down Expand Up @@ -81,12 +89,8 @@ describe('longStackTraceZone', function() {
fail('should not get here');
});
setTimeout(function() {
try {
expect(log[0].stack.split('Elapsed').length).toBe(5);
done();
} catch (e) {
expect(e).toBe(null);
}
expectElapsed(log[0].stack, 5);
done();
}, 0);
}, 0);
}, 0);
Expand All @@ -105,7 +109,7 @@ describe('longStackTraceZone', function() {
promise.catch(function(error) {
// should be able to get long stack trace
const longStackFrames: string = longStackTraceZoneSpec.getLongStackTrace(error);
expect(longStackFrames.split('Elapsed').length).toBe(4);
expectElapsed(longStackFrames, 4);
done();
});
}, 0);
Expand Down

0 comments on commit 6010557

Please sign in to comment.