amphora 8.3.0 → 8.4.1-dev.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/render.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var uriRoutes, renderers,
3
+ var uriRoutes, renderers, errorHandler,
4
4
  db = require('./services/db'),
5
5
  log = require('./services/logger').setup({
6
6
  file: __filename,
@@ -264,6 +264,16 @@ function logTime(hrStart, uri) {
264
264
  };
265
265
  }
266
266
 
267
+ /**
268
+ * Passes page error handling back to the site.
269
+ *
270
+ * @param {Function} handler - (error, response, next) => {}
271
+ */
272
+ function registerErrorHandler(handler) {
273
+ errorHandler = handler;
274
+ }
275
+
276
+
267
277
  /**
268
278
  * Run composer by translating url to a "page" by base64ing it. Errors are handled by Express.
269
279
  *
@@ -276,17 +286,25 @@ function renderExpressRoute(req, res, next) {
276
286
  var hrStart = process.hrtime(),
277
287
  site = res.locals.site,
278
288
  prefix = `${getExpressRoutePrefix(site)}/_uris/`,
279
- pageReference = `${prefix}${buf.encode(req.hostname + req.baseUrl + req.path)}`;
289
+ // We're using decodeURIComponent to match any encoded value that might be sent by the browser.
290
+ // An example of this is a smart quote ’ -- That value is going to be encoded to `%E2%80%99`.
291
+ // We store the actual symbol in our database, not the encoded value, so, when decoding it, we ensure we can get the direct symbol
292
+ // and, in consequence, the page we're looking for.
293
+ pageReference = `${prefix}${buf.encode(req.hostname + req.baseUrl + decodeURIComponent(req.path))}`;
280
294
 
281
295
  return module.exports.renderUri(pageReference, req, res, hrStart)
282
296
  .catch((error) => {
283
- if (error.name === 'NotFoundError') {
284
- log('info', `could not find resource ${req.uri}`, {
285
- message: error.message
286
- });
287
- next();
297
+ if (errorHandler) {
298
+ errorHandler(error, res, next);
288
299
  } else {
289
- next(error);
300
+ if (error.name === 'NotFoundError') {
301
+ log('info', `could not find resource ${req.uri}`, {
302
+ message: error.message
303
+ });
304
+ next();
305
+ } else {
306
+ next(error);
307
+ }
290
308
  }
291
309
  });
292
310
  }
@@ -373,6 +391,7 @@ module.exports.findRenderer = findRenderer;
373
391
  // Setup
374
392
  module.exports.renderers = {}; // Overriden when a user registers a render object
375
393
  module.exports.registerRenderers = registerRenderers;
394
+ module.exports.registerErrorHandler = registerErrorHandler;
376
395
 
377
396
  // For Testing
378
397
  module.exports.resetUriRouteHandlers = resetUriRouteHandlers;
package/lib/setup.js CHANGED
@@ -35,6 +35,7 @@ module.exports = function (options = {}) {
35
35
  providers = [],
36
36
  sessionStore,
37
37
  renderers,
38
+ errorHandler,
38
39
  plugins = [],
39
40
  bootstrap = true,
40
41
  storage = false,
@@ -58,6 +59,10 @@ module.exports = function (options = {}) {
58
59
  render.registerRenderers(renderers);
59
60
  }
60
61
 
62
+ if (errorHandler) {
63
+ render.registerErrorHandler(errorHandler);
64
+ }
65
+
61
66
  // Make sure the storage module is run, then bootstrap
62
67
  // all the default data and finally return the router
63
68
  return storage.setup()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amphora",
3
- "version": "8.3.0",
3
+ "version": "8.4.1-dev.0",
4
4
  "description": "An API mixin for Express that saves, publishes and composes data with the key-value store of your choice.",
5
5
  "main": "index.js",
6
6
  "scripts": {