From 557dcc8fd84ef72a3b104f2a28eb8bace4a8956f Mon Sep 17 00:00:00 2001 From: Gabriel JUCHAULT Date: Thu, 23 Mar 2017 00:43:11 +0100 Subject: [PATCH] feat(errors): document not found error when parsing non-existent results (#14) * feat(errors): document not found error when parsing non-existent results * chore: version bump --- lib/model/model.js | 2 +- lib/model/query/query.js | 8 +++++++- package.json | 2 +- test/test.errors.js | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/test.errors.js diff --git a/lib/model/model.js b/lib/model/model.js index a0f8be8..00724c2 100644 --- a/lib/model/model.js +++ b/lib/model/model.js @@ -185,7 +185,7 @@ function createModel (requelize) { * @param {string} ModelA Model name * @param {string} ModelB Model name */ - static customJoinTable(ModelA, ModelB) { + static customJoinTable (ModelA, ModelB) { Model.index(ModelA) Model.index(ModelB) } diff --git a/lib/model/query/query.js b/lib/model/query/query.js index 5ae568b..95ff94c 100644 --- a/lib/model/query/query.js +++ b/lib/model/query/query.js @@ -31,7 +31,13 @@ class Query { // enable model parsing for query result if (this._parse) { - q = q.then((res) => this._Model._parse(res)) + q = q.then((res) => { + if (res) { + return this._Model._parse(res) + } else { + return Promise.reject(new RequelizeError('DocumentNotFound')) + } + }) } return q diff --git a/package.json b/package.json index 844a16a..8f512e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "requelize", - "version": "0.6.0", + "version": "0.6.1", "description": "RethinkDB ORM", "main": "index.js", "repository": "https://github.com/buckless/requelize.git", diff --git a/test/test.errors.js b/test/test.errors.js new file mode 100644 index 0000000..565bf43 --- /dev/null +++ b/test/test.errors.js @@ -0,0 +1,22 @@ +const { test, requelize, dropDb } = require('./utils') + +test('document not found error', (t) => { + t.plan(2) + + let Foo + + dropDb() + .then(() => { + Foo = requelize.model('foo') + + return requelize.sync() + }) + .then(() => { + return Foo.get('foo').run() + }) + .catch((err) => { + t.equal('RequelizeError', err.name) + t.equal('DocumentNotFound', err.message) + t.end() + }) +})