Skip to content

Commit 8d79dbf

Browse files
committed
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.
1 parent 4002ba9 commit 8d79dbf

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

packages/testkit-backend/src/backend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class Backend {
2525
try {
2626
this._controller.handle(contextId, request)
2727
} catch (e) {
28-
this._channel.writeBackendError(contextId, e)
28+
this._channel.writeBackendError(contextId, e.message)
2929
}
3030
})
3131
}

packages/testkit-backend/src/channel/testkit-protocol.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import readline from 'readline'
22
import EventEmitter from 'events'
3+
import stringify from '../stringify'
34

45
export default class Protocol extends EventEmitter {
56
constructor (stream) {
@@ -61,7 +62,7 @@ export default class Protocol extends EventEmitter {
6162

6263
serializeResponse (response) {
6364
console.log('> writing response', response)
64-
const responseStr = this._stringify(response)
65+
const responseStr = stringify(response)
6566
return ['#response begin', responseStr, '#response end'].join('\n') + '\n'
6667
}
6768

@@ -72,9 +73,4 @@ export default class Protocol extends EventEmitter {
7273
this.emit('request', { name, data })
7374
}
7475

75-
_stringify (val) {
76-
return JSON.stringify(val, (_, value) =>
77-
typeof value === 'bigint' ? `${value}n` : value
78-
)
79-
}
8076
}

packages/testkit-backend/src/controller/local.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Context from '../context'
22
import Controller from './interface'
3+
import stringify from '../stringify'
34

45

56
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function stringify(json) {
2+
return JSON.stringify(json, (_, value) =>
3+
typeof value === 'bigint' ? `${value}n` : value
4+
)
5+
}

0 commit comments

Comments
 (0)