Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit db07876

Browse files
✨ Add global error handling
1 parent 27f57d5 commit db07876

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@types/mustache": "^0.8.32",
3838
"bcrypt": "^3.0.6",
39+
"express-async-handler": "^1.1.4",
3940
"fs-extra": "^7.0.1",
4041
"jsonwebtoken": "^8.5.1",
4142
"marked": "^0.6.2",

src/helpers/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const errorHandler = (error: string) => {
2+
const errorString = error.toString();
3+
if (errorString.startsWith("JsonWebTokenError"))
4+
return { status: 401, code: "invalid-token" };
5+
return { status: 500, code: "server-error" };
6+
};

src/index.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
import express from "express";
1+
import express, { Request, Response, NextFunction } from "express";
22
import { PORT } from "./config";
33
import { register, verifyEmail } from "./rest/auth";
4+
import asyncHandler from "express-async-handler";
5+
import { HTTPError } from "./interfaces/general";
6+
import { errorHandler } from "./helpers/errors";
47

58
const app = express();
69

710
app.get("/", (req, res) => res.json({ hello: "world" }));
8-
app.get("/create-account", async (req, res) => {
9-
try {
10-
const users = await register(
11-
{ name: "Anand Chowdhary" },
12-
"anand@oswaldlabs.com"
13-
);
14-
res.json({ success: true, users });
15-
} catch (error) {
16-
console.log("Error", error);
17-
res.json({ success: false });
18-
}
11+
12+
app.put("/user", async (req, res) => {
13+
const user = req.body;
14+
const email = user.email;
15+
delete user.email;
16+
const users = await register(user, email);
17+
res.json({ success: true, users });
1918
});
20-
app.get("/verify-email/:token", async (req, res) => {
21-
try {
22-
res.json({ success: await verifyEmail(req.params.token) });
23-
} catch (error) {
24-
console.log("Error", error);
25-
res.json({ success: false });
26-
}
19+
20+
app.get(
21+
"/verify-email/:token",
22+
asyncHandler(async (req, res) => {
23+
await verifyEmail(req.params.token);
24+
res.json({ success: true });
25+
})
26+
);
27+
28+
app.use((error: any, req: Request, res: Response, next: NextFunction) => {
29+
const response: HTTPError = errorHandler(error.toString());
30+
res.status(response.status);
31+
res.json({ error: response.code, message: response.message });
2732
});
2833

2934
app.listen(PORT, () => console.log("App running"));

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,11 @@ expect@^24.7.1:
14551455
jest-message-util "^24.7.1"
14561456
jest-regex-util "^24.3.0"
14571457

1458+
express-async-handler@^1.1.4:
1459+
version "1.1.4"
1460+
resolved "https://registry.yarnpkg.com/express-async-handler/-/express-async-handler-1.1.4.tgz#225a84908df63b35ae9df94b6f0f1af061266426"
1461+
integrity sha512-HdmbVF4V4w1q/iz++RV7bUxIeepTukWewiJGkoCKQMtvPF11MLTa7It9PRc/reysXXZSEyD4Pthchju+IUbMiQ==
1462+
14581463
express@^4.16.4:
14591464
version "4.16.4"
14601465
resolved "https://registry.yarnpkg.com/express/-/express-4.16.4.tgz#fddef61926109e24c515ea97fd2f1bdbf62df12e"

0 commit comments

Comments
 (0)