Skip to content

Commit

Permalink
fix(media): throw not found rather than timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore03109 committed Mar 4, 2024
1 parent 6a1ac28 commit aa0f9c7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
1 change: 0 additions & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services:
- .env
volumes:
- ./:/opt/isomercms-backend
- /opt/isomercms-backend/node_modules
- ${EFS_VOL_PATH}:${EFS_VOL_PATH}
- "~/.gitconfig:/etc/gitconfig"

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/errors/RouteNotFoundError.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
}
13 changes: 13 additions & 0 deletions src/middleware/catchNonExistentRouteHandler.ts
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions src/routes/v2/authenticatedSites/__tests__/Media.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
})
})
4 changes: 4 additions & 0 deletions src/routes/v2/authenticatedSites/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -445,6 +447,8 @@ export class MediaRouter {
attachRollbackRouteHandlerWrapper(this.deleteMediaFile)
)

router.use(catchNonExistentRoutesMiddleware)

return router
}
}

0 comments on commit aa0f9c7

Please sign in to comment.