Skip to content

Commit 6d46519

Browse files
committed
Make thrown errors actual Error objects and simplify messages
1 parent ac9de13 commit 6d46519

File tree

5 files changed

+142
-127
lines changed

5 files changed

+142
-127
lines changed

src/internal/stream-observers.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,7 @@ class FailedObserver extends ResultStreamObserver {
484484
constructor ({ error, onError }) {
485485
super({ beforeError: onError })
486486

487-
if (error instanceof Error) {
488-
this.onError(error)
489-
} else if (isString(error)) {
490-
this.onError({ error: error })
491-
}
487+
this.onError(error)
492488
}
493489
}
494490

src/transaction.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
FailedObserver,
3030
CompletedObserver
3131
} from './internal/stream-observers'
32+
import { newError } from './error'
3233

3334
/**
3435
* Represents a transaction in the Neo4j database.
@@ -210,10 +211,9 @@ let _states = {
210211
return {
211212
result: newCompletedResult(
212213
new FailedObserver({
213-
error:
214-
'Cannot commit statements in this transaction, because previous statements in the ' +
215-
'transaction has failed and the transaction has been rolled back. Please start a new ' +
216-
'transaction to run another statement.',
214+
error: newError(
215+
'Cannot commit this transaction, because it has been rolled back either because of an error or explicit termination.'
216+
),
217217
onError
218218
}),
219219
'COMMIT',
@@ -235,9 +235,9 @@ let _states = {
235235
) => {
236236
return newCompletedResult(
237237
new FailedObserver({
238-
error:
239-
'Cannot run statement, because previous statements in the ' +
240-
'transaction has failed and the transaction has already been rolled back.',
238+
error: newError(
239+
'Cannot run statement in this transaction, because it has been rolled back either because of an error or explicit termination.'
240+
),
241241
onError
242242
}),
243243
statement,
@@ -252,10 +252,9 @@ let _states = {
252252
return {
253253
result: newCompletedResult(
254254
new FailedObserver({
255-
error:
256-
'Cannot commit statements in this transaction, because commit has already been ' +
257-
'successfully called on the transaction and transaction has been closed. Please ' +
258-
'start a new transaction to run another statement.',
255+
error: newError(
256+
'Cannot commit this transaction, because it has already been committed.'
257+
),
259258
onError
260259
}),
261260
'COMMIT',
@@ -268,8 +267,9 @@ let _states = {
268267
return {
269268
result: newCompletedResult(
270269
new FailedObserver({
271-
error:
272-
'Cannot rollback transaction, because transaction has already been successfully closed.',
270+
error: newError(
271+
'Cannot rollback this transaction, because it has already been committed.'
272+
),
273273
onError
274274
}),
275275
'ROLLBACK',
@@ -285,8 +285,9 @@ let _states = {
285285
) => {
286286
return newCompletedResult(
287287
new FailedObserver({
288-
error:
289-
'Cannot run statement, because transaction has already been successfully closed.',
288+
error: newError(
289+
'Cannot run statement in this transaction, because it has already been committed.'
290+
),
290291
onError
291292
}),
292293
statement,
@@ -301,8 +302,9 @@ let _states = {
301302
return {
302303
result: newCompletedResult(
303304
new FailedObserver({
304-
error:
305-
'Cannot commit this transaction, because it has already been rolled back.',
305+
error: newError(
306+
'Cannot commit this transaction, because it has already been rolled back.'
307+
),
306308
onError
307309
}),
308310
'COMMIT',
@@ -315,8 +317,9 @@ let _states = {
315317
return {
316318
result: newCompletedResult(
317319
new FailedObserver({
318-
error:
319-
'Cannot rollback transaction, because transaction has already been rolled back.'
320+
error: newError(
321+
'Cannot rollback this transaction, because it has already been rolled back.'
322+
)
320323
}),
321324
'ROLLBACK',
322325
{}
@@ -331,8 +334,9 @@ let _states = {
331334
) => {
332335
return newCompletedResult(
333336
new FailedObserver({
334-
error:
335-
'Cannot run statement, because transaction has already been rolled back.',
337+
error: newError(
338+
'Cannot run statement in this transaction, because it has already been rolled back.'
339+
),
336340
onError
337341
}),
338342
statement,

test/internal/node/encryption.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ describe('#integration encryption', () => {
7474
sharedNeo4j.restart(tlsConfig(tlsLevel))
7575

7676
const config = {
77-
encrypted: encrypted,
78-
logging: sharedNeo4j.logging
77+
encrypted: encrypted
7978
}
8079
if (trust) {
8180
config.trust = trust

test/rx/transaction.test.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ describe('#integration-rx transaction', () => {
189189
expect(result).toEqual([
190190
Notification.createError(
191191
jasmine.objectContaining({
192-
error: jasmine.stringMatching(
193-
/Cannot commit statements in this transaction/
192+
message: jasmine.stringMatching(
193+
/Cannot commit this transaction, because .* of an error/
194194
)
195195
})
196196
)
@@ -237,8 +237,8 @@ describe('#integration-rx transaction', () => {
237237
expect(result).toEqual([
238238
Notification.createError(
239239
jasmine.objectContaining({
240-
error: jasmine.stringMatching(
241-
/Cannot commit statements in this transaction/
240+
message: jasmine.stringMatching(
241+
/Cannot commit this transaction, because .* of an error/
242242
)
243243
})
244244
)
@@ -286,17 +286,15 @@ describe('#integration-rx transaction', () => {
286286
expect(result).toEqual([
287287
Notification.createError(
288288
jasmine.objectContaining({
289-
error: jasmine.stringMatching(
290-
/Cannot run statement, because previous statements in the transaction has failed/
289+
message: jasmine.stringMatching(
290+
/Cannot run statement in this transaction, because .* of an error/
291291
)
292292
})
293293
)
294294
])
295295
})
296296

297-
it('should allow commit after commit', async () => {
298-
pending('behaviour difference across drivers')
299-
297+
it('should not allow commit after commit', async () => {
300298
if (serverVersion.compareTo(VERSION_4_0_0) < 0) {
301299
return
302300
}
@@ -313,12 +311,18 @@ describe('#integration-rx transaction', () => {
313311
toArray()
314312
)
315313
.toPromise()
316-
expect(result).toEqual([Notification.createComplete()])
314+
expect(result).toEqual([
315+
Notification.createError(
316+
jasmine.objectContaining({
317+
message: jasmine.stringMatching(
318+
/Cannot commit this transaction, because .* committed/
319+
)
320+
})
321+
)
322+
])
317323
})
318324

319-
it('should allow rollback after rollback', async () => {
320-
pending('behaviour difference across drivers')
321-
325+
it('should not allow rollback after rollback', async () => {
322326
if (serverVersion.compareTo(VERSION_4_0_0) < 0) {
323327
return
324328
}
@@ -335,7 +339,15 @@ describe('#integration-rx transaction', () => {
335339
toArray()
336340
)
337341
.toPromise()
338-
expect(result).toEqual([Notification.createComplete()])
342+
expect(result).toEqual([
343+
Notification.createError(
344+
jasmine.objectContaining({
345+
message: jasmine.stringMatching(
346+
/Cannot rollback this transaction, because .* rolled back/
347+
)
348+
})
349+
)
350+
])
339351
})
340352

341353
it('should fail to rollback after commit', async () => {
@@ -358,8 +370,8 @@ describe('#integration-rx transaction', () => {
358370
expect(result).toEqual([
359371
Notification.createError(
360372
jasmine.objectContaining({
361-
error: jasmine.stringMatching(
362-
/Cannot rollback transaction, because transaction has already been successfully closed/
373+
message: jasmine.stringMatching(
374+
/Cannot rollback this transaction, because .* committed/
363375
)
364376
})
365377
)
@@ -386,8 +398,8 @@ describe('#integration-rx transaction', () => {
386398
expect(result).toEqual([
387399
Notification.createError(
388400
jasmine.objectContaining({
389-
error: jasmine.stringMatching(
390-
/Cannot commit this transaction, because it has already been rolled back/
401+
message: jasmine.stringMatching(
402+
/Cannot commit this transaction, because .* rolled back/
391403
)
392404
})
393405
)
@@ -596,8 +608,8 @@ describe('#integration-rx transaction', () => {
596608
expect(result).toEqual([
597609
Notification.createError(
598610
jasmine.objectContaining({
599-
error: jasmine.stringMatching(
600-
/Cannot run statement, because transaction/
611+
message: jasmine.stringMatching(
612+
/Cannot run statement in this transaction, because/
601613
)
602614
})
603615
)

0 commit comments

Comments
 (0)