Skip to content

Commit

Permalink
fix: move 404 router handler before last error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Apr 25, 2023
1 parent 6a16be1 commit d481fa0
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
});
Expand Down

0 comments on commit d481fa0

Please sign in to comment.