(Zone: ZoneType)
| 13 | import {ZoneType} from '../zone-impl'; |
| 14 | |
| 15 | export function patchLongStackTrace(Zone: ZoneType): void { |
| 16 | const NEWLINE = '\n'; |
| 17 | const IGNORE_FRAMES: {[k: string]: true} = {}; |
| 18 | const creationTrace = '__creationTrace__'; |
| 19 | const ERROR_TAG = 'STACKTRACE TRACKING'; |
| 20 | const SEP_TAG = '__SEP_TAG__'; |
| 21 | let sepTemplate: string = SEP_TAG + '@[native]'; |
| 22 | |
| 23 | class LongStackTrace { |
| 24 | error: Error = getStacktrace(); |
| 25 | timestamp: Date = new Date(); |
| 26 | } |
| 27 | |
| 28 | function getStacktraceWithUncaughtError(): Error { |
| 29 | return new Error(ERROR_TAG); |
| 30 | } |
| 31 | |
| 32 | function getStacktraceWithCaughtError(): Error { |
| 33 | try { |
| 34 | throw getStacktraceWithUncaughtError(); |
| 35 | } catch (err) { |
| 36 | return err as Error; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Some implementations of exception handling don't create a stack trace if the exception |
| 41 | // isn't thrown, however it's faster not to actually throw the exception. |
| 42 | const error = getStacktraceWithUncaughtError(); |
| 43 | const caughtError = getStacktraceWithCaughtError(); |
| 44 | const getStacktrace = error.stack |
| 45 | ? getStacktraceWithUncaughtError |
| 46 | : caughtError.stack |
| 47 | ? getStacktraceWithCaughtError |
| 48 | : getStacktraceWithUncaughtError; |
| 49 | |
| 50 | function getFrames(error: Error): string[] { |
| 51 | return error.stack ? error.stack.split(NEWLINE) : []; |
| 52 | } |
| 53 | |
| 54 | function addErrorStack(lines: string[], error: Error): void { |
| 55 | let trace: string[] = getFrames(error); |
| 56 | for (let i = 0; i < trace.length; i++) { |
| 57 | const frame = trace[i]; |
| 58 | // Filter out the Frames which are part of stack capturing. |
| 59 | if (!IGNORE_FRAMES.hasOwnProperty(frame)) { |
| 60 | lines.push(trace[i]); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function renderLongStackTrace(frames: LongStackTrace[], stack?: string): string { |
| 66 | const longTrace: string[] = [stack ? stack.trim() : '']; |
| 67 | |
| 68 | if (frames) { |
| 69 | let timestamp = new Date().getTime(); |
| 70 | for (let i = 0; i < frames.length; i++) { |
| 71 | const traceFrames: LongStackTrace = frames[i]; |
| 72 | const lastTime = traceFrames.timestamp; |
no test coverage detected
searching dependent graphs…