diff --git a/src/errors/RouteNotFoundError.ts b/src/errors/RouteNotFoundError.ts new file mode 100644 index 000000000..fbeeee926 --- /dev/null +++ b/src/errors/RouteNotFoundError.ts @@ -0,0 +1,11 @@ +import { BaseIsomerError } from "./BaseError" + +export default class RouteNotFoundError extends BaseIsomerError { + constructor(meta = {}) { + super({ + status: 404, + code: "Route Not Found Error", + meta, + }) + } +} diff --git a/src/middleware/catchNonExistentRouteHandler.ts b/src/middleware/catchNonExistentRouteHandler.ts new file mode 100644 index 000000000..087427718 --- /dev/null +++ b/src/middleware/catchNonExistentRouteHandler.ts @@ -0,0 +1,13 @@ +import { NextFunction, Request, Response } from "express" + +import RouteNotFoundError from "@root/errors/RouteNotFoundError" + +export const catchNonExistentRoutesMiddleware = ( + req: Request, + res: Response, + next: NextFunction +) => { + next(new RouteNotFoundError()) +} + +export default catchNonExistentRoutesMiddleware diff --git a/src/routes/v2/authenticatedSites/__tests__/Media.spec.ts b/src/routes/v2/authenticatedSites/__tests__/Media.spec.ts index d891b7569..9735d12e6 100644 --- a/src/routes/v2/authenticatedSites/__tests__/Media.spec.ts +++ b/src/routes/v2/authenticatedSites/__tests__/Media.spec.ts @@ -7,6 +7,7 @@ import { mockGithubSessionData, } from "@root/fixtures/sessionData" import { MOCK_REPO_NAME_ONE } from "@root/fixtures/sites" +import catchNonExistentRoutesMiddleware from "@root/middleware/catchNonExistentRouteHandler" import { attachReadRouteHandlerWrapper } from "@root/middleware/routeHandler" import { MediaDirectoryService } from "@root/services/directoryServices/MediaDirectoryService" import { MediaFileService } from "@root/services/fileServices/MdPageServices/MediaFileService" @@ -80,6 +81,8 @@ describe("Media Router", () => { attachReadRouteHandlerWrapper(router.deleteMediaFile) ) + subrouter.use(catchNonExistentRoutesMiddleware) + const app = generateRouterForDefaultUserWithSite(subrouter) const siteName = MOCK_REPO_NAME_ONE const directoryName = "directoryName" @@ -438,4 +441,10 @@ describe("Media Router", () => { ) }) }) + + describe("catchNonExistentRoutes", () => { + it("returns 404 for non-existent routes", async () => { + await request(app).get(`/${siteName}/media/imagesblah/pages`).expect(404) + }) + }) }) diff --git a/src/routes/v2/authenticatedSites/media.ts b/src/routes/v2/authenticatedSites/media.ts index 3d09d9e87..5f20c61e1 100644 --- a/src/routes/v2/authenticatedSites/media.ts +++ b/src/routes/v2/authenticatedSites/media.ts @@ -11,6 +11,7 @@ import { import GithubSessionData from "@classes/GithubSessionData" import UserWithSiteSessionData from "@classes/UserWithSiteSessionData" +import { catchNonExistentRoutesMiddleware } from "@root/middleware/catchNonExistentRouteHandler" import type { MediaDirOutput, MediaFileOutput, @@ -445,6 +446,8 @@ export class MediaRouter { attachRollbackRouteHandlerWrapper(this.deleteMediaFile) ) + router.use(catchNonExistentRoutesMiddleware) + return router } }