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CAAC;QAWS,4CAAmB,GAA7B,UAA8B,KAAe;YAA7C,iBAWC;YAVC,OAAO,KAAK,CAAC,GAAG,CAAC,UAAA,IAAI;gBACnB,IAAI,KAA8B,CAAC;gBACnC,IAAI,CAAC,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE;oBACxC,OAAO,KAAI,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;iBAC/D;qBAAM,IAAI,CAAC,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE;oBAC/C,OAAO,KAAI,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;iBAC/C;qBAAM;oBACL,OAAO,IAAI,CAAC;iBACb;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QACH,qBAAC;IAAD,CAAC,AArOD,IAqOC","sourcesContent":["import { diffJson, Change } from 'diff';\n\nimport { InternError } from '../types';\nimport { Executor } from '../executors/Executor';\n\nexport default class ErrorFormatter implements ErrorFormatterProperties {\n readonly executor: Executor;\n\n constructor(executor: Executor) {\n this.executor = executor;\n }\n\n /**\n * Generates a full error message from a plain Error object, avoiding\n * duplicate error messages that might be caused by different opinions on\n * what a stack trace should look like.\n *\n * @param error An object describing the error.\n * @returns A string message describing the error.\n */\n format(\n error: string | Error | InternError,\n options?: ErrorFormatOptions\n ): string {\n options = options || {};\n let message: string;\n\n if (typeof error !== 'string' && (error.message || error.stack)) {\n message =\n (error.name || 'Error') + ': ' + (error.message || 'Unknown error');\n let stack = error.stack;\n\n if (stack) {\n // V8 puts the original error at the top of the stack too; avoid\n // redundant output that may cause confusion about how many\n // times an assertion was actually called\n if (stack.indexOf(message) === 0) {\n stack = stack.slice(message.length);\n } else if (stack.indexOf(error.message) === 0) {\n stack = stack.slice(String(error.message).length);\n } else if (stack.indexOf('Error\\n') === 0) {\n // The stack for errors in Internet Explorer may start with\n // 'Error'\n stack = stack.slice('Error'.length);\n }\n\n stack = this._normalizeStackTrace(stack);\n }\n\n const anyError: any = error;\n\n // Assertion errors may have showDiff, actual, and expected properties\n if (\n (anyError.showDiff &&\n (typeof anyError.actual === 'object' &&\n typeof anyError.expected === 'object')) ||\n (typeof anyError.actual === 'string' &&\n typeof anyError.expected === 'string')\n ) {\n const diff = this._createDiff(anyError.actual, anyError.expected);\n if (diff) {\n message += '\\n\\n' + diff + '\\n';\n }\n }\n\n if (stack && /\\S/.test(stack)) {\n message += stack;\n } else if (anyError.fileName) {\n // FireFox errors may have fileName, lineNumber, and\n // columnNumber properties\n message += '\\n at ' + anyError.fileName;\n if (anyError.lineNumber != null) {\n message += ':' + anyError.lineNumber;\n\n if (anyError.columnNumber != null) {\n message += ':' + anyError.columnNumber;\n }\n }\n\n message += '\\nNo stack';\n } else {\n message += '\\nNo stack or location';\n }\n } else {\n message = String(error);\n }\n\n const space = options.space;\n if (space != null) {\n const lines = message.split('\\n');\n message = [lines[0]]\n .concat(\n lines.slice(1).map(line => {\n return space + line;\n })\n )\n .join('\\n');\n }\n\n return message;\n }\n\n protected _getSource(tracepath: string): string {\n if (tracepath === '<anonymous>') {\n return 'anonymous';\n }\n return tracepath;\n }\n\n /**\n * Creates a unified diff to explain the difference between two objects.\n *\n * @param actual The actual result.\n * @param expected The expected result.\n * @returns A unified diff formatted string representing the difference\n * between the two objects.\n */\n protected _createDiff(actual: string, expected: string): string;\n protected _createDiff(actual: object, expected: object): string;\n protected _createDiff(\n actual: string | object,\n expected: string | object\n ): string {\n // TODO: Remove the casts when the diffJson typings are updated (the\n // current typings are missing the options argument).\n let diff = <Change[]>(<any>diffJson)(actual, expected, {\n undefinedReplacement: null\n });\n if (diff.length === 1 && !diff[0].added && !diff[0].removed) {\n return '';\n }\n\n return diff.reduce((d, { value, added, removed }) => {\n const lastChar = value[value.length - 1] === '\\n' ? '\\n' : '';\n const lines = value.split('\\n');\n if (lastChar === '\\n') {\n lines.pop();\n }\n let prefix = '';\n if (d.length > 0 && d[d.length - 1] !== '\\n') {\n prefix = '\\n';\n }\n const char = added ? 'E' : removed ? 'A' : ' ';\n return d + `${prefix}${char} ` + lines.join(`\\n${char} `) + lastChar;\n }, '');\n }\n\n /**\n * Return a trace line in a standardized format.\n */\n protected _formatLine(data: { func?: string; source: string }) {\n if (!data.func) {\n return ' @ ' + this._getSource(data.source);\n }\n return ' at ' + data.func + ' @ ' + this._getSource(data.source);\n }\n\n /**\n * Parse a stack trace, apply any source mappings, and normalize its format.\n */\n protected _normalizeStackTrace(stack: string) {\n let lines = stack.replace(/\\s+$/, '').split('\\n');\n let firstLine = '';\n\n // strip leading blank lines\n while (/^\\s*$/.test(lines[0])) {\n lines = lines.slice(1);\n }\n\n let stackLines = /^\\s*at /.test(lines[0])\n ? this._processChromeTrace(lines)\n : this._processSafariTrace(lines);\n\n if (this.executor.config.filterErrorStack) {\n stackLines = stackLines.filter(line => {\n return !(\n /\\binternal\\/process\\//.test(line) ||\n /\\bnode_modules\\/(?!digdug|leadfoot)/.test(line) ||\n /\\/__intern\\//.test(line) ||\n /\\bModule\\.(?:runMain|load)/.test(line) ||\n /\\bModule\\._\\w+/.test(line) ||\n /\\bbootstrap_node\\.js/.test(line) ||\n /<module.js[:>]/.test(line) ||\n /<anonymous>$/.test(line) ||\n /<native>$/.test(line)\n );\n });\n }\n\n return '\\n' + firstLine + stackLines.join('\\n');\n }\n\n /**\n * Process Chrome, Opera, and IE traces.\n *\n * Ex)\n * at Object._updateExpressionOptions (AxiomEditor.js:511)\n * at Object.<anonymous> (AxiomEditor.js:291)\n * at Function.m.emit (dojo.js.uncompressed.js:8875)\n */\n protected _processChromeTrace(lines: string[]) {\n return lines.map(line => {\n let match: RegExpMatchArray | null;\n if ((match = /^\\s*at (.+?) \\(([^)]+)\\)$/.exec(line))) {\n return this._formatLine({ func: match[1], source: match[2] });\n } else if ((match = /^\\s*at (.*)/.exec(line))) {\n return this._formatLine({ source: match[1] });\n } else {\n return line;\n }\n });\n }\n\n /**\n * Process Safari and Firefox traces.\n *\n * Ex)\n * _updateExpressionOptions@http://localhost:8080/AxiomEditor.js:511:49\n * http://localhost:8080/AxiomEditor.js:291:34\n * dispatchEvent@[native code]\n * emit@http://ajax.googleapis.com/ajax/libs/dojo/1.12.2/dojo/dojo.js:118:282\n */\n protected _processSafariTrace(lines: string[]) {\n return lines.map(line => {\n let match: RegExpMatchArray | null;\n if ((match = /^([^@]+)@(.*)/.exec(line))) {\n return this._formatLine({ func: match[1], source: match[2] });\n } else if ((match = /^(\\w+:\\/\\/.*)/.exec(line))) {\n return this._formatLine({ source: match[1] });\n } else {\n return line;\n }\n });\n }\n}\n\nexport interface ErrorFormatterProperties {\n executor: Executor;\n}\n\nexport interface ErrorFormatOptions {\n space?: string;\n}\n"]} |