@tryghost/errors 1.0.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/errors.js +43 -2
- package/lib/utils.js +8 -1
- package/package.json +4 -3
package/lib/errors.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const uuid = require('uuid');
|
|
2
2
|
const merge = require('lodash/merge');
|
|
3
3
|
const isString = require('lodash/isString');
|
|
4
|
+
const cloneDeep = require('lodash/cloneDeep');
|
|
4
5
|
const utils = require('./utils');
|
|
5
6
|
|
|
6
7
|
class GhostError extends Error {
|
|
@@ -59,7 +60,10 @@ class GhostError extends Error {
|
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
if (property === 'stack') {
|
|
62
|
-
this
|
|
63
|
+
if (this.hideStack) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
this[property] = utils.wrapStack(this, options.err);
|
|
63
67
|
return;
|
|
64
68
|
}
|
|
65
69
|
|
|
@@ -67,6 +71,34 @@ class GhostError extends Error {
|
|
|
67
71
|
});
|
|
68
72
|
}
|
|
69
73
|
}
|
|
74
|
+
|
|
75
|
+
prepareErrorForUser() {
|
|
76
|
+
const error = cloneDeep(this);
|
|
77
|
+
|
|
78
|
+
let stackbits = error.stack.split(/\n/);
|
|
79
|
+
|
|
80
|
+
// We build this up backwards, so we always insert at position 1
|
|
81
|
+
|
|
82
|
+
if (process.env.NODE_ENV === 'production') {
|
|
83
|
+
stackbits.splice(1, stackbits.length - 1);
|
|
84
|
+
} else {
|
|
85
|
+
// Clearly mark the strack trace
|
|
86
|
+
stackbits.splice(1, 0, `Stack Trace:`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Add in our custom cotext and help methods
|
|
90
|
+
|
|
91
|
+
if (this.help) {
|
|
92
|
+
stackbits.splice(1, 0, `${this.help}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (this.context) {
|
|
96
|
+
stackbits.splice(1, 0, `${this.context}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
error.stack = stackbits.join('\n');
|
|
100
|
+
return error;
|
|
101
|
+
}
|
|
70
102
|
}
|
|
71
103
|
|
|
72
104
|
const ghostErrors = {
|
|
@@ -95,7 +127,8 @@ const ghostErrors = {
|
|
|
95
127
|
super(merge({
|
|
96
128
|
statusCode: 404,
|
|
97
129
|
errorType: 'NotFoundError',
|
|
98
|
-
message: 'Resource could not be found.'
|
|
130
|
+
message: 'Resource could not be found.',
|
|
131
|
+
hideStack: true
|
|
99
132
|
}, options));
|
|
100
133
|
}
|
|
101
134
|
},
|
|
@@ -299,6 +332,14 @@ const ghostErrors = {
|
|
|
299
332
|
hideStack: true
|
|
300
333
|
}, options));
|
|
301
334
|
}
|
|
335
|
+
},
|
|
336
|
+
ConflictError: class ConflictError extends GhostError {
|
|
337
|
+
constructor(options) {
|
|
338
|
+
super(merge({
|
|
339
|
+
errorType: 'ConflictError',
|
|
340
|
+
statusCode: 409
|
|
341
|
+
}, options));
|
|
342
|
+
}
|
|
302
343
|
}
|
|
303
344
|
};
|
|
304
345
|
|
package/lib/utils.js
CHANGED
|
@@ -127,6 +127,12 @@ _private.JSONAPIDeserialize = function JSONAPIDeserialize(errorFormat) {
|
|
|
127
127
|
return internalError;
|
|
128
128
|
};
|
|
129
129
|
|
|
130
|
+
exports.wrapStack = function wrapStack(err, internalErr) {
|
|
131
|
+
const extraLine = err.stack.split(/\n/g)[1];
|
|
132
|
+
const [firstLine, ...rest] = internalErr.stack.split(/\n/g);
|
|
133
|
+
return [firstLine, extraLine, ...rest].join('\n');
|
|
134
|
+
};
|
|
135
|
+
|
|
130
136
|
/**
|
|
131
137
|
* @description Serialize GhostError instance to error JSON format
|
|
132
138
|
*
|
|
@@ -182,6 +188,7 @@ exports.deserialize = function deserialize(errorFormat) {
|
|
|
182
188
|
*/
|
|
183
189
|
exports.isGhostError = function isGhostError(err) {
|
|
184
190
|
const errorName = this.GhostError.name;
|
|
191
|
+
const legacyErrorName = 'IgnitionError';
|
|
185
192
|
|
|
186
193
|
const recursiveIsGhostError = function recursiveIsGhostError(obj) {
|
|
187
194
|
// no super constructor available anymore
|
|
@@ -189,7 +196,7 @@ exports.isGhostError = function isGhostError(err) {
|
|
|
189
196
|
return false;
|
|
190
197
|
}
|
|
191
198
|
|
|
192
|
-
if (obj.name === errorName) {
|
|
199
|
+
if (obj.name === errorName || obj.name === legacyErrorName) {
|
|
193
200
|
return true;
|
|
194
201
|
}
|
|
195
202
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/errors",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"repository": "https://github.com/TryGhost/Utils/tree/main/packages/errors",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"sinon": "11.1.2"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"lodash": "^4.17.21"
|
|
28
|
+
"lodash": "^4.17.21",
|
|
29
|
+
"uuid": "^8.3.2"
|
|
29
30
|
},
|
|
30
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "deb1552864a7da3430e0f0854ff013b5d0aa4a0c"
|
|
31
32
|
}
|