@tryghost/mw-error-handler 0.1.8 → 0.2.2

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.
@@ -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
+ methodNotAcceptableVersionAhead: {
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
+ methodNotAcceptableVersionBehind: {
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,45 @@ const prepareUserMessage = (err, req) => {
174
185
  };
175
186
 
176
187
  module.exports.resourceNotFound = (req, res, next) => {
177
- next(new errors.NotFoundError({message: tpl(messages.resourceNotFound)}));
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.methodNotAcceptableVersionAhead.message
201
+ ),
202
+ context: tpl(messages.methodNotAcceptableVersionAhead.context, {
203
+ acceptVersion: req.headers['accept-version'],
204
+ ghostVersion: `v${res.locals.safeVersion}`
205
+ }),
206
+ help: tpl(messages.methodNotAcceptableVersionAhead.help),
207
+ code: 'UPDATE_GHOST'
208
+ };
209
+ } else {
210
+ errorOptions = {
211
+ message: tpl(
212
+ messages.methodNotAcceptableVersionBehind.message
213
+ ),
214
+ context: tpl(messages.methodNotAcceptableVersionBehind.context, {
215
+ acceptVersion: req.headers['accept-version'],
216
+ ghostVersion: `v${res.locals.safeVersion}`
217
+ }),
218
+ help: tpl(messages.methodNotAcceptableVersionBehind.help),
219
+ code: 'UPDATE_CLIENT'
220
+ };
221
+ }
222
+
223
+ next(new errors.RequestNotAcceptableError(errorOptions));
224
+ } else {
225
+ next(new errors.NotFoundError({message: tpl(messages.resourceNotFound)}));
226
+ }
178
227
  };
179
228
 
180
229
  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.1.8",
3
+ "version": "0.2.2",
4
4
  "repository": "https://github.com/TryGhost/Utils/tree/main/packages/mw-error-handler",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -19,16 +19,17 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "c8": "7.11.0",
22
+ "c8": "7.11.2",
23
23
  "mocha": "9.1.3",
24
24
  "should": "13.2.3",
25
25
  "sinon": "12.0.1"
26
26
  },
27
27
  "dependencies": {
28
28
  "@tryghost/debug": "^0.1.9",
29
- "@tryghost/errors": "1.2.8",
29
+ "@tryghost/errors": "1.2.12",
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": "d49b83751cb42ecbf40c60d3ee9ade27732eeee2"
34
+ "gitHead": "9a90e404200df76c6e4e3056f89d73eb69c9013e"
34
35
  }