Skip to content

Commit 4fd738d

Browse files
committed
Properly handle TestKit's FrontendError
1 parent 44064f0 commit 4fd738d

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Context from '../context'
22
import Controller from './interface'
33
import stringify from '../stringify'
4+
import { isFrontendError } from '../request-handlers'
45

56

67
/**
@@ -24,7 +25,7 @@ export default class LocalController extends Controller {
2425
closeContext (contextId) {
2526
this._contexts.delete(contextId)
2627
}
27-
28+
2829
async handle (contextId, { name, data }) {
2930
if (!this._contexts.has(contextId)) {
3031
throw new Error(`Context ${contextId} does not exist`)
@@ -58,15 +59,21 @@ export default class LocalController extends Controller {
5859

5960
_writeError (contextId, e) {
6061
if (e.name) {
61-
const id = this._contexts.get(contextId).addError(e)
62-
this._writeResponse(contextId, 'DriverError', {
63-
id,
64-
msg: e.message + ' (' + e.code + ')',
65-
code: e.code
66-
})
62+
if (isFrontendError(e)) {
63+
this._writeResponse(contextId, 'FrontendError', {
64+
msg: 'Simulating the client code throwing some error.',
65+
})
66+
} else {
67+
const id = this._contexts.get(contextId).addError(e)
68+
this._writeResponse(contextId, 'DriverError', {
69+
id,
70+
msg: e.message + ' (' + e.code + ')',
71+
code: e.code
72+
})
73+
}
6774
return
6875
}
6976
this._writeBackendError(contextId, e)
7077
}
71-
78+
7279
}

packages/testkit-backend/src/request-handlers.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import neo4j from './neo4j'
2-
import {
3-
cypherToNative,
4-
nativeToCypher,
2+
import {
3+
cypherToNative,
4+
nativeToCypher,
55
} from './cypher-native-binders.js'
66
import {
77
nativeToTestkitSummary,
@@ -21,6 +21,14 @@ const SUPPORTED_TLS = (() => {
2121
return [];
2222
})();
2323

24+
export function throwFrontendError() {
25+
throw new Error("TestKit FrontendError")
26+
}
27+
28+
export function isFrontendError(error) {
29+
return error.message === 'TestKit FrontendError'
30+
}
31+
2432
export function NewDriver (context, data, wire) {
2533
const {
2634
uri,
@@ -107,8 +115,7 @@ export function DriverClose (context, data, wire) {
107115
.then(() => {
108116
wire.writeResponse('Driver', { id: driverId })
109117
})
110-
.catch(err => wire.writeError(err))
111-
.finally(() => context.removeDriver(driverId))
118+
.catch(err => wire.writeError(err))
112119
}
113120

114121
export function NewSession (context, data, wire) {
@@ -213,8 +220,6 @@ export function ResultPeek (context, data, wire) {
213220
}
214221

215222
export function ResultConsume (context, data, wire) {
216-
217-
218223
const { resultId } = data
219224
const result = context.getResult(resultId)
220225

@@ -276,7 +281,7 @@ export function RetryablePositive (context, data, wire) {
276281

277282
export function RetryableNegative (context, data, wire) {
278283
const { sessionId, errorId } = data
279-
const error = context.getError(errorId) || new Error('Client error')
284+
const error = context.getError(errorId) || new Error('TestKit FrontendError')
280285
context.getTxsBySessionId(sessionId).forEach(tx => {
281286
tx.reject(error)
282287
})

0 commit comments

Comments
 (0)