-
Notifications
You must be signed in to change notification settings - Fork 0
/
error-message.js
66 lines (59 loc) · 1.52 KB
/
error-message.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
/* eslint-disable jsdoc/require-returns-description */
/* eslint-disable jsdoc/require-param-description */
/* eslint-disable jsdoc/valid-types */
// @ts-check
const fullTypeName = require('./full-type-name');
/**
* @typedef {import("./check-error.js").CheckError} CheckError
*/
/**
* @param {CheckError} error
* @param {string | number} space
* @returns {string}
*/
module.exports = function errorMessage(error, space = 2) {
const name = error.errorName;
let spaceStr = '';
if (space) {
spaceStr += '\n';
if (typeof space === 'number') {
spaceStr += ' '.repeat(space);
} else {
spaceStr += space;
}
}
let pathText = '';
if (error.path && error.path.length !== 0) {
if (space) {
pathText += spaceStr;
} else {
pathText += ' ';
}
pathText += `at '${error.path.join('.')}'`;
}
let typeText = '';
if (error.type) {
const typeName = fullTypeName(error.type);
typeText = `with type '${typeName}'`;
if (space) {
typeText = spaceStr + typeText;
} else {
typeText = ' ' + typeText;
}
}
let infoText = '';
if (error.info) {
const info = {...error.info};
if (info.errors) {
info.errors = info.errors.map((error) => errorMessage(error, ''));
}
let infoStr = JSON.stringify(info, null, space);
if (space) {
infoStr = infoStr.replace(/[\n]/g, spaceStr);
infoText = `${spaceStr}info: ${infoStr}`;
} else {
infoText = `: ${infoStr}`;
}
}
return `${name}${pathText}${typeText}${infoText}`;
};