Skip to content

Commit

Permalink
chore(ts): move an exception handler in order to catch exceptions fro…
Browse files Browse the repository at this point in the history
…m custom routes

Part of #27
Relate to #48
  • Loading branch information
php-coder committed Apr 21, 2024
1 parent 23af839 commit efdea2a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions examples/ts/express/mysql/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express'
import { NextFunction, Request, Response } from 'express'
import mysql from 'mysql'

const routes = require('./routes')
Expand Down Expand Up @@ -37,6 +38,11 @@ const pool = mysql.createPool({
routes.register(app, pool)
custom_routes.register(app, pool)

app.use((error: any, req: Request, res: Response, next: NextFunction) => {
console.error(error)
res.status(500).json({ "error": "Internal Server Error" })
})

const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Listen on ${port}`)
Expand Down
4 changes: 4 additions & 0 deletions examples/ts/express/mysql/custom_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ exports.register = (app: Express, pool: Pool) => {
res.json({ "custom": true })
})

app.get('/custom/exception', (req: Request, res: Response, next: NextFunction) => {
throw new Error('expected err')
})

}
4 changes: 0 additions & 4 deletions examples/ts/express/mysql/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ const register = (app: Express, pool: Pool) => {
)
})

app.use((error: any, req: Request, res: Response, next: NextFunction) => {
console.error(error)
res.status(500).json({ "error": "Internal Server Error" })
})
}

exports.register = register
6 changes: 6 additions & 0 deletions src/templates/app.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function removeExtension(filename) {
}
-%>
import express from 'express'
import { NextFunction, Request, Response } from 'express'
import mysql from 'mysql'

const routes = require('./routes')
Expand Down Expand Up @@ -53,6 +54,11 @@ routes.register(app, pool)
<%- routerName %>.register(app, pool)
<% }) -%>

app.use((error: any, req: Request, res: Response, next: NextFunction) => {
console.error(error)
res.status(500).json({ "error": "Internal Server Error" })
})

const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Listen on ${port}`)
Expand Down
4 changes: 0 additions & 4 deletions src/templates/routes.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ endpoints.forEach(function(endpoint) {
})
})
%>
app.use((error: any, req: Request, res: Response, next: NextFunction) => {
console.error(error)
res.status(500).json({ "error": "Internal Server Error" })
})
}

exports.register = register

0 comments on commit efdea2a

Please sign in to comment.