Skip to content

Commit

Permalink
catch errors and forward to error middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
1cedsoda committed Oct 18, 2023
1 parent 361428b commit 84dcb15
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/node-express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pastapi-node-express",
"version": "0.3.0",
"version": "0.4.0",
"main": "dist/index",
"types": "dist/index",
"files": [
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/node-express/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Operation } from "pastapi-core";
import { operationNamespaces } from "./namespace";
import { router } from "./router";
import { handlerType } from "./handers";
import { handlerType } from "./handlers";
import { boilerplate } from "./boilerplate";
import { format } from "@prettier/sync";
import { Options } from "prettier";
Expand Down
2 changes: 1 addition & 1 deletion packages/node-express/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const createRouter = (handler: Handler | undefined): Router => {
else res.status(500).send(e);
return next();
}
if (handler !== undefined) handler(req, res, parsed);
if (handler !== undefined) handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
Expand Down
10 changes: 9 additions & 1 deletion packages/node-express/test/api1/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ paths:
in: query
schema:
type: string

/error:
get:
responses:
"500":
description: "Internal Server Error"
content:
text/plain:
schema:
type: string
components:
schemas:
User:
Expand Down
16 changes: 16 additions & 0 deletions packages/node-express/test/api1/gen/api-operations.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,21 @@
}
}
]
},
{
"path": "/error",
"method": "get",
"operationId": "getError",
"responses": [
{
"statusCode": "500",
"applicationType": "text/plain",
"bodySchema": {
"type": "string"
}
}
],
"requestBodies": [],
"requestParameters": []
}
]
68 changes: 62 additions & 6 deletions packages/node-express/test/api1/gen/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export namespace GetUser {
else res.status(500).send(e);
return next();
}
if (handler !== undefined) handler(req, res, parsed);
if (handler !== undefined)
handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
Expand Down Expand Up @@ -112,7 +113,8 @@ export namespace PostUser {
else res.status(500).send(e);
return next();
}
if (handler !== undefined) handler(req, res, parsed);
if (handler !== undefined)
handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
Expand Down Expand Up @@ -167,7 +169,8 @@ export namespace GetUserId {
else res.status(500).send(e);
return next();
}
if (handler !== undefined) handler(req, res, parsed);
if (handler !== undefined)
handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
Expand Down Expand Up @@ -234,7 +237,8 @@ export namespace GetCookie {
else res.status(500).send(e);
return next();
}
if (handler !== undefined) handler(req, res, parsed);
if (handler !== undefined)
handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
Expand Down Expand Up @@ -301,7 +305,8 @@ export namespace GetHeader {
else res.status(500).send(e);
return next();
}
if (handler !== undefined) handler(req, res, parsed);
if (handler !== undefined)
handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
Expand Down Expand Up @@ -362,7 +367,55 @@ export namespace GetQuery {
else res.status(500).send(e);
return next();
}
if (handler !== undefined) handler(req, res, parsed);
if (handler !== undefined)
handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
};
}

export namespace GetError {
export const bodySchemas = {};
export type ParsedBody = {};
export const paramSchemas = {};
export type ParamsParsed = {};
export type Parsed = {
contentType: undefined;
body: ParsedBody;
params: ParamsParsed;
};
export type Handler = (
req: Request,
res: Response,
parsed: Parsed,
) => Promise<void>;

export const parse = (req: Request): Parsed => {
const contentType = undefined;

const parsed: Parsed = {
contentType,
body: {},
params: {},
};

return parsed;
};

export const createRouter = (handler: Handler | undefined): Router => {
const router = Router({ mergeParams: true });
router.use(async (req, res, next) => {
let parsed: Parsed;
try {
parsed = parse(req);
} catch (e) {
if (e instanceof z.ZodError) res.status(422).send(e.issues);
else res.status(500).send(e);
return next();
}
if (handler !== undefined)
handler(req, res, parsed).catch((e) => next(e));
next();
});
return router;
Expand All @@ -376,6 +429,7 @@ export type PastapiHandlers = {
getCookie?: GetCookie.Handler | undefined;
getHeader?: GetHeader.Handler | undefined;
getQuery?: GetQuery.Handler | undefined;
getError?: GetError.Handler | undefined;
};

export function createRouter(handlers: PastapiHandlers): Router {
Expand All @@ -393,6 +447,8 @@ export function createRouter(handlers: PastapiHandlers): Router {

router.get("/query", GetQuery.createRouter(handlers.getQuery));

router.get("/error", GetError.createRouter(handlers.getError));

return router;
}

Expand Down
31 changes: 29 additions & 2 deletions packages/node-express/test/api1/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("api1", () => {
}
);
});
it("should return 422 when giving no Content-Type", async () => {
it("should return 422 when giving wrong Content-Type", async () => {
app.use(
createRouter({
postUser: async (req, res, parsed) => {
Expand All @@ -127,7 +127,7 @@ describe("api1", () => {
},
{
headers: {
"Content-Type": undefined,
"Content-Type": "wrong",
},
}
);
Expand Down Expand Up @@ -336,5 +336,32 @@ describe("api1", () => {
server.close();
});
});

describe("getError", () => {
beforeEach(async () => {
app = express();
server = app.listen(9999);
});

it("should return 500", async () => {
app.use(
createRouter({
getError: async (req, res, parsed) => {
throw new Error("something wrong");
},
})
);
app.use((err, req, res, next) => {
if (err) res.status(500).send("ok");
else next();
});
const res = await get("http://localhost:9999/error");
expect(res.status).to.equal(500);
});

afterEach(() => {
server.close();
});
});
});
});
Loading

0 comments on commit 84dcb15

Please sign in to comment.