Skip to content

Commit

Permalink
fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Sep 12, 2017
1 parent 0485b58 commit 55e9f14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/moleculer-db/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ module.exports = {
})

.then(doc => {
if (!doc)
return Promise.reject(new EntityNotFoundError(params.id));

origDoc = doc;
return this.transformDocuments(ctx, ctx.params, doc);
})
Expand Down
15 changes: 13 additions & 2 deletions packages/moleculer-db/test/integration/crud.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { ServiceBroker } = require("moleculer");
const DbService = require("../../src");
const { EntityNotFoundError } = require("../../src/errors");
const Adapter = require("../../src/memory-adapter");

function protectReject(err) {
Expand Down Expand Up @@ -93,6 +94,16 @@ describe("Test CRUD methods", () => {
});
});

it("should throw error if entity not found", () => {
return broker.call("posts.get", { id: 123123 }).then(protectReject).catch(res => {
expect(res).toBeInstanceOf(EntityNotFoundError);
expect(res.name).toBe("EntityNotFoundError");
expect(res.code).toBe(404);
expect(res.message).toBe("Entity not found");
expect(res.data.id).toBe(123123);
});
});

it("should return with multiple entity by IDs", () => {
return broker.call("posts.get", { id: [posts[2]._id, posts[0]._id] }).catch(protectReject).then(res => {
expect(res.length).toBe(2);
Expand Down Expand Up @@ -207,7 +218,7 @@ describe("Test CRUD methods", () => {

it("should throw 404 because entity is not exist (remove)", () => {
return broker.call("posts.remove", { id: posts[2]._id }).then(protectReject).catch(res => {
expect(res).toBeInstanceOf(Error);
expect(res).toBeInstanceOf(EntityNotFoundError);
expect(res.name).toBe("EntityNotFoundError");
expect(res.code).toBe(404);
expect(res.message).toBe("Entity not found");
Expand All @@ -217,7 +228,7 @@ describe("Test CRUD methods", () => {

it("should throw 404 because entity is not exist (update)", () => {
return broker.call("posts.update", { id: posts[2]._id, name: "Adam" }).then(protectReject).catch(res => {
expect(res).toBeInstanceOf(Error);
expect(res).toBeInstanceOf(EntityNotFoundError);
expect(res.name).toBe("EntityNotFoundError");
expect(res.code).toBe(404);
expect(res.message).toBe("Entity not found");
Expand Down

0 comments on commit 55e9f14

Please sign in to comment.