All files / server/router error.js

94.11% Statements 16/17
83.33% Branches 5/6
100% Functions 3/3
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 3027x 27x 27x   27x   27x   102x     102x   102x 102x     19x 19x     19x   19x 19x 19x   102x    
const join = require('../src/join');
const parse = require('./parse');
const { match } = require('path-to-regexp');
 
const decode = decodeURIComponent;
 
module.exports = (...all) => {
  // Extracted or otherwise it'd shift once per call; also more performant
  const { path, middle } = parse(all);
 
  // Convert to the proper path, since the new ones use `(.*)` instead of `*`
  const parsePath = match(path.replace(/\*/g, '(.*)'), { decode: decode });
 
  const generic = () => {};
  generic.error = async ctx => {
 
    // Only do this if the correct path
    ctx.error.code = ctx.error.code || '';
    ctx.error.params = parsePath(ctx.error.code).params;
 
    // Add an extra-allowing initial matching
    Iif (!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;
};