Skip to content

Commit

Permalink
return json for all 404s
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Dec 12, 2024
1 parent dd25f57 commit 5894fb9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/controllers/tileController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {NextFunction, Request, Response} from "express";

Check failure on line 1 in src/controllers/tileController.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `NextFunction,·Request,·Response` with `·NextFunction,·Request,·Response·`
import {AppLocals} from "../types/app";

Check failure on line 2 in src/controllers/tileController.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `AppLocals` with `·AppLocals·`
import asyncControllerHandler from "../errors/asyncControllerHandler";
import {GroutError} from "../errors/groutError";
import {ErrorType} from "../errors/errorType";
import notFound from "../errors/notFound";

export class TileController {
static getTile = async (req: Request, res: Response, next: NextFunction) => {

Check failure on line 7 in src/controllers/tileController.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `req:·Request,·res:·Response,·next:·NextFunction` with `⏎········req:·Request,⏎········res:·Response,⏎········next:·NextFunction⏎····`
Expand All @@ -11,8 +10,6 @@ export class TileController {
const {tileDatasets} = req.app.locals as AppLocals;

Check failure on line 10 in src/controllers/tileController.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `tileDatasets` with `·tileDatasets·`

// TODO: check coords params and throw 400 GroutError if not ints
// TODO: also throw error if requested dataset or level not found
// TODO: also return JSON response if route is not matched (always return JSON response in all cases!)
let tileData = null;
if (tileDatasets[dataset] && tileDatasets[dataset][level]) {
const db = tileDatasets[dataset][level];
Expand All @@ -29,7 +26,7 @@ export class TileController {
"Content-Encoding": "gzip"
}).end(tileData);
} else {
res.writeHead(404).end();
throw notFound(req);
}
});
};
Expand Down
8 changes: 8 additions & 0 deletions src/errors/notFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Request } from "express";
import {ErrorType} from "./errorType";
import {GroutError} from "./groutError";

export default (req: Request) => {
const { url } = req;
throw new GroutError(`Route not found: ${url}`, 404, ErrorType.NOT_FOUND);
};
5 changes: 5 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Router } from "express";
import { IndexController } from "./controllers/indexController";
import { TileController } from "./controllers/tileController";
import notFound from "./errors/notFound";

export const registerRoutes = () => {
const router = Router();
router.get("/", IndexController.getIndex);
router.get("/tile/:dataset/:level/:z/:x/:y", TileController.getTile);

// Throw 404 error for any unmatched routes
router.use(notFound);

Check failure on line 12 in src/routes.ts

View workflow job for this annotation

GitHub Actions / test-and-push

tests/unit/routes.spec.ts > registerRoutes > registers expected routes

TypeError: router.use is not a function ❯ Module.registerRoutes src/routes.ts:12:12 ❯ tests/unit/routes.spec.ts:21:24

return router;
};

0 comments on commit 5894fb9

Please sign in to comment.