From aa0f9c732d9825b89503b357e5b8bb77e45c2a54 Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Mon, 4 Mar 2024 09:47:57 +0800 Subject: [PATCH] fix(media): throw not found rather than timeout --- docker-compose.dev.yml | 1 - package.json | 2 +- src/errors/RouteNotFoundError.ts | 11 +++++++++++ src/middleware/catchNonExistentRouteHandler.ts | 13 +++++++++++++ .../v2/authenticatedSites/__tests__/Media.spec.ts | 9 +++++++++ src/routes/v2/authenticatedSites/media.ts | 4 ++++ 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/errors/RouteNotFoundError.ts create mode 100644 src/middleware/catchNonExistentRouteHandler.ts diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index ca47cfc96..f7f2e0bb3 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -12,7 +12,6 @@ services: - .env volumes: - ./:/opt/isomercms-backend - - /opt/isomercms-backend/node_modules - ${EFS_VOL_PATH}:${EFS_VOL_PATH} - "~/.gitconfig:/etc/gitconfig" diff --git a/package.json b/package.json index 202c3f047..ed8d4e47f 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "node --unhandled-rejections=warn -r ts-node/register/transpile-only -r tsconfig-paths/register -r dotenv/config build/server.js dotenv_config_path=/efs/isomer/.isomer.env", "start:ecs:prod": "node --unhandled-rejections=warn -r ts-node/register/transpile-only -r tsconfig-paths/register src/server.js", "start:ecs:staging": "node --unhandled-rejections=warn -r ts-node/register/transpile-only -r tsconfig-paths/register src/server.js", - "start::ecs:dev": "ts-node-dev --respawn src/server.js", + "start:ecs:dev": "ts-node-dev --respawn src/server.js", "dev": "source .env && docker compose -f docker-compose.dev.yml up", "test:docker": "docker run -d -p 54321:5432 --name postgres -e POSTGRES_USER=isomer -e POSTGRES_PASSWORD=password -e POSTGRES_DB=isomercms_test postgres:latest", "test": "source .env.test && jest --runInBand", 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..1f51cd500 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, @@ -83,6 +84,7 @@ export class MediaRouter { { page: number; limit: number; search: string }, { userWithSiteSessionData: UserWithSiteSessionData } > = async (req, res) => { + console.log("in listMediaDirectoryFiles") const { userWithSiteSessionData } = res.locals const { directoryName } = req.params @@ -445,6 +447,8 @@ export class MediaRouter { attachRollbackRouteHandlerWrapper(this.deleteMediaFile) ) + router.use(catchNonExistentRoutesMiddleware) + return router } }