1 | (function (factory) {
|
2 | if (typeof module === "object" && typeof module.exports === "object") {
|
3 | var v = factory(require, exports);
|
4 | if (v !== undefined) module.exports = v;
|
5 | }
|
6 | else if (typeof define === "function" && define.amd) {
|
7 | define(["require", "exports", "diff"], factory);
|
8 | }
|
9 | })(function (require, exports) {
|
10 | "use strict";
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | var diff_1 = require("diff");
|
13 | var ErrorFormatter = (function () {
|
14 | function ErrorFormatter(executor) {
|
15 | this.executor = executor;
|
16 | }
|
17 | ErrorFormatter.prototype.format = function (error, options) {
|
18 | options = options || {};
|
19 | var message;
|
20 | if (typeof error !== 'string' && (error.message || error.stack)) {
|
21 | message =
|
22 | (error.name || 'Error') + ': ' + (error.message || 'Unknown error');
|
23 | var stack = error.stack;
|
24 | if (stack) {
|
25 | if (stack.indexOf(message) === 0) {
|
26 | stack = stack.slice(message.length);
|
27 | }
|
28 | else if (stack.indexOf(error.message) === 0) {
|
29 | stack = stack.slice(String(error.message).length);
|
30 | }
|
31 | else if (stack.indexOf('Error\n') === 0) {
|
32 | stack = stack.slice('Error'.length);
|
33 | }
|
34 | stack = this._normalizeStackTrace(stack);
|
35 | }
|
36 | var anyError = error;
|
37 | if ((anyError.showDiff &&
|
38 | (typeof anyError.actual === 'object' &&
|
39 | typeof anyError.expected === 'object')) ||
|
40 | (typeof anyError.actual === 'string' &&
|
41 | typeof anyError.expected === 'string')) {
|
42 | var diff = this._createDiff(anyError.actual, anyError.expected);
|
43 | if (diff) {
|
44 | message += '\n\n' + diff + '\n';
|
45 | }
|
46 | }
|
47 | if (stack && /\S/.test(stack)) {
|
48 | message += stack;
|
49 | }
|
50 | else if (anyError.fileName) {
|
51 | message += '\n at ' + anyError.fileName;
|
52 | if (anyError.lineNumber != null) {
|
53 | message += ':' + anyError.lineNumber;
|
54 | if (anyError.columnNumber != null) {
|
55 | message += ':' + anyError.columnNumber;
|
56 | }
|
57 | }
|
58 | message += '\nNo stack';
|
59 | }
|
60 | else {
|
61 | message += '\nNo stack or location';
|
62 | }
|
63 | }
|
64 | else {
|
65 | message = String(error);
|
66 | }
|
67 | var space = options.space;
|
68 | if (space != null) {
|
69 | var lines = message.split('\n');
|
70 | message = [lines[0]]
|
71 | .concat(lines.slice(1).map(function (line) {
|
72 | return space + line;
|
73 | }))
|
74 | .join('\n');
|
75 | }
|
76 | return message;
|
77 | };
|
78 | ErrorFormatter.prototype._getSource = function (tracepath) {
|
79 | if (tracepath === '<anonymous>') {
|
80 | return 'anonymous';
|
81 | }
|
82 | return tracepath;
|
83 | };
|
84 | ErrorFormatter.prototype._createDiff = function (actual, expected) {
|
85 | var diff = diff_1.diffJson(actual, expected, {
|
86 | undefinedReplacement: null
|
87 | });
|
88 | if (diff.length === 1 && !diff[0].added && !diff[0].removed) {
|
89 | return '';
|
90 | }
|
91 | return diff.reduce(function (d, _a) {
|
92 | var value = _a.value, added = _a.added, removed = _a.removed;
|
93 | var lastChar = value[value.length - 1] === '\n' ? '\n' : '';
|
94 | var lines = value.split('\n');
|
95 | if (lastChar === '\n') {
|
96 | lines.pop();
|
97 | }
|
98 | var prefix = '';
|
99 | if (d.length > 0 && d[d.length - 1] !== '\n') {
|
100 | prefix = '\n';
|
101 | }
|
102 | var char = added ? 'E' : removed ? 'A' : ' ';
|
103 | return d + ("" + prefix + char + " ") + lines.join("\n" + char + " ") + lastChar;
|
104 | }, '');
|
105 | };
|
106 | ErrorFormatter.prototype._formatLine = function (data) {
|
107 | if (!data.func) {
|
108 | return ' @ ' + this._getSource(data.source);
|
109 | }
|
110 | return ' at ' + data.func + ' @ ' + this._getSource(data.source);
|
111 | };
|
112 | ErrorFormatter.prototype._normalizeStackTrace = function (stack) {
|
113 | var lines = stack.replace(/\s+$/, '').split('\n');
|
114 | var firstLine = '';
|
115 | while (/^\s*$/.test(lines[0])) {
|
116 | lines = lines.slice(1);
|
117 | }
|
118 | var stackLines = /^\s*at /.test(lines[0])
|
119 | ? this._processChromeTrace(lines)
|
120 | : this._processSafariTrace(lines);
|
121 | if (this.executor.config.filterErrorStack) {
|
122 | stackLines = stackLines.filter(function (line) {
|
123 | return !(/\binternal\/process\//.test(line) ||
|
124 | /\bnode_modules\/(?!digdug|leadfoot)/.test(line) ||
|
125 | /\/__intern\
|
126 | /\bModule\.(?:runMain|load)/.test(line) ||
|
127 | /\bModule\._\w+/.test(line) ||
|
128 | /\bbootstrap_node\.js/.test(line) ||
|
129 | /<module.js[:>]/.test(line) ||
|
130 | /<anonymous>$/.test(line) ||
|
131 | /<native>$/.test(line));
|
132 | });
|
133 | }
|
134 | return '\n' + firstLine + stackLines.join('\n');
|
135 | };
|
136 | ErrorFormatter.prototype._processChromeTrace = function (lines) {
|
137 | var _this = this;
|
138 | return lines.map(function (line) {
|
139 | var match;
|
140 | if ((match = /^\s*at (.+?) \(([^)]+)\)$/.exec(line))) {
|
141 | return _this._formatLine({ func: match[1], source: match[2] });
|
142 | }
|
143 | else if ((match = /^\s*at (.*)/.exec(line))) {
|
144 | return _this._formatLine({ source: match[1] });
|
145 | }
|
146 | else {
|
147 | return line;
|
148 | }
|
149 | });
|
150 | };
|
151 | ErrorFormatter.prototype._processSafariTrace = function (lines) {
|
152 | var _this = this;
|
153 | return lines.map(function (line) {
|
154 | var match;
|
155 | if ((match = /^([^@]+)@(.*)/.exec(line))) {
|
156 | return _this._formatLine({ func: match[1], source: match[2] });
|
157 | }
|
158 | else if ((match = /^(\w+:\/\/.*)/.exec(line))) {
|
159 | return _this._formatLine({ source: match[1] });
|
160 | }
|
161 | else {
|
162 | return line;
|
163 | }
|
164 | });
|
165 | };
|
166 | return ErrorFormatter;
|
167 | }());
|
168 | exports.default = ErrorFormatter;
|
169 | });
|
170 | //# sourceMappingURL=ErrorFormatter.js.map |
\ | No newline at end of file |