From 0fec5ed3351a664724e1627297d0959ad55d0edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Barc=C3=A9los?= Date: Thu, 20 Jan 2022 13:01:16 -0300 Subject: [PATCH] testkit: Fix `Unknown request` error path (#845) There was two different errors during the unknown request error treatment. First, the stringify 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 + packages/testkit-backend/src/stringify.js | 5 +++++ 4 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 packages/testkit-backend/src/stringify.js 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' /** 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 + ) +}