@tryghost/mw-error-handler 0.1.8 → 0.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/mw-error-handler.js +48 -1
- package/package.json +5 -4
package/lib/mw-error-handler.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const _ = require('lodash');
|
|
2
|
+
const semver = require('semver');
|
|
2
3
|
const debug = require('@tryghost/debug')('error-handler');
|
|
3
4
|
const errors = require('@tryghost/errors');
|
|
4
5
|
const {prepareStackForUser} = require('@tryghost/errors').utils;
|
|
@@ -7,6 +8,16 @@ const tpl = require('@tryghost/tpl');
|
|
|
7
8
|
const messages = {
|
|
8
9
|
pageNotFound: 'Page not found',
|
|
9
10
|
resourceNotFound: 'Resource not found',
|
|
11
|
+
methodNotAcceptableVersionBehind: {
|
|
12
|
+
message: 'Request not acceptable for provided Accept-Version header.',
|
|
13
|
+
context: 'Provided client version {acceptVersion} is ahead of current Ghost instance version {ghostVersion}.',
|
|
14
|
+
help: 'Upgrade your Ghost instance.'
|
|
15
|
+
},
|
|
16
|
+
methodNotAcceptableVersionAhead: {
|
|
17
|
+
message: 'Request not acceptable for provided Accept-Version header.',
|
|
18
|
+
context: 'Provided client version {acceptVersion} is outdated and is behind current Ghost version {ghostVersion}.',
|
|
19
|
+
help: 'Upgrade your Ghost API client.'
|
|
20
|
+
},
|
|
10
21
|
actions: {
|
|
11
22
|
images: {
|
|
12
23
|
upload: 'upload image'
|
|
@@ -174,7 +185,43 @@ const prepareUserMessage = (err, req) => {
|
|
|
174
185
|
};
|
|
175
186
|
|
|
176
187
|
module.exports.resourceNotFound = (req, res, next) => {
|
|
177
|
-
|
|
188
|
+
if (req && req.headers && req.headers['accept-version']
|
|
189
|
+
&& res.locals && res.locals.safeVersion
|
|
190
|
+
&& semver.compare(semver.coerce(req.headers['accept-version']), semver.coerce(res.locals.safeVersion)) !== 0) {
|
|
191
|
+
const versionComparison = semver.compare(
|
|
192
|
+
semver.coerce(req.headers['accept-version']),
|
|
193
|
+
semver.coerce(res.locals.safeVersion)
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
let errorOptions;
|
|
197
|
+
if (versionComparison === 1) {
|
|
198
|
+
errorOptions = {
|
|
199
|
+
message: tpl(
|
|
200
|
+
messages.methodNotAcceptableVersionBehind.message
|
|
201
|
+
),
|
|
202
|
+
context: tpl(messages.methodNotAcceptableVersionBehind.context, {
|
|
203
|
+
acceptVersion: req.headers['accept-version'],
|
|
204
|
+
ghostVersion: `v${res.locals.safeVersion}`
|
|
205
|
+
}),
|
|
206
|
+
help: tpl(messages.methodNotAcceptableVersionBehind.help)
|
|
207
|
+
};
|
|
208
|
+
} else {
|
|
209
|
+
errorOptions = {
|
|
210
|
+
message: tpl(
|
|
211
|
+
messages.methodNotAcceptableVersionAhead.message
|
|
212
|
+
),
|
|
213
|
+
context: tpl(messages.methodNotAcceptableVersionAhead.context, {
|
|
214
|
+
acceptVersion: req.headers['accept-version'],
|
|
215
|
+
ghostVersion: `v${res.locals.safeVersion}`
|
|
216
|
+
}),
|
|
217
|
+
help: tpl(messages.methodNotAcceptableVersionAhead.help)
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
next(new errors.RequestNotAcceptableError(errorOptions));
|
|
222
|
+
} else {
|
|
223
|
+
next(new errors.NotFoundError({message: tpl(messages.resourceNotFound)}));
|
|
224
|
+
}
|
|
178
225
|
};
|
|
179
226
|
|
|
180
227
|
module.exports.pageNotFound = (req, res, next) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mw-error-handler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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,10 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@tryghost/debug": "^0.1.9",
|
|
29
|
-
"@tryghost/errors": "1.2.
|
|
29
|
+
"@tryghost/errors": "1.2.10",
|
|
30
30
|
"@tryghost/tpl": "^0.1.8",
|
|
31
|
-
"lodash": "^4.17.21"
|
|
31
|
+
"lodash": "^4.17.21",
|
|
32
|
+
"semver": "^7.3.6"
|
|
32
33
|
},
|
|
33
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "c2b9000721dc2113cf3338db58a139da3bb37fb0"
|
|
34
35
|
}
|