@tryghost/errors 1.0.3 → 1.2.0

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 CHANGED
@@ -59,7 +59,10 @@ class GhostError extends Error {
59
59
  }
60
60
 
61
61
  if (property === 'stack') {
62
- this[property] += '\n\n' + options.err[property];
62
+ if (this.hideStack) {
63
+ return;
64
+ }
65
+ this[property] = utils.wrapStack(this, options.err);
63
66
  return;
64
67
  }
65
68
 
@@ -95,7 +98,8 @@ const ghostErrors = {
95
98
  super(merge({
96
99
  statusCode: 404,
97
100
  errorType: 'NotFoundError',
98
- message: 'Resource could not be found.'
101
+ message: 'Resource could not be found.',
102
+ hideStack: true
99
103
  }, options));
100
104
  }
101
105
  },
@@ -299,6 +303,14 @@ const ghostErrors = {
299
303
  hideStack: true
300
304
  }, options));
301
305
  }
306
+ },
307
+ ConflictError: class ConflictError extends GhostError {
308
+ constructor(options) {
309
+ super(merge({
310
+ errorType: 'ConflictError',
311
+ statusCode: 409
312
+ }, options));
313
+ }
302
314
  }
303
315
  };
304
316
 
@@ -308,5 +320,6 @@ const ghostErrorsWithBase = Object.assign({}, ghostErrors, {GhostError});
308
320
  module.exports.utils = {
309
321
  serialize: utils.serialize.bind(ghostErrorsWithBase),
310
322
  deserialize: utils.deserialize.bind(ghostErrorsWithBase),
311
- isGhostError: utils.isGhostError.bind(ghostErrorsWithBase)
323
+ isGhostError: utils.isGhostError.bind(ghostErrorsWithBase),
324
+ prepareStackForUser: utils.prepareStackForUser
312
325
  };
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
  *
@@ -177,11 +183,42 @@ exports.deserialize = function deserialize(errorFormat) {
177
183
  return internalError;
178
184
  };
179
185
 
186
+ /**
187
+ * @description Replace the stack with a user-facing one
188
+ * @params {Error} err
189
+ */
190
+ exports.prepareStackForUser = function prepareStackForUser(error) {
191
+ let stackbits = error.stack.split(/\n/);
192
+
193
+ // We build this up backwards, so we always insert at position 1
194
+
195
+ if (process.env.NODE_ENV === 'production') {
196
+ stackbits.splice(1, stackbits.length - 1);
197
+ } else {
198
+ // Clearly mark the strack trace
199
+ stackbits.splice(1, 0, `Stack Trace:`);
200
+ }
201
+
202
+ // Add in our custom cotext and help methods
203
+
204
+ if (this.help) {
205
+ stackbits.splice(1, 0, `${this.help}`);
206
+ }
207
+
208
+ if (this.context) {
209
+ stackbits.splice(1, 0, `${this.context}`);
210
+ }
211
+
212
+ error.stack = stackbits.join('\n');
213
+ return error;
214
+ };
215
+
180
216
  /**
181
217
  * @description Check whether an error instance is a GhostError.
182
218
  */
183
219
  exports.isGhostError = function isGhostError(err) {
184
220
  const errorName = this.GhostError.name;
221
+ const legacyErrorName = 'IgnitionError';
185
222
 
186
223
  const recursiveIsGhostError = function recursiveIsGhostError(obj) {
187
224
  // no super constructor available anymore
@@ -189,7 +226,7 @@ exports.isGhostError = function isGhostError(err) {
189
226
  return false;
190
227
  }
191
228
 
192
- if (obj.name === errorName) {
229
+ if (obj.name === errorName || obj.name === legacyErrorName) {
193
230
  return true;
194
231
  }
195
232
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/errors",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "repository": "https://github.com/TryGhost/Utils/tree/main/packages/errors",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -28,5 +28,5 @@
28
28
  "lodash": "^4.17.21",
29
29
  "uuid": "^8.3.2"
30
30
  },
31
- "gitHead": "d4cd268b6c74e681b17497db0a50af9d87d1d4e3"
31
+ "gitHead": "8ecf8264002298829fbce686a1bd0ad413e2e01b"
32
32
  }