From 66ea68a383840a837df6da516bae02aa538ee9dd Mon Sep 17 00:00:00 2001 From: Nicolas Thouvenin Date: Wed, 25 Oct 2023 15:21:24 +0200 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20improve=20error=20me?= =?UTF-8?q?ssage=20display?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/engine.js | 9 +++++++++ packages/core/src/feed.js | 8 ++++++++ packages/core/src/index.js | 19 +++---------------- packages/core/src/server/errorHandler.js | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/core/src/engine.js b/packages/core/src/engine.js index ed7b42966..fc2a0a2f9 100644 --- a/packages/core/src/engine.js +++ b/packages/core/src/engine.js @@ -33,6 +33,15 @@ function createErrorWith(error, index, funcName, chunk) { const err = Error(msg); err.sourceError = error; err.sourceChunk = JSON.stringify(chunk); + err.toJSON = () => ({ + type: error.type || 'Standard error', + scope: error.scope || 'code', + date: error.date || new Date(), + message: msg.split('\n').shift(), + func: funcName, + index, + chunk, + }); Error.captureStackTrace(err, createErrorWith); debug('ezs')('Caught an', err); return err; diff --git a/packages/core/src/feed.js b/packages/core/src/feed.js index 578154fa2..ed392596b 100644 --- a/packages/core/src/feed.js +++ b/packages/core/src/feed.js @@ -16,6 +16,11 @@ export default class Feed { if (something === null) { this.seal(); } else if (something !== undefined) { + if (something instanceof Error) { + something.type = 'Data corruption error'; + something.scope = 'data'; + something.date = new Date(); + } this.push(something); } } @@ -67,6 +72,9 @@ export default class Feed { } stop(withError) { + withError.type = 'Fatal run-time error'; + withError.scope = 'statements'; + withError.date = new Date(); this.error(withError); this.seal(); } diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 60d0d1b5b..9e96013e7 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -3,6 +3,7 @@ import writeTo from 'stream-write'; import globalModules from 'global-modules'; import { resolve } from 'path'; import LRU from 'lru-cache'; +import _ from 'lodash'; import Engine from './engine'; import Script, { parseCommand } from './script'; import File from './file'; @@ -113,25 +114,11 @@ ezs.createPipeline = (input, commands, trap) => { } return output .pipe(ezs.catch((e) => { - trap.write({ - type: 'Run-time warning', - scope: 'data', - message: e.message.split('\n').shift(), - messageFull: e.message, - sourceError: e.sourceError, - sourceChunk: e.sourceChunk, - }); + trap.write(e.toJSON()); // see engine.js createErrorWith return false; // do not catch the error })) .once('error', (e) => { - trap.write({ - type: 'Fatal run-time error', - scope: 'statements', - message: e.message.split('\n').shift(), - messageFull: e.message, - sourceError: e.sourceError, - sourceChunk: e.sourceChunk, - }); + trap.write(e.toJSON()); // see engine.js createErrorWith trap.end(); }) .once('end', () => { diff --git a/packages/core/src/server/errorHandler.js b/packages/core/src/server/errorHandler.js index 586532549..e6b5f537c 100644 --- a/packages/core/src/server/errorHandler.js +++ b/packages/core/src/server/errorHandler.js @@ -9,7 +9,7 @@ const errorHandler = (request, response) => (error, code = 400) => { response.setHeader('Content-Type', 'text/plain'); response.setHeader('Content-Disposition', 'inline'); response.writeHead(code, { 'X-Error': Parameter.encode(error.toString()) }); - response.write(error.toString().split('\n', 1)[0]); + response.write(JSON.stringify(error)); } response.end(); }; From 4ad17899d12bac30d8ec4c25d01da63301ef7165 Mon Sep 17 00:00:00 2001 From: Nicolas Thouvenin Date: Wed, 25 Oct 2023 17:53:31 +0200 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20json=20error=20bodyre?= =?UTF-8?q?sponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/index.js | 1 - packages/core/src/server/errorHandler.js | 14 ++++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 9e96013e7..9f166bda3 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -3,7 +3,6 @@ import writeTo from 'stream-write'; import globalModules from 'global-modules'; import { resolve } from 'path'; import LRU from 'lru-cache'; -import _ from 'lodash'; import Engine from './engine'; import Script, { parseCommand } from './script'; import File from './file'; diff --git a/packages/core/src/server/errorHandler.js b/packages/core/src/server/errorHandler.js index e6b5f537c..53c3f3171 100644 --- a/packages/core/src/server/errorHandler.js +++ b/packages/core/src/server/errorHandler.js @@ -5,13 +5,15 @@ import { httpRequestErrorTotal } from './metrics'; const errorHandler = (request, response) => (error, code = 400) => { debug('ezs')('Server has caught an error', error); httpRequestErrorTotal.labels(request.pathName).inc(); - if (!response.headersSent) { - response.setHeader('Content-Type', 'text/plain'); - response.setHeader('Content-Disposition', 'inline'); - response.writeHead(code, { 'X-Error': Parameter.encode(error.toString()) }); - response.write(JSON.stringify(error)); + if (response.headersSent) { + return response.end(); } - response.end(); + const bodyResponse = JSON.stringify(error); + response.setHeader('Content-Type', 'application/json'); + response.setHeader('Content-Length', bodyResponse.length); + response.setHeader('Content-Disposition', 'inline'); + response.writeHead(code, { 'X-Error': Parameter.encode(error.toString()) }); + return response.end(bodyResponse); }; export default errorHandler;