Skip to content

Commit

Permalink
support global exception catch
Browse files Browse the repository at this point in the history
  • Loading branch information
lixinyang123 committed Jan 19, 2024
1 parent 3f014a7 commit 7c18185
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
39 changes: 29 additions & 10 deletions src/core/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ export class App {
return
}

try {
action.invoke(req, res)
}
catch (err) {
action.invoke(req, res).catch(err => {
this.logger.error(err)
res.error('Server Error')
}
})
}

use(callback) {
Expand All @@ -54,18 +51,40 @@ export class App {
}
return this
}

useLogger(logger) {
this.logger = logger
}

map(routePath, callback) {
routePath = routePath.replaceAll('\\', '/')

this.actions.push({
path: routePath,
invoke: callback
})
switch (Object.prototype.toString.call(callback)) {

case '[object AsyncFunction]':
this.actions.push({
path: routePath,
invoke: callback
})
break

case '[object Function]':
this.actions.push({
path: routePath,
invoke: (req, res) => new Promise((resolve, reject) => {
try {
callback(req, res)
resolve()
}
catch (err) { reject(err) }
})
})
break

default:
throw 'need [ Function / AsyncFunction ]'
}

return this
}

Expand Down
1 change: 0 additions & 1 deletion src/core/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export default {
info(msg) {
console.log(msg)
Expand Down

0 comments on commit 7c18185

Please sign in to comment.