fix: standardize API responses for unknown routes
- Add a centralized not-found handler returning a consistent 404 response - Assert the expected error payload for unknown endpoints
This commit is contained in:
parent
ca5ca12dba
commit
75bb74f5b3
2 changed files with 10 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ import assert from "node:assert";
|
||||||
import { after, describe, it } from "node:test";
|
import { after, describe, it } from "node:test";
|
||||||
|
|
||||||
import { buildApp } from "./server.js";
|
import { buildApp } from "./server.js";
|
||||||
|
import { setServers } from "node:dns";
|
||||||
|
|
||||||
describe("API endpoints", () => {
|
describe("API endpoints", () => {
|
||||||
const app = buildApp();
|
const app = buildApp();
|
||||||
|
|
@ -119,6 +120,9 @@ describe("API endpoints", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(response.statusCode, 404);
|
assert.equal(response.statusCode, 404);
|
||||||
|
assert.deepEqual(response.json(), {
|
||||||
|
error: "Not Found",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns a generic 500 response for unexpected error", async () => {
|
it("returns a generic 500 response for unexpected error", async () => {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,12 @@ export const buildApp = () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.setNotFoundHandler((_request, reply) => {
|
||||||
|
return reply.code(404).send({
|
||||||
|
error: "Not Found",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
app.get("/health", async () => ({
|
app.get("/health", async () => ({
|
||||||
status: "ok",
|
status: "ok",
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue