Skip to content

Commit 447d241

Browse files
committed
feat(js): handle internal server errors and avoid failing a whole app
Part of #48
1 parent 3bf753e commit 447d241

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

examples/js/express/mysql/routes.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const register = (app, pool) => {
22

3-
app.get('/v1/categories/count', (req, res) => {
3+
app.get('/v1/categories/count', (req, res, next) => {
44
pool.query(
55
'SELECT COUNT(*) AS counter FROM categories',
66
(err, rows, fields) => {
77
if (err) {
8-
throw err
8+
return next(err)
99
}
1010
if (rows.length === 0) {
1111
res.status(404).end()
@@ -16,13 +16,13 @@ const register = (app, pool) => {
1616
)
1717
})
1818

19-
app.get('/v1/collections/:collectionId/categories/count', (req, res) => {
19+
app.get('/v1/collections/:collectionId/categories/count', (req, res, next) => {
2020
pool.query(
2121
'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',
2222
{ "collectionId": req.params.collectionId },
2323
(err, rows, fields) => {
2424
if (err) {
25-
throw err
25+
return next(err)
2626
}
2727
if (rows.length === 0) {
2828
res.status(404).end()
@@ -33,39 +33,39 @@ const register = (app, pool) => {
3333
)
3434
})
3535

36-
app.get('/v1/categories', (req, res) => {
36+
app.get('/v1/categories', (req, res, next) => {
3737
pool.query(
3838
'SELECT id , name , name_ru , slug FROM categories LIMIT :limit',
3939
{ "limit": req.query.limit },
4040
(err, rows, fields) => {
4141
if (err) {
42-
throw err
42+
return next(err)
4343
}
4444
res.json(rows)
4545
}
4646
)
4747
})
4848

49-
app.post('/v1/categories', (req, res) => {
49+
app.post('/v1/categories', (req, res, next) => {
5050
pool.query(
5151
'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 )',
5252
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id },
5353
(err, rows, fields) => {
5454
if (err) {
55-
throw err
55+
return next(err)
5656
}
5757
res.sendStatus(204)
5858
}
5959
)
6060
})
6161

62-
app.get('/v1/categories/:categoryId', (req, res) => {
62+
app.get('/v1/categories/:categoryId', (req, res, next) => {
6363
pool.query(
6464
'SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId',
6565
{ "categoryId": req.params.categoryId },
6666
(err, rows, fields) => {
6767
if (err) {
68-
throw err
68+
return next(err)
6969
}
7070
if (rows.length === 0) {
7171
res.status(404).end()
@@ -76,32 +76,36 @@ const register = (app, pool) => {
7676
)
7777
})
7878

79-
app.put('/v1/categories/:categoryId', (req, res) => {
79+
app.put('/v1/categories/:categoryId', (req, res, next) => {
8080
pool.query(
8181
'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId',
8282
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "user_id": req.body.user_id, "categoryId": req.params.categoryId },
8383
(err, rows, fields) => {
8484
if (err) {
85-
throw err
85+
return next(err)
8686
}
8787
res.sendStatus(204)
8888
}
8989
)
9090
})
9191

92-
app.delete('/v1/categories/:categoryId', (req, res) => {
92+
app.delete('/v1/categories/:categoryId', (req, res, next) => {
9393
pool.query(
9494
'DELETE FROM categories WHERE id = :categoryId',
9595
{ "categoryId": req.params.categoryId },
9696
(err, rows, fields) => {
9797
if (err) {
98-
throw err
98+
return next(err)
9999
}
100100
res.sendStatus(204)
101101
}
102102
)
103103
})
104104

105+
app.use((error, req, res, next) => {
106+
console.error(error)
107+
res.status(500).json({ "error": "Internal Server Error" })
108+
})
105109
}
106110

107111
exports.register = register;

src/templates/routes.js.ejs

+12-8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ endpoints.forEach(function(endpoint) {
1818
1919
if (hasGetOne || hasGetMany) {
2020
%>
21-
app.get('<%- path %>', (req, res) => {
21+
app.get('<%- path %>', (req, res, next) => {
2222
pool.query(
2323
'<%- sql %>',<%- formattedParams %>
2424
(err, rows, fields) => {
2525
if (err) {
26-
throw err
26+
return next(err)
2727
}
2828
<% if (hasGetMany) { -%>
2929
res.json(rows)
@@ -41,12 +41,12 @@ endpoints.forEach(function(endpoint) {
4141
}
4242
if (method.name === 'post') {
4343
%>
44-
app.post('<%- path %>', (req, res) => {
44+
app.post('<%- path %>', (req, res, next) => {
4545
pool.query(
4646
'<%- sql %>',<%- formattedParams %>
4747
(err, rows, fields) => {
4848
if (err) {
49-
throw err
49+
return next(err)
5050
}
5151
res.sendStatus(204)
5252
}
@@ -56,12 +56,12 @@ endpoints.forEach(function(endpoint) {
5656
}
5757
if (method.name === 'put') {
5858
%>
59-
app.put('<%- path %>', (req, res) => {
59+
app.put('<%- path %>', (req, res, next) => {
6060
pool.query(
6161
'<%- sql %>',<%- formattedParams %>
6262
(err, rows, fields) => {
6363
if (err) {
64-
throw err
64+
return next(err)
6565
}
6666
res.sendStatus(204)
6767
}
@@ -71,12 +71,12 @@ endpoints.forEach(function(endpoint) {
7171
}
7272
if (method.name === 'delete') {
7373
%>
74-
app.delete('<%- path %>', (req, res) => {
74+
app.delete('<%- path %>', (req, res, next) => {
7575
pool.query(
7676
'<%- sql %>',<%- formattedParams %>
7777
(err, rows, fields) => {
7878
if (err) {
79-
throw err
79+
return next(err)
8080
}
8181
res.sendStatus(204)
8282
}
@@ -87,6 +87,10 @@ endpoints.forEach(function(endpoint) {
8787
});
8888
});
8989
%>
90+
app.use((error, req, res, next) => {
91+
console.error(error)
92+
res.status(500).json({ "error": "Internal Server Error" })
93+
})
9094
}
9195

9296
exports.register = register;

0 commit comments

Comments
 (0)