Skip to content

Commit 3e3c037

Browse files
committed
feat(ts): handle internal server errors and avoid failing a whole app
Part of #48
1 parent 7083cff commit 3e3c037

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

examples/ts/express/mysql/routes.ts

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { Express, Request, Response } from 'express'
1+
import { Express, NextFunction, Request, Response } from 'express'
22
import { Pool } from 'mysql'
33

44
const register = (app: Express, pool: Pool) => {
55

6-
app.get('/v1/categories/count', (req: Request, res: Response) => {
6+
app.get('/v1/categories/count', (req: Request, res: Response, next: NextFunction) => {
77
pool.query(
88
'SELECT COUNT(*) AS counter FROM categories',
99
(err, rows, fields) => {
1010
if (err) {
11-
throw err
11+
return next(err)
1212
}
1313
if (rows.length === 0) {
1414
res.status(404).end()
@@ -19,13 +19,13 @@ const register = (app: Express, pool: Pool) => {
1919
)
2020
})
2121

22-
app.get('/v1/collections/:collectionId/categories/count', (req: Request, res: Response) => {
22+
app.get('/v1/collections/:collectionId/categories/count', (req: Request, res: Response, next: NextFunction) => {
2323
pool.query(
2424
'SELECT COUNT(DISTINCT s.category_id) AS counter FROM collections_series cs JOIN series s ON s.id = cs.series_id WHERE cs.collection_id = :collectionId',
2525
{ "collectionId": req.params.collectionId },
2626
(err, rows, fields) => {
2727
if (err) {
28-
throw err
28+
return next(err)
2929
}
3030
if (rows.length === 0) {
3131
res.status(404).end()
@@ -36,39 +36,39 @@ const register = (app: Express, pool: Pool) => {
3636
)
3737
})
3838

39-
app.get('/v1/categories', (req: Request, res: Response) => {
39+
app.get('/v1/categories', (req: Request, res: Response, next: NextFunction) => {
4040
pool.query(
4141
'SELECT id , name , name_ru , slug FROM categories LIMIT :limit',
4242
{ "limit": req.query.limit },
4343
(err, rows, fields) => {
4444
if (err) {
45-
throw err
45+
return next(err)
4646
}
4747
res.json(rows)
4848
}
4949
)
5050
})
5151

52-
app.post('/v1/categories', (req: Request, res: Response) => {
52+
app.post('/v1/categories', (req: Request, res: Response, next: NextFunction) => {
5353
pool.query(
5454
'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :name_ru , :slug , NOW() , :user_id , NOW() , :user_id )',
5555
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id },
5656
(err, rows, fields) => {
5757
if (err) {
58-
throw err
58+
return next(err)
5959
}
6060
res.sendStatus(204)
6161
}
6262
)
6363
})
6464

65-
app.get('/v1/categories/:categoryId', (req: Request, res: Response) => {
65+
app.get('/v1/categories/:categoryId', (req: Request, res: Response, next: NextFunction) => {
6666
pool.query(
6767
'SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId',
6868
{ "categoryId": req.params.categoryId },
6969
(err, rows, fields) => {
7070
if (err) {
71-
throw err
71+
return next(err)
7272
}
7373
if (rows.length === 0) {
7474
res.status(404).end()
@@ -79,32 +79,36 @@ const register = (app: Express, pool: Pool) => {
7979
)
8080
})
8181

82-
app.put('/v1/categories/:categoryId', (req: Request, res: Response) => {
82+
app.put('/v1/categories/:categoryId', (req: Request, res: Response, next: NextFunction) => {
8383
pool.query(
8484
'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId',
8585
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id, "categoryId": req.params.categoryId },
8686
(err, rows, fields) => {
8787
if (err) {
88-
throw err
88+
return next(err)
8989
}
9090
res.sendStatus(204)
9191
}
9292
)
9393
})
9494

95-
app.delete('/v1/categories/:categoryId', (req: Request, res: Response) => {
95+
app.delete('/v1/categories/:categoryId', (req: Request, res: Response, next: NextFunction) => {
9696
pool.query(
9797
'DELETE FROM categories WHERE id = :categoryId',
9898
{ "categoryId": req.params.categoryId },
9999
(err, rows, fields) => {
100100
if (err) {
101-
throw err
101+
return next(err)
102102
}
103103
res.sendStatus(204)
104104
}
105105
)
106106
})
107107

108+
app.use((error: any, req: Request, res: Response, next: NextFunction) => {
109+
console.error(error)
110+
res.status(500).json({ "error": "Internal Server Error" })
111+
})
108112
}
109113

110114
exports.register = register;

src/templates/routes.ts.ejs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Express, Request, Response } from 'express'
1+
import { Express, NextFunction, Request, Response } from 'express'
22
import { Pool } from 'mysql'
33

44
const register = (app: Express, pool: Pool) => {
@@ -21,12 +21,12 @@ endpoints.forEach(function(endpoint) {
2121
2222
if (hasGetOne || hasGetMany) {
2323
%>
24-
app.get('<%- path %>', (req: Request, res: Response) => {
24+
app.get('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
2525
pool.query(
2626
'<%- sql %>',<%- formattedParams %>
2727
(err, rows, fields) => {
2828
if (err) {
29-
throw err
29+
return next(err)
3030
}
3131
<% if (hasGetMany) { -%>
3232
res.json(rows)
@@ -44,12 +44,12 @@ endpoints.forEach(function(endpoint) {
4444
}
4545
if (method.name === 'post') {
4646
%>
47-
app.post('<%- path %>', (req: Request, res: Response) => {
47+
app.post('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
4848
pool.query(
4949
'<%- sql %>',<%- formattedParams %>
5050
(err, rows, fields) => {
5151
if (err) {
52-
throw err
52+
return next(err)
5353
}
5454
res.sendStatus(204)
5555
}
@@ -59,12 +59,12 @@ endpoints.forEach(function(endpoint) {
5959
}
6060
if (method.name === 'put') {
6161
%>
62-
app.put('<%- path %>', (req: Request, res: Response) => {
62+
app.put('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
6363
pool.query(
6464
'<%- sql %>',<%- formattedParams %>
6565
(err, rows, fields) => {
6666
if (err) {
67-
throw err
67+
return next(err)
6868
}
6969
res.sendStatus(204)
7070
}
@@ -74,12 +74,12 @@ endpoints.forEach(function(endpoint) {
7474
}
7575
if (method.name === 'delete') {
7676
%>
77-
app.delete('<%- path %>', (req: Request, res: Response) => {
77+
app.delete('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
7878
pool.query(
7979
'<%- sql %>',<%- formattedParams %>
8080
(err, rows, fields) => {
8181
if (err) {
82-
throw err
82+
return next(err)
8383
}
8484
res.sendStatus(204)
8585
}
@@ -90,6 +90,10 @@ endpoints.forEach(function(endpoint) {
9090
});
9191
});
9292
%>
93+
app.use((error: any, req: Request, res: Response, next: NextFunction) => {
94+
console.error(error)
95+
res.status(500).json({ "error": "Internal Server Error" })
96+
})
9397
}
9498

9599
exports.register = register;

0 commit comments

Comments
 (0)