diff --git a/packages/core/src/engine.js b/packages/core/src/engine.js index ed7b4296..fc2a0a2f 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 578154fa..ed392596 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 60d0d1b5..9f166bda 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -113,25 +113,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 58653254..53c3f317 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(error.toString().split('\n', 1)[0]); + 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;