From 4ad17899d12bac30d8ec4c25d01da63301ef7165 Mon Sep 17 00:00:00 2001 From: Nicolas Thouvenin Date: Wed, 25 Oct 2023 17:53:31 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20json=20error=20bodyrespon?= =?UTF-8?q?se?= 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 9e96013e..9f166bda 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 e6b5f537..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(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;