Rethinkdb: Promise error handling

Created on 9 Apr 2017  路  3Comments  路  Source: rethinkdb/rethinkdb

Why catch doesn't fired when i tried to add data row with the same key?

rethinkdb.table('user').insert(user).run(req._rdb).then(result => {
    console.log(result);
    done(null, user, { message: 'Successfull' });
}).catch(error => done(error));

Output from console.log, not from done(error).

{ deleted: 0,
  errors: 1,
  first_error: 'Duplicate primary key ...'
  inserted: 0,
  replaced: 0,
  skipped: 0,
  unchanged: 0 }

All 3 comments

There was an issue about changing this behavior I will see if I can dig it up. There might also have been a flag to change the behavior. For the moment this is working as documented. insert and the rest of the CRUD family of queries return a success document that details how far they progressed.

3440 is the issue I was looking for.

As @grandquista mentioned, this is expected behavior. Consider that multiple writes can be issued in a single query. If the query executes successfully and perhaps only one of these writes failed it doesn't necessarily make sense to raise an exception. That behavior would be determined by how your application interprets the response.

If you want your query to fail when the write fails, you can do something like this to raise an error on the server (though, I would recommend just raising an exception on the client):

r.table('users').insert({...}).do(result => {
  return result('errors').gt(0).branch(
    r.error(result('first_error')),
    result
  )
})

Closing as duplicate of #3440.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lchunleo picture lchunleo  路  4Comments

RubenKelevra picture RubenKelevra  路  3Comments

leon-strong picture leon-strong  路  4Comments

biapar picture biapar  路  5Comments

manyopensource picture manyopensource  路  3Comments