@tryghost/mw-error-handler 0.1.4 → 0.1.7

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.
@@ -36,7 +36,8 @@ const messages = {
36
36
  HostLimitError: 'Host Limit error, cannot {action}.',
37
37
  DisabledFeatureError: 'Theme validation error, the {{{helperName}}} helper is not available. Cannot {action}.',
38
38
  UpdateCollisionError: 'Saving failed! Someone else is editing this post.'
39
- }
39
+ },
40
+ UnknownError: 'Unknown error - {name}, cannot {action}.'
40
41
  };
41
42
 
42
43
  /**
@@ -78,8 +79,6 @@ module.exports.prepareError = (err, req, res, next) => {
78
79
  // alternative for res.status();
79
80
  res.statusCode = err.statusCode;
80
81
 
81
- prepareStackForUser(err);
82
-
83
82
  // never cache errors
84
83
  res.set({
85
84
  'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
@@ -88,6 +87,12 @@ module.exports.prepareError = (err, req, res, next) => {
88
87
  next(err);
89
88
  };
90
89
 
90
+ module.exports.prepareStack = (err, req, res, next) => { // eslint-disable-line no-unused-vars
91
+ const clonedError = prepareStackForUser(err);
92
+
93
+ next(clonedError);
94
+ };
95
+
91
96
  const jsonErrorRenderer = (err, req, res, next) => { // eslint-disable-line no-unused-vars
92
97
  res.json({
93
98
  errors: [{
@@ -157,7 +162,11 @@ const prepareUserMessage = (err, req) => {
157
162
  userError.context = err.message;
158
163
  }
159
164
 
160
- userError.message = tpl(messages.userMessages[err.name], {action: action});
165
+ if (_.get(messages.userMessages, err.name)) {
166
+ userError.message = tpl(messages.userMessages[err.name], {action: action});
167
+ } else {
168
+ userError.message = tpl(messages.UnknownError, {action, name: err.name});
169
+ }
161
170
  }
162
171
  }
163
172
 
@@ -173,27 +182,33 @@ module.exports.pageNotFound = (req, res, next) => {
173
182
  };
174
183
 
175
184
  module.exports.handleJSONResponse = sentry => [
176
- // Handle the error in Sentry
177
- sentry.errorHandler,
178
185
  // Make sure the error can be served
179
186
  module.exports.prepareError,
187
+ // Handle the error in Sentry
188
+ sentry.errorHandler,
189
+ // Format the stack for the user
190
+ module.exports.prepareStack,
180
191
  // Render the error using JSON format
181
192
  jsonErrorRenderer
182
193
  ];
183
194
 
184
195
  module.exports.handleJSONResponseV2 = sentry => [
185
- // Handle the error in Sentry
186
- sentry.errorHandler,
187
196
  // Make sure the error can be served
188
197
  module.exports.prepareError,
198
+ // Handle the error in Sentry
199
+ sentry.errorHandler,
200
+ // Format the stack for the user
201
+ module.exports.prepareStack,
189
202
  // Render the error using JSON format
190
203
  jsonErrorRendererV2
191
204
  ];
192
205
 
193
206
  module.exports.handleHTMLResponse = sentry => [
207
+ // Make sure the error can be served
208
+ module.exports.prepareError,
194
209
  // Handle the error in Sentry
195
210
  sentry.errorHandler,
196
- // Make sure the error can be served
197
- module.exports.prepareError
211
+ // Format the stack for the user
212
+ module.exports.prepareStack
198
213
  ];
199
214
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mw-error-handler",
3
- "version": "0.1.4",
3
+ "version": "0.1.7",
4
4
  "repository": "https://github.com/TryGhost/Utils/tree/main/packages/mw-error-handler",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -26,9 +26,9 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@tryghost/debug": "^0.1.9",
29
- "@tryghost/errors": "1.2.5",
29
+ "@tryghost/errors": "^1.2.5",
30
30
  "@tryghost/tpl": "^0.1.8",
31
- "lodash": "4.17.21"
31
+ "lodash": "^4.17.21"
32
32
  },
33
- "gitHead": "3bf8b2007541db078debd4d6b4b9f16e66f08c60"
33
+ "gitHead": "b44db9dbd6382d426125bd9627cb193a8360a2c2"
34
34
  }