diff --git a/src/lib/app.ts b/src/lib/app.ts index 46daa21a026..9fb28fc5092 100644 --- a/src/lib/app.ts +++ b/src/lib/app.ts @@ -28,7 +28,7 @@ import { Knex } from 'knex'; import maintenanceMiddleware from './middleware/maintenance-middleware'; import { unless } from './middleware/unless-middleware'; import { catchAllErrorHandler } from './middleware/catch-all-error-handler'; -import { UnleashError } from './error/api-error'; +import NotFoundError from './error/notfound-error'; export default async function getApp( config: IUnleashConfig, @@ -174,6 +174,13 @@ export default async function getApp( services.openApiService.useErrorHandler(app); } + // handle all API 404s + app.use(`${baseUriPath}/api`, (req) => { + throw new NotFoundError( + `The path you were looking for (${baseUriPath}/api${req.path}) is not available.`, + ); + }); + if (process.env.NODE_ENV !== 'production') { app.use(errorHandler()); } else { @@ -184,16 +191,6 @@ export default async function getApp( res.send(indexHTML); }); - // handle all API 404s - app.use(`${baseUriPath}/api`, (req, res) => { - const error = new UnleashError({ - name: 'NotFoundError', - message: `The path you were looking for (${baseUriPath}/api${req.path}) is not available.`, - }); - res.status(error.statusCode).send(error); - return; - }); - app.get(`${baseUriPath}/*`, (req, res) => { res.send(indexHTML); });