@tryghost/mw-error-handler 0.2.4 → 0.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.
- package/lib/mw-error-handler.js +41 -7
- package/package.json +3 -2
package/lib/mw-error-handler.js
CHANGED
|
@@ -3,6 +3,7 @@ const semver = require('semver');
|
|
|
3
3
|
const debug = require('@tryghost/debug')('error-handler');
|
|
4
4
|
const errors = require('@tryghost/errors');
|
|
5
5
|
const {prepareStackForUser} = require('@tryghost/errors').utils;
|
|
6
|
+
const {isReqResUserSpecific, cacheControlValues} = require('@tryghost/http-cache-utils');
|
|
6
7
|
const tpl = require('@tryghost/tpl');
|
|
7
8
|
|
|
8
9
|
const messages = {
|
|
@@ -62,8 +63,8 @@ module.exports.prepareError = (err, req, res, next) => {
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
if (!errors.utils.isGhostError(err)) {
|
|
65
|
-
// We need a special case for 404 errors
|
|
66
|
-
if (err.statusCode && err.statusCode === 404) {
|
|
66
|
+
// We need a special case for 404 errors & bookshelf empty errors
|
|
67
|
+
if ((err.statusCode && err.statusCode === 404) || err.message === 'EmptyResponse') {
|
|
67
68
|
err = new errors.NotFoundError({
|
|
68
69
|
err: err
|
|
69
70
|
});
|
|
@@ -90,11 +91,6 @@ module.exports.prepareError = (err, req, res, next) => {
|
|
|
90
91
|
// alternative for res.status();
|
|
91
92
|
res.statusCode = err.statusCode;
|
|
92
93
|
|
|
93
|
-
// never cache errors
|
|
94
|
-
res.set({
|
|
95
|
-
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
|
96
|
-
});
|
|
97
|
-
|
|
98
94
|
next(err);
|
|
99
95
|
};
|
|
100
96
|
|
|
@@ -104,6 +100,13 @@ module.exports.prepareStack = (err, req, res, next) => { // eslint-disable-line
|
|
|
104
100
|
next(clonedError);
|
|
105
101
|
};
|
|
106
102
|
|
|
103
|
+
/**
|
|
104
|
+
* @private the method is exposed for testing purposes only
|
|
105
|
+
* @param {Object} err
|
|
106
|
+
* @param {import('express').Request} req
|
|
107
|
+
* @param {import('express').Response} res
|
|
108
|
+
* @param {import('express').NextFunction} next
|
|
109
|
+
*/
|
|
107
110
|
const jsonErrorRenderer = (err, req, res, next) => { // eslint-disable-line no-unused-vars
|
|
108
111
|
res.json({
|
|
109
112
|
errors: [{
|
|
@@ -135,6 +138,31 @@ const jsonErrorRendererV2 = (err, req, res, next) => { // eslint-disable-line no
|
|
|
135
138
|
});
|
|
136
139
|
};
|
|
137
140
|
|
|
141
|
+
/**
|
|
142
|
+
*
|
|
143
|
+
* @param {String} [cacheControlHeaderValue] cache-control header value
|
|
144
|
+
*/
|
|
145
|
+
module.exports.prepareErrorCacheControl = (cacheControlHeaderValue) => {
|
|
146
|
+
return (err, req, res, next) => {
|
|
147
|
+
let cacheControl = cacheControlHeaderValue;
|
|
148
|
+
if (!cacheControlHeaderValue) {
|
|
149
|
+
// never cache errors unless it's a 404
|
|
150
|
+
cacheControl = cacheControlValues.private;
|
|
151
|
+
|
|
152
|
+
// Do not include 'private' cache-control directive for 404 responses
|
|
153
|
+
if (err.statusCode === 404 && req.method === 'GET' && !isReqResUserSpecific(req, res)) {
|
|
154
|
+
cacheControl = cacheControlValues.noCacheDynamic;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
res.set({
|
|
159
|
+
'Cache-Control': cacheControl
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
next(err);
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
|
|
138
166
|
const prepareUserMessage = (err, req) => {
|
|
139
167
|
const userError = {
|
|
140
168
|
message: err.message,
|
|
@@ -238,6 +266,8 @@ module.exports.handleJSONResponse = sentry => [
|
|
|
238
266
|
// Make sure the error can be served
|
|
239
267
|
module.exports.prepareError,
|
|
240
268
|
// Handle the error in Sentry
|
|
269
|
+
// Add cache-control header
|
|
270
|
+
module.exports.prepareErrorCacheControl(),
|
|
241
271
|
sentry.errorHandler,
|
|
242
272
|
// Format the stack for the user
|
|
243
273
|
module.exports.prepareStack,
|
|
@@ -252,6 +282,8 @@ module.exports.handleJSONResponse = sentry => [
|
|
|
252
282
|
module.exports.handleJSONResponseV2 = sentry => [
|
|
253
283
|
// Make sure the error can be served
|
|
254
284
|
module.exports.prepareError,
|
|
285
|
+
// Add cache-control header
|
|
286
|
+
module.exports.prepareErrorCacheControl(),
|
|
255
287
|
// Handle the error in Sentry
|
|
256
288
|
sentry.errorHandler,
|
|
257
289
|
// Format the stack for the user
|
|
@@ -263,6 +295,8 @@ module.exports.handleJSONResponseV2 = sentry => [
|
|
|
263
295
|
module.exports.handleHTMLResponse = sentry => [
|
|
264
296
|
// Make sure the error can be served
|
|
265
297
|
module.exports.prepareError,
|
|
298
|
+
// Add cache-control header
|
|
299
|
+
module.exports.prepareErrorCacheControl(cacheControlValues.private),
|
|
266
300
|
// Handle the error in Sentry
|
|
267
301
|
sentry.errorHandler,
|
|
268
302
|
// Format the stack for the user
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mw-error-handler",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"repository": "https://github.com/TryGhost/Utils/tree/main/packages/mw-error-handler",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,9 +27,10 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@tryghost/debug": "^0.1.9",
|
|
29
29
|
"@tryghost/errors": "1.2.12",
|
|
30
|
+
"@tryghost/http-cache-utils": "0.1.3",
|
|
30
31
|
"@tryghost/tpl": "^0.1.8",
|
|
31
32
|
"lodash": "^4.17.21",
|
|
32
33
|
"semver": "^7.3.6"
|
|
33
34
|
},
|
|
34
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "ebfd454e760c61ef69cf69d9eb9ac5889aacded2"
|
|
35
36
|
}
|