/
serialize-error.js
204 lines (178 loc) 路 6.33 KB
/
serialize-error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
require('../lib/chalk').set();
require('../lib/worker/options').set({});
const fs = require('fs');
const path = require('path');
const sourceMapFixtures = require('source-map-fixtures');
const sourceMapSupport = require('source-map-support');
const tempWrite = require('temp-write');
const uniqueTempDir = require('unique-temp-dir');
const {test} = require('tap');
const avaAssert = require('../lib/assert');
const beautifyStack = require('../lib/beautify-stack');
const serializeError = require('../lib/serialize-error');
const serialize = error => serializeError('Test', true, error);
// Needed to test stack traces from source map fixtures.
sourceMapSupport.install({environment: 'node'});
test('serialize standard props', t => {
const error = new Error('Hello');
const serializedError = serialize(error);
t.is(Object.keys(serializedError).length, 8);
t.is(serializedError.avaAssertionError, false);
t.is(serializedError.nonErrorObject, false);
t.deepEqual(serializedError.object, {});
t.is(serializedError.name, 'Error');
t.is(serializedError.stack, beautifyStack(error.stack));
t.is(serializedError.message, 'Hello');
t.is(serializedError.summary, 'Error: Hello');
t.is(typeof serializedError.source.isDependency, 'boolean');
t.is(typeof serializedError.source.isWithinProject, 'boolean');
t.is(typeof serializedError.source.file, 'string');
t.is(typeof serializedError.source.line, 'number');
t.end();
});
test('additional error properties are preserved', t => {
const serializedError = serialize(Object.assign(new Error(), {foo: 'bar'}));
t.deepEqual(serializedError.object, {foo: 'bar'});
t.end();
});
test('source file is an absolute path', t => {
const error = new Error('Hello');
const serializedError = serialize(error);
t.is(serializedError.source.file, __filename);
t.end();
});
test('source file is an absolute path, after source map correction', t => {
const fixture = sourceMapFixtures.mapFile('throws');
try {
fixture.require().run();
t.fail('Fixture should have thrown');
} catch (error) {
const serializedError = serialize(error);
t.is(serializedError.source.file, fixture.sourceFile);
t.end();
}
});
test('source file is an absolute path, after source map correction, even if already absolute', t => {
const fixture = sourceMapFixtures.mapFile('throws');
const map = JSON.parse(fs.readFileSync(fixture.file + '.map'));
const tmp = uniqueTempDir({create: true});
const sourceRoot = path.join(tmp, 'src');
const expectedSourceFile = path.join(sourceRoot, map.file);
const tmpFile = path.join(tmp, path.basename(fixture.file));
fs.writeFileSync(tmpFile, fs.readFileSync(fixture.file));
fs.writeFileSync(tmpFile + '.map', JSON.stringify(Object.assign(map, {sourceRoot}), null, 2));
try {
require(tmpFile).run();
t.fail('Fixture should have thrown');
} catch (error) {
const serializedError = serialize(error);
t.is(serializedError.source.file, expectedSourceFile);
t.end();
}
});
test('determines whether source file is within the project', t => {
const file = tempWrite.sync('module.exports = () => { throw new Error("hello") }');
try {
require(file)();
t.fail('Should have thrown');
} catch (error2) {
const serializedError = serialize(error2);
t.is(serializedError.source.file, file);
t.is(serializedError.source.isWithinProject, false);
}
const error = new Error('Hello');
const serializedError = serialize(error);
t.is(serializedError.source.file, __filename);
t.is(serializedError.source.isWithinProject, true);
t.end();
});
test('determines whether source file, if within the project, is a dependency', t => {
const fixture = sourceMapFixtures.mapFile('throws');
try {
fixture.require().run();
t.fail('Fixture should have thrown');
} catch (error2) {
const serializedError = serialize(error2);
t.is(serializedError.source.file, fixture.sourceFile);
t.is(serializedError.source.isWithinProject, true);
t.is(serializedError.source.isDependency, true);
}
const error = new Error('Hello');
const serializedError = serialize(error);
t.is(serializedError.source.file, __filename);
t.is(serializedError.source.isDependency, false);
t.end();
});
test('sets avaAssertionError to true if indeed an assertion error', t => {
const error = new avaAssert.AssertionError({});
const serializedError = serialize(error);
t.true(serializedError.avaAssertionError);
t.end();
});
test('includes statements of assertion errors', t => {
const error = new avaAssert.AssertionError({
assertion: 'true'
});
error.statements = [
['actual.a[0]', '1'],
['actual.a', '[1]'],
['actual', '{a: [1]}']
];
const serializedError = serialize(error);
t.is(serializedError.statements, error.statements);
t.end();
});
test('includes values of assertion errors', t => {
const error = new avaAssert.AssertionError({
assertion: 'is',
values: [{label: 'actual:', formatted: '1'}, {label: 'expected:', formatted: 'a'}]
});
const serializedError = serialize(error);
t.is(serializedError.values, error.values);
t.end();
});
test('remove non-string error properties', t => {
const error = {
name: [42],
stack: /re/g
};
const serializedError = serialize(error);
t.is(serializedError.name, undefined);
t.is(serializedError.stack, undefined);
t.end();
});
test('creates multiline summaries for syntax errors', t => {
const error = new SyntaxError();
Object.defineProperty(error, 'stack', {
value: 'Hello\nThere\nSyntaxError here\nIgnore me'
});
const serializedError = serialize(error);
t.is(serializedError.name, 'SyntaxError');
t.is(serializedError.summary, 'Hello\nThere\nSyntaxError here');
t.end();
});
test('skips esm enhancement lines when finding the summary', t => {
const error = new Error();
Object.defineProperty(error, 'stack', {
value: 'file://file.js:1\nFirst line\nSecond line'
});
const serializedError = serialize(error);
t.is(serializedError.summary, 'First line\nSecond line');
t.end();
});
test('works around esm\'s insertion of file:// urls', t => {
const fixture = sourceMapFixtures.mapFile('throws');
try {
fixture.require().run();
t.fail('Fixture should have thrown');
} catch (error) {
const expected = serialize(error);
Object.defineProperty(error, 'stack', {
value: error.stack.split('\n').map(line => line.replace('(/', '(file:///')).join('\n')
});
const serializedError = serialize(error);
t.is(serializedError.source.file, expected.source.file);
t.end();
}
});