All files / router error.js

20% Statements 3/15
0% Branches 0/8
0% Functions 0/3
21.43% Lines 3/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 251x 1x   1x                                          
const parse = require('./parse');
const params = require('./path-to-regexp-wrap')();
 
module.exports = (...all) => {
  // Extracted or otherwise it'd shift once per call; also more performant
  const { path, middle } = parse(all);
  const match = params(path || '');
 
  const generic = () => {};
  generic.error = async ctx => {
 
    // Only do this if the correct path
    ctx.error.code = ctx.error.code || '';
    ctx.error.params = match(ctx.error.code);
 
    // Add an extra-allowing initial matching
    if (!ctx.error.params && ctx.error.code.slice(0, path.length) !== path) return;
 
    const ret = await middle[0](ctx);
    delete ctx.error;
    return ret;
  };
  return generic;
};