Files
directus/api/src/controllers/not-found.ts
Nicola Krumschmidt d64ca14348 Explicitly set catch parameters to any type (#7654)
This fixes not being able to build the repo due to type issues
introduced by the Typescript 4.4 option "useUnknownInCatchVariables",
which is enabled by default in strict mode.
2021-08-27 10:33:30 -04:00

29 lines
767 B
TypeScript

import { RequestHandler } from 'express';
import emitter from '../emitter';
import { RouteNotFoundException } from '../exceptions';
/**
* Handles not found routes.
*
* - If a hook throws an error, the error gets forwarded to the error handler.
* - If a hook returns true, the handler assumes the response has been
* processed and won't generate a response.
*
* @param req
* @param res
* @param next
*/
const notFound: RequestHandler = async (req, res, next) => {
try {
const hooksResult = await emitter.emitAsync('request.not_found', req, res);
if (hooksResult.reduce((prev, current) => current || prev, false)) {
return next();
}
next(new RouteNotFoundException(req.path));
} catch (err: any) {
next(err);
}
};
export default notFound;