All files / backend/koa-middleware notFound.js

100% Statements 5/5
100% Branches 2/2
100% Functions 1/1
100% Lines 5/5
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x                                                                   2x 1x         1x       1x  
const HttpError = require('./HttpError');
 
/**
 * @swagger
 * definition:
 *   NoFoundError:
 *     description:
 *       HTTP status code 404.
 *       A requested resource is not found. Might be cause by missing API or missing DB record.
 *
 *     properties:
 *       errorId:
 *         type: string
 *         format: uuid
 *
 *       errorCode:
 *         type: string
 *         example: ROUTE_NOT_FOUND
 *
 *       errorMessage:
 *         type: string
 *         example: Route GET /api/v1/users was not found
 */
 
/**
 * This middleware should be use as last in the koa app.
 * If no other middlewares and APIs responded with any content then
 * this middleware will triggers a 404/Not found error.
 *
 * @category middleware
 *
 * @param {Object} ctx Koa context
 */
function notFound(ctx) {
  if (ctx.status === 404) {
    const error = new HttpError(
        404,
        'ROUTE_NOT_FOUND',
        `Route ${ctx.request.method} ${ctx.request.url} was not found`
    );
    ctx.app.emit('error', error, ctx);
  }
}
 
module.exports = notFound;