Skip to content

Commit

Permalink
terminate middleware and report exception bug fix (#9)
Browse files Browse the repository at this point in the history
* version upgrade

* view async error

* Update package.json

* Update package.json

* terminate middleware for json handler

* terminate reporter for logging

* report exception null validation

---------

Co-authored-by: developeramarkumar <[email protected]>
  • Loading branch information
amarksingh and developeramarkumar authored Dec 4, 2023
1 parent 4d4b9a6 commit a366013
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
12 changes: 7 additions & 5 deletions exception/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class Handler {
constructor(handler) {
this[kHandle] = handler
this[kLogger] = app('logger')

}

handle(request, response, $e = {}) {
this.render(request, response, $e)
}

terminate(request, response, $e = {}) {
this.report($e)
}

render(request, response, $e = {}) {
Expand Down Expand Up @@ -133,22 +135,22 @@ class Handler {
} else {
return fs.readFile(path.join(__dirname, 'template/errors/403'), {
encoding: 'utf8'
}, function(error, data) {
}, function (error, data) {
response.send(data, $e.statusCode)
})
}
}
pageNotFoundException(request, response, $e) {
return fs.readFile(path.join(__dirname, 'template/errors/404'), {
encoding: 'utf8'
}, function(error, data) {
}, function (error, data) {
response.send(data, $e.statusCode)
})
}
report($e) {
if (!this.$dontReport.find((clazz) => $e instanceof clazz)) {
if (typeof this[kLogger].getConfig == 'function' && !this[kLogger].getConfig('ignore_exceptions')) {
if (typeof $e == 'object') {
if ($e && typeof $e == 'object') {
if (typeof $e.stack == 'string') {
$e.capture = $e.stack.split('\n at ')
}
Expand All @@ -161,4 +163,4 @@ class Handler {
this[kLogger].channel('console').error($e)
}
}
module.exports = Handler
module.exports = Handler
9 changes: 8 additions & 1 deletion exception/middleware/exceptionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ class ExceptionHandler {
error
)
}
terminate(error, {request,response} ,next){
this.$exceptionHandler.handler('json').terminate(
request,
response,
error
)
}
}

module.exports = ExceptionHandler
module.exports = ExceptionHandler
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "@ostro/foundation",
"version": "0.0.0-alpha.4",
"version": "1.0.2",
"description": "Foundation for OstroJS",
"main": "application.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@ostro/auth": "^0.0.0-alpha",
"@ostro/config": "^0.0.0-alpha",
"@ostro/container": "^0.0.0-alpha",
"@ostro/contracts": "^0.0.0-alpha",
"@ostro/filesystem": "^0.0.0-alpha",
"@ostro/http": "^0.0.0-alpha",
"@ostro/logger": "^0.0.0-alpha",
"@ostro/support": "^0.0.0-alpha",
"@ostro/auth": "^1.0.0",
"@ostro/config": "^1.0.2",
"@ostro/container": "^1.0.2",
"@ostro/contracts": "^1.0.0",
"@ostro/filesystem": "^1.0.2",
"@ostro/http": "^1.0.5",
"@ostro/logger": "^1.0.0",
"@ostro/support": "^1.0.6",
"dotenv": "10.0.0",
"csrf": "3.1.0",
"fs-extra": "^10.0.0",
Expand Down
22 changes: 13 additions & 9 deletions view/viewEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ class View {
Object.assign(data, context)

app.view.engine(http.response.__viewEngine).renderFile(file, data, async (data) => {
data = await data
if (typeof data == 'object' && data instanceof Promise == false) {
return next(new ViewException(data))
}
try {
http.response.send(data, status)
} catch (e) {
next(e)
}
Promise.resolve(data)
.then(res => {
if (typeof res == 'object' && res instanceof Promise == false) {
throw (new ViewException(res))
}
http.response.send(res, status)



})
.catch(e => {
next(e)
})

})
return http.response
Expand Down

0 comments on commit a366013

Please sign in to comment.