From 747b07d20e37e10aac9a4cc7482143d443927886 Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Thu, 20 Jan 2022 14:41:18 +0100 Subject: [PATCH 1/2] testkit: Fix `Unknown request` error path There was two different errors durint the unknown request error treatment. First, the stringfy function was not define in the LocalController scope. It get solve by extract this function to it own file and import it in the local.js. Second, the backend was capturing the error in the `request` event and calling `writeBackendError` with the error object instead of the message. --- packages/testkit-backend/src/backend.js | 2 +- packages/testkit-backend/src/channel/testkit-protocol.js | 8 ++------ packages/testkit-backend/src/controller/local.js | 1 + 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/testkit-backend/src/backend.js b/packages/testkit-backend/src/backend.js index f1f8983cd..80fc63265 100644 --- a/packages/testkit-backend/src/backend.js +++ b/packages/testkit-backend/src/backend.js @@ -25,7 +25,7 @@ export default class Backend { try { this._controller.handle(contextId, request) } catch (e) { - this._channel.writeBackendError(contextId, e) + this._channel.writeBackendError(contextId, e.message) } }) } diff --git a/packages/testkit-backend/src/channel/testkit-protocol.js b/packages/testkit-backend/src/channel/testkit-protocol.js index bacc41e64..1670566a3 100644 --- a/packages/testkit-backend/src/channel/testkit-protocol.js +++ b/packages/testkit-backend/src/channel/testkit-protocol.js @@ -1,5 +1,6 @@ import readline from 'readline' import EventEmitter from 'events' +import stringify from '../stringify' export default class Protocol extends EventEmitter { constructor (stream) { @@ -61,7 +62,7 @@ export default class Protocol extends EventEmitter { serializeResponse (response) { console.log('> writing response', response) - const responseStr = this._stringify(response) + const responseStr = stringify(response) return ['#response begin', responseStr, '#response end'].join('\n') + '\n' } @@ -72,9 +73,4 @@ export default class Protocol extends EventEmitter { this.emit('request', { name, data }) } - _stringify (val) { - return JSON.stringify(val, (_, value) => - typeof value === 'bigint' ? `${value}n` : value - ) - } } diff --git a/packages/testkit-backend/src/controller/local.js b/packages/testkit-backend/src/controller/local.js index 1c59ede1b..ae2638e51 100644 --- a/packages/testkit-backend/src/controller/local.js +++ b/packages/testkit-backend/src/controller/local.js @@ -1,5 +1,6 @@ import Context from '../context' import Controller from './interface' +import stringify from '../stringify' /** From f93c722990b46053ad829f55fcf2c8163f4f7e4f Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Thu, 20 Jan 2022 15:33:07 +0100 Subject: [PATCH 2/2] forgot file --- packages/testkit-backend/src/stringify.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 packages/testkit-backend/src/stringify.js diff --git a/packages/testkit-backend/src/stringify.js b/packages/testkit-backend/src/stringify.js new file mode 100644 index 000000000..996e6dad4 --- /dev/null +++ b/packages/testkit-backend/src/stringify.js @@ -0,0 +1,5 @@ +export default function stringify(json) { + return JSON.stringify(json, (_, value) => + typeof value === 'bigint' ? `${value}n` : value + ) +}