@tryghost/errors 1.2.2 → 1.2.5

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.
Files changed (3) hide show
  1. package/lib/errors.js +22 -26
  2. package/lib/utils.js +19 -23
  3. package/package.json +5 -4
package/lib/errors.js CHANGED
@@ -4,9 +4,7 @@ const isString = require('lodash/isString');
4
4
  const utils = require('./utils');
5
5
 
6
6
  class GhostError extends Error {
7
- constructor(options) {
8
- options = options || {};
9
-
7
+ constructor(options = {}) {
10
8
  super();
11
9
 
12
10
  /**
@@ -34,7 +32,7 @@ class GhostError extends Error {
34
32
  this.redirect = options.redirect || null;
35
33
 
36
34
  this.message = options.message || this.message;
37
- this.hideStack = options.hideStack;
35
+ this.hideStack = options.hideStack || false;
38
36
 
39
37
  // NOTE: Error to inherit from, override!
40
38
  // Nested objects are getting copied over in one piece (can be changed, but not needed right now)
@@ -52,16 +50,13 @@ class GhostError extends Error {
52
50
  }
53
51
 
54
52
  // CASE: `code` should put options as priority over err
55
- if (property === 'code') {
53
+ if (property === 'code') {
56
54
  // @ts-ignore
57
55
  this[property] = this[property] || options.err[property];
58
56
  return;
59
57
  }
60
58
 
61
- if (property === 'stack') {
62
- if (this.hideStack) {
63
- return;
64
- }
59
+ if (property === 'stack' && !this.hideStack) {
65
60
  this[property] = utils.wrapStack(this, options.err);
66
61
  return;
67
62
  }
@@ -206,7 +201,8 @@ const ghostErrors = {
206
201
  constructor(options) {
207
202
  super(merge({
208
203
  statusCode: 500,
209
- errorType: 'DataExportError'
204
+ errorType: 'DataExportError',
205
+ message: 'The server encountered an error whilst exporting data.'
210
206
  }, options));
211
207
  }
212
208
  },
@@ -214,16 +210,8 @@ const ghostErrors = {
214
210
  constructor(options) {
215
211
  super(merge({
216
212
  statusCode: 500,
217
- errorType: 'DataImportError'
218
- }, options));
219
- }
220
- },
221
- DatabaseVersionError: class DatabaseVersionError extends GhostError {
222
- constructor(options) {
223
- super(merge({
224
- hideStack: true,
225
- statusCode: 500,
226
- errorType: 'DatabaseVersionError'
213
+ errorType: 'DataImportError',
214
+ message: 'The server encountered an error whilst importing data.'
227
215
  }, options));
228
216
  }
229
217
  },
@@ -231,7 +219,8 @@ const ghostErrors = {
231
219
  constructor(options) {
232
220
  super(merge({
233
221
  statusCode: 500,
234
- errorType: 'EmailError'
222
+ errorType: 'EmailError',
223
+ message: 'The server encountered an error whilst sending email.'
235
224
  }, options));
236
225
  }
237
226
  },
@@ -240,6 +229,7 @@ const ghostErrors = {
240
229
  super(merge({
241
230
  statusCode: 422,
242
231
  errorType: 'ThemeValidationError',
232
+ message: 'The theme has a validation error.',
243
233
  errorDetails: {}
244
234
  }, options));
245
235
  }
@@ -248,7 +238,8 @@ const ghostErrors = {
248
238
  constructor(options) {
249
239
  super(merge({
250
240
  statusCode: 409,
251
- errorType: 'DisabledFeatureError'
241
+ errorType: 'DisabledFeatureError',
242
+ message: 'Unable to complete the request, this feature is disabled.'
252
243
  }, options));
253
244
  }
254
245
  },
@@ -256,7 +247,8 @@ const ghostErrors = {
256
247
  constructor(options) {
257
248
  super(merge({
258
249
  statusCode: 409,
259
- errorType: 'UpdateCollisionError'
250
+ errorType: 'UpdateCollisionError',
251
+ message: 'Unable to complete the request, there was a conflict.'
260
252
  }, options));
261
253
  }
262
254
  },
@@ -265,7 +257,8 @@ const ghostErrors = {
265
257
  super(merge({
266
258
  errorType: 'HostLimitError',
267
259
  hideStack: true,
268
- statusCode: 403
260
+ statusCode: 403,
261
+ message: 'Unable to complete the request, this resource is limited.'
269
262
  }, options));
270
263
  }
271
264
  },
@@ -273,7 +266,9 @@ const ghostErrors = {
273
266
  constructor(options) {
274
267
  super(merge({
275
268
  errorType: 'HelperWarning',
276
- hideStack: true
269
+ hideStack: true,
270
+ statusCode: 400,
271
+ message: 'A theme helper has done something unexpected.'
277
272
  }, options));
278
273
  }
279
274
  },
@@ -308,7 +303,8 @@ const ghostErrors = {
308
303
  constructor(options) {
309
304
  super(merge({
310
305
  errorType: 'ConflictError',
311
- statusCode: 409
306
+ statusCode: 409,
307
+ message: 'The server has encountered an conflict.'
312
308
  }, options));
313
309
  }
314
310
  }
package/lib/utils.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const omit = require('lodash/omit');
2
2
  const merge = require('lodash/merge');
3
3
  const extend = require('lodash/extend');
4
+ const cloneError = require('utils-copy-error');
4
5
  const _private = {};
5
6
 
6
7
  _private.serialize = function serialize(err) {
@@ -27,21 +28,15 @@ _private.serialize = function serialize(err) {
27
28
  };
28
29
 
29
30
  _private.deserialize = function deserialize(obj) {
30
- try {
31
- return {
32
- id: obj.id,
33
- message: obj.detail || obj.error_description || obj.message,
34
- statusCode: obj.status,
35
- code: obj.code || obj.error,
36
- level: obj.meta && obj.meta.level,
37
- help: obj.meta && obj.meta.help,
38
- context: obj.meta && obj.meta.context
39
- };
40
- } catch (err) {
41
- return {
42
- message: 'Something went wrong.'
43
- };
44
- }
31
+ return {
32
+ id: obj.id,
33
+ message: obj.detail || obj.error_description || obj.message,
34
+ statusCode: obj.status,
35
+ code: obj.code || obj.error,
36
+ level: obj.meta && obj.meta.level,
37
+ help: obj.meta && obj.meta.help,
38
+ context: obj.meta && obj.meta.context
39
+ };
45
40
  };
46
41
 
47
42
  /**
@@ -186,6 +181,7 @@ exports.deserialize = function deserialize(errorFormat) {
186
181
  /**
187
182
  * @description Replace the stack with a user-facing one
188
183
  * @params {Error} err
184
+ * @returns {Error} Clone of the original error with a user-facing stack
189
185
  */
190
186
  exports.prepareStackForUser = function prepareStackForUser(error) {
191
187
  let stackbits = error.stack.split(/\n/);
@@ -200,17 +196,17 @@ exports.prepareStackForUser = function prepareStackForUser(error) {
200
196
  }
201
197
 
202
198
  // Add in our custom cotext and help methods
203
-
204
- if (this.help) {
205
- stackbits.splice(1, 0, `${this.help}`);
199
+ if (error.help) {
200
+ stackbits.splice(1, 0, `${error.help}`);
206
201
  }
207
202
 
208
- if (this.context) {
209
- stackbits.splice(1, 0, `${this.context}`);
203
+ if (error.context) {
204
+ stackbits.splice(1, 0, `${error.context}`);
210
205
  }
211
206
 
212
- error.stack = stackbits.join('\n');
213
- return error;
207
+ const errorClone = cloneError(error);
208
+ errorClone.stack = stackbits.join('\n');
209
+ return errorClone;
214
210
  };
215
211
 
216
212
  /**
@@ -234,4 +230,4 @@ exports.isGhostError = function isGhostError(err) {
234
230
  };
235
231
 
236
232
  return recursiveIsGhostError(err.constructor);
237
- };
233
+ };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryghost/errors",
3
- "version": "1.2.2",
3
+ "version": "1.2.5",
4
4
  "repository": "https://github.com/TryGhost/framework/tree/main/packages/errors",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "dev": "echo \"Implement me!\"",
10
- "test": "NODE_ENV=testing c8 --reporter text --reporter cobertura mocha './test/**/*.test.js'",
10
+ "test": "NODE_ENV=testing c8 --check-coverage --all --reporter text --reporter cobertura mocha './test/**/*.test.js'",
11
11
  "lint": "eslint . --ext .js --cache",
12
12
  "posttest": "yarn lint"
13
13
  },
@@ -20,13 +20,14 @@
20
20
  },
21
21
  "devDependencies": {
22
22
  "c8": "7.11.0",
23
- "mocha": "9.2.0",
23
+ "mocha": "9.2.1",
24
24
  "should": "13.2.3",
25
25
  "sinon": "13.0.1"
26
26
  },
27
27
  "dependencies": {
28
28
  "lodash": "^4.17.21",
29
+ "utils-copy-error": "1.0.1",
29
30
  "uuid": "^8.3.2"
30
31
  },
31
- "gitHead": "e954893434fda3f8c8de1970aaaa6f6a8b297f5d"
32
+ "gitHead": "d09bf53d3435dd9782cea9c5a7062e07538a2b0b"
32
33
  }