Skip to content

Commit e49e0b4

Browse files
committed
Merge pull request #16 from BBB/feature/handle-cursor-errors
Handle cursor errors
2 parents a8fa3ed + f19b32e commit e49e0b4

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,10 @@ class DSRethinkDBAdapter {
353353
options = options || {}
354354
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
355355
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).insert(attrs, {returnChanges: true}).run()
356-
}).then(cursor => cursor.changes[0].new_val)
356+
}).then(cursor => {
357+
this._handleErrors(cursor)
358+
return cursor.changes[0].new_val
359+
})
357360
}
358361

359362
update (resourceConfig, id, attrs, options) {
@@ -362,6 +365,7 @@ class DSRethinkDBAdapter {
362365
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
363366
return this.r.db(options.db || this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, {returnChanges: true}).run()
364367
}).then(cursor => {
368+
this._handleErrors(cursor)
365369
if (cursor.changes && cursor.changes.length && cursor.changes[0].new_val) {
366370
return cursor.changes[0].new_val
367371
} else {
@@ -377,6 +381,7 @@ class DSRethinkDBAdapter {
377381
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(() => {
378382
return this.filterSequence(this.selectTable(resourceConfig, options), params).update(attrs, {returnChanges: true}).run()
379383
}).then(cursor => {
384+
this._handleErrors(cursor)
380385
if (cursor && cursor.changes && cursor.changes.length) {
381386
let items = []
382387
cursor.changes.forEach(change => items.push(change.new_val))
@@ -401,6 +406,15 @@ class DSRethinkDBAdapter {
401406
return this.filterSequence(this.selectTable(resourceConfig, options), params).delete().run()
402407
}).then(() => undefined)
403408
}
409+
410+
_handleErrors (cursor) {
411+
if (cursor && cursor.errors > 0) {
412+
if (cursor.first_error) {
413+
throw new Error(cursor.first_error)
414+
}
415+
throw new Error('Unknown RethinkDB Error')
416+
}
417+
}
404418
}
405419

406420
module.exports = DSRethinkDBAdapter

test/handleErrors.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
describe('handleErrors', function () {
2+
it('should do nothing when passed a falsy value', function () {
3+
var error
4+
try {
5+
adapter._handleErrors(false)
6+
} catch(err) {
7+
error = err
8+
}
9+
assert.equal(error, undefined)
10+
})
11+
it('should do nothing when errors is 0', function () {
12+
var error
13+
try {
14+
adapter._handleErrors({
15+
errors: 0
16+
})
17+
} catch(err) {
18+
error = err
19+
}
20+
assert.equal(error, undefined)
21+
})
22+
it('should throw an error when errors > 0 && first_error is a string', function () {
23+
var errorString = 'error string', error
24+
try {
25+
adapter._handleErrors({
26+
errors: 1,
27+
first_error: errorString
28+
})
29+
} catch(err) {
30+
error = err
31+
}
32+
assert.equal(error.message, errorString)
33+
})
34+
it('should throw a generic error when errors > 0 && first_error is nothing', function () {
35+
var error
36+
try {
37+
adapter._handleErrors({
38+
errors: 1,
39+
})
40+
} catch(err) {
41+
error = err
42+
}
43+
assert.equal(error.message, 'Unknown RethinkDB Error')
44+
})
45+
})

0 commit comments

Comments
 (0)