1 | 'use strict';
|
2 | const path = require('path');
|
3 | const cleanYamlObject = require('clean-yaml-object');
|
4 | const concordance = require('concordance');
|
5 | const isError = require('is-error');
|
6 | const StackUtils = require('stack-utils');
|
7 | const assert = require('./assert');
|
8 | const beautifyStack = require('./beautify-stack');
|
9 | const concordanceOptions = require('./concordance-options').default;
|
10 |
|
11 | function isAvaAssertionError(source) {
|
12 | return source instanceof assert.AssertionError;
|
13 | }
|
14 |
|
15 | function filter(propertyName, isRoot) {
|
16 | return !isRoot || (propertyName !== 'message' && propertyName !== 'name' && propertyName !== 'stack');
|
17 | }
|
18 |
|
19 | const stackUtils = new StackUtils();
|
20 | function extractSource(stack) {
|
21 | if (!stack) {
|
22 | return null;
|
23 | }
|
24 |
|
25 | const firstStackLine = stack.split('\n')[0];
|
26 | return stackUtils.parseLine(firstStackLine);
|
27 | }
|
28 |
|
29 | function buildSource(source) {
|
30 | if (!source) {
|
31 | return null;
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | const projectDir = process.cwd();
|
38 |
|
39 | const file = path.resolve(projectDir, source.file.trim());
|
40 | const rel = path.relative(projectDir, file);
|
41 |
|
42 | const isWithinProject = rel.split(path.sep)[0] !== '..';
|
43 | const isDependency = isWithinProject && path.dirname(rel).split(path.sep).includes('node_modules');
|
44 |
|
45 | return {
|
46 | isDependency,
|
47 | isWithinProject,
|
48 | file,
|
49 | line: source.line
|
50 | };
|
51 | }
|
52 |
|
53 | function trySerializeError(err, shouldBeautifyStack) {
|
54 | let stack = err.savedError ? err.savedError.stack : err.stack;
|
55 |
|
56 | if (shouldBeautifyStack) {
|
57 | stack = beautifyStack(stack);
|
58 | }
|
59 |
|
60 | const retval = {
|
61 | avaAssertionError: isAvaAssertionError(err),
|
62 | nonErrorObject: false,
|
63 | source: buildSource(extractSource(stack)),
|
64 | stack
|
65 | };
|
66 |
|
67 | if (err.actualStack) {
|
68 | retval.stack = shouldBeautifyStack ? beautifyStack(err.actualStack) : err.actualStack;
|
69 | }
|
70 |
|
71 | if (retval.avaAssertionError) {
|
72 | retval.improperUsage = err.improperUsage;
|
73 | retval.message = err.message;
|
74 | retval.name = err.name;
|
75 | retval.statements = err.statements;
|
76 | retval.values = err.values;
|
77 |
|
78 | if (err.fixedSource) {
|
79 | const source = buildSource(err.fixedSource);
|
80 | if (source) {
|
81 | retval.source = source;
|
82 | }
|
83 | }
|
84 |
|
85 | if (err.assertion) {
|
86 | retval.assertion = err.assertion;
|
87 | }
|
88 |
|
89 | if (err.operator) {
|
90 | retval.operator = err.operator;
|
91 | }
|
92 | } else {
|
93 | retval.object = cleanYamlObject(err, filter);
|
94 | if (typeof err.message === 'string') {
|
95 | retval.message = err.message;
|
96 | }
|
97 |
|
98 | if (typeof err.name === 'string') {
|
99 | retval.name = err.name;
|
100 | }
|
101 | }
|
102 |
|
103 | if (typeof err.stack === 'string') {
|
104 | const lines = err.stack.split('\n');
|
105 | if (err.name === 'SyntaxError' && !lines[0].startsWith('SyntaxError')) {
|
106 | retval.summary = '';
|
107 | for (const line of lines) {
|
108 | retval.summary += line + '\n';
|
109 | if (line.startsWith('SyntaxError')) {
|
110 | break;
|
111 | }
|
112 | }
|
113 |
|
114 | retval.summary = retval.summary.trim();
|
115 | } else {
|
116 |
|
117 |
|
118 | const start = lines.findIndex(line => !/:\d+$/.test(line));
|
119 | retval.summary = '';
|
120 | for (let index = start; index < lines.length; index++) {
|
121 | if (lines[index].startsWith(' at')) {
|
122 | break;
|
123 | }
|
124 |
|
125 | const next = index + 1;
|
126 | const end = next === lines.length || lines[next].startsWith(' at');
|
127 | retval.summary += end ? lines[index] : lines[index] + '\n';
|
128 | }
|
129 | }
|
130 | }
|
131 |
|
132 | return retval;
|
133 | }
|
134 |
|
135 | function serializeError(origin, shouldBeautifyStack, err) {
|
136 | if (!isError(err)) {
|
137 | return {
|
138 | avaAssertionError: false,
|
139 | nonErrorObject: true,
|
140 | formatted: concordance.formatDescriptor(concordance.describe(err, concordanceOptions), concordanceOptions)
|
141 | };
|
142 | }
|
143 |
|
144 | try {
|
145 | return trySerializeError(err, shouldBeautifyStack);
|
146 | } catch (_) {
|
147 | const replacement = new Error(`${origin}: Could not serialize error`);
|
148 | return {
|
149 | avaAssertionError: false,
|
150 | nonErrorObject: false,
|
151 | name: replacement.name,
|
152 | message: replacement.message,
|
153 | stack: replacement.stack,
|
154 | summary: replacement.message
|
155 | };
|
156 | }
|
157 | }
|
158 |
|
159 | module.exports = serializeError;
|