full
This commit is contained in:
20
utils/asyncHandler.js
Normal file
20
utils/asyncHandler.js
Normal file
@@ -0,0 +1,20 @@
|
||||
function asyncHandler (handler) {
|
||||
return (req, res, next) => {
|
||||
Promise.resolve(handler(req, res, next)).catch((error) => {
|
||||
console.error('[asyncHandler]', error)
|
||||
|
||||
if (res.headersSent) {
|
||||
return next(error)
|
||||
}
|
||||
|
||||
const status = error?.status || 500
|
||||
return res.status(status).json({
|
||||
message: error?.userMessage || 'خطای سرور',
|
||||
en_message: error?.message || 'internal_error',
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = asyncHandler
|
||||
module.exports.asyncHandler = asyncHandler
|
||||
Reference in New Issue
Block a user