From a3660137e1d5d1eeb30274662cf323dc34fea7a1 Mon Sep 17 00:00:00 2001 From: Amar kumar singh Date: Mon, 4 Dec 2023 12:20:41 +0530 Subject: [PATCH] terminate middleware and report exception bug fix (#9) * 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 <30429306+developeramarkumar@users.noreply.github.com> --- exception/handler.js | 12 +++++++----- exception/middleware/exceptionHandler.js | 9 ++++++++- package.json | 18 +++++++++--------- view/viewEngine.js | 22 +++++++++++++--------- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/exception/handler.js b/exception/handler.js index c023b12..843442c 100644 --- a/exception/handler.js +++ b/exception/handler.js @@ -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 = {}) { @@ -133,7 +135,7 @@ 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) }) } @@ -141,14 +143,14 @@ class Handler { 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 ') } @@ -161,4 +163,4 @@ class Handler { this[kLogger].channel('console').error($e) } } -module.exports = Handler \ No newline at end of file +module.exports = Handler diff --git a/exception/middleware/exceptionHandler.js b/exception/middleware/exceptionHandler.js index fe8438c..0387aae 100644 --- a/exception/middleware/exceptionHandler.js +++ b/exception/middleware/exceptionHandler.js @@ -9,6 +9,13 @@ class ExceptionHandler { error ) } + terminate(error, {request,response} ,next){ + this.$exceptionHandler.handler('json').terminate( + request, + response, + error + ) + } } -module.exports = ExceptionHandler \ No newline at end of file +module.exports = ExceptionHandler diff --git a/package.json b/package.json index 55611df..9cd7ccd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/view/viewEngine.js b/view/viewEngine.js index d6e5321..dad0073 100644 --- a/view/viewEngine.js +++ b/view/viewEngine.js @@ -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