@tryghost/errors 1.0.4 → 1.2.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2013-2021 Ghost Foundation
3
+ Copyright (c) 2013-2022 Ghost Foundation
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -36,4 +36,4 @@ Follow the instructions for the top-level repo.
36
36
 
37
37
  # Copyright & License
38
38
 
39
- Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
39
+ Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
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,6 +183,36 @@ 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
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/errors",
3
- "version": "1.0.4",
3
+ "version": "1.2.1",
4
4
  "repository": "https://github.com/TryGhost/Utils/tree/main/packages/errors",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "c8": "7.10.0",
22
+ "c8": "7.11.0",
23
23
  "mocha": "9.1.3",
24
24
  "should": "13.2.3",
25
25
  "sinon": "11.1.2"
@@ -28,5 +28,5 @@
28
28
  "lodash": "^4.17.21",
29
29
  "uuid": "^8.3.2"
30
30
  },
31
- "gitHead": "646fbc5aac374659c3a469ecf99dc5defb1901cf"
31
+ "gitHead": "d9374c7c0a1bd92f53b4bf330797a890cdae7e6d"
32
32
  }