Skip to content

Commit ea417d0

Browse files
committed
Merge branch 'fix-express-error-handle' of github.com:ut-code/CourseMate into make-it-ssg
2 parents bb149bd + 2303fda commit ea417d0

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

server/src/index.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,6 @@ const app = express();
2020
// https://expressjs.com/ja/api.html#app.settings.table の query parser を参照。
2121
app.set("query parser", "simple");
2222

23-
// I don't understand any of those
24-
// https://expressjs.com/en/guide/error-handling.html
25-
// https://qiita.com/nyandora/items/cd4f12eb62295c10269c
26-
// https://note.shiftinc.jp/n/n42b96d36f0cf
27-
// エラーハンドラを Express に管理させる。
28-
// TEMPORARY: will be replaced by hono's `onError`.
29-
app.use(async (err: unknown, _: unknown, res: unknown, next: unknown) => {
30-
try {
31-
if (typeof (err as Error)?.cause === "number") {
32-
(res as Response)
33-
.status((err as Error).cause as number)
34-
.send((err as Error).message);
35-
} else {
36-
console.error(err);
37-
(res as Response).status(500).send("Internal Error");
38-
}
39-
await (next as () => Promise<unknown>)();
40-
} catch (err) {
41-
console.log("[ERR] failed to handle error:", err);
42-
try {
43-
(res as Response).status(500).send("Internal error");
44-
} catch (err) {
45-
console.log("[ERR] failed to handle error twice:", err);
46-
}
47-
} finally {
48-
try {
49-
(res as Response).end("\n");
50-
} catch {}
51-
}
52-
});
53-
5423
const port = process.env.PORT || 3000;
5524
const allowedOrigins = (
5625
process.env.CORS_ALLOW_ORIGINS || panic("env CORS_ALLOW_ORIGINS is missing")
@@ -91,6 +60,37 @@ app.use("/requests", requestsRoutes);
9160
app.use("/matches", matchesRoutes);
9261
app.use("/chat", chatRoutes);
9362

63+
// I don't understand any of those
64+
// https://expressjs.com/en/guide/error-handling.html
65+
// https://qiita.com/nyandora/items/cd4f12eb62295c10269c
66+
// https://note.shiftinc.jp/n/n42b96d36f0cf
67+
// エラーハンドラを Express に管理させる。
68+
// TEMPORARY: will be replaced by hono's `onError`.
69+
app.use(async (err: unknown, _: unknown, res: unknown, next: unknown) => {
70+
try {
71+
if (typeof (err as Error)?.cause === "number") {
72+
(res as Response)
73+
.status((err as Error).cause as number)
74+
.send((err as Error).message);
75+
} else {
76+
console.error(err);
77+
(res as Response).status(500).send("Internal Error");
78+
}
79+
await (next as () => Promise<unknown>)();
80+
} catch (err) {
81+
console.log("[ERR] failed to handle error:", err);
82+
try {
83+
(res as Response).status(500).send("Internal error");
84+
} catch (err) {
85+
console.log("[ERR] failed to handle error twice:", err);
86+
}
87+
} finally {
88+
try {
89+
(res as Response).end("\n");
90+
} catch {}
91+
}
92+
});
93+
9494
export function main() {
9595
// サーバーの起動
9696
const server = app.listen(port, () => {

0 commit comments

Comments
 (0)