Skip to content

Commit

Permalink
fix(webiny-entity): Throw EntityError instead of plain Error
Browse files Browse the repository at this point in the history
close #9
  • Loading branch information
adrians5j committed Jan 28, 2018
1 parent fbfa466 commit c314f31
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
14 changes: 9 additions & 5 deletions packages-api/webiny-entity/src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import EntityCollection from "./entityCollection";
import EntityModel from "./entityModel";
import EntityAttributesContainer from "./entityAttributesContainer";
import QueryResult from "./queryResult";
import { EntityError } from "./index";

declare type EntitySaveParams = {
validation?: boolean,
Expand Down Expand Up @@ -69,12 +70,12 @@ class Entity {
});

const modelClass = this.getDriver().getModelClass();
this.model = new modelClass().setParentEntity(proxy);

if (!this.model) {
throw Error("Entity model is missing.");
if (!modelClass) {
throw new EntityError("Entity model is missing.", EntityError.MODEL_MISSING);
}

this.model = new modelClass().setParentEntity(proxy);

this.listeners = {};
this.existing = false;
this.processing = null;
Expand All @@ -87,7 +88,10 @@ class Entity {

this.on("delete", () => {
if (this.getAttribute("id").isEmpty()) {
throw Error("Entity cannot be deleted because it was not previously saved.");
throw new EntityError(
"Entity cannot be deleted because it was not previously saved.",
EntityError.CANNOT_DELETE_NO_ID
);
}
});

Expand Down
7 changes: 5 additions & 2 deletions packages-api/webiny-entity/src/entityCollection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import { Entity } from ".";
import EntityCollectionError from "./entityCollectionError";

class EntityCollection extends Array<Entity> {
entityCollection: { params: ?Object, meta: ?Object };
Expand All @@ -13,7 +14,7 @@ class EntityCollection extends Array<Entity> {
this.push(item);
return true;
}
throw Error(
throw new EntityCollectionError(
"EntityCollection cannot accept a value that is not an instance of Entity."
);
});
Expand Down Expand Up @@ -42,7 +43,9 @@ class EntityCollection extends Array<Entity> {
return super.push(value);
}

throw Error("EntityCollection cannot accept a value that is not an instance of Entity.");
throw new EntityCollectionError(
"EntityCollection cannot accept a value that is not an instance of Entity."
);
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages-api/webiny-entity/src/entityCollectionError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow
class EntityCollectionError extends Error {}

export default EntityCollectionError;
5 changes: 5 additions & 0 deletions packages-api/webiny-entity/src/entityError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// @flow
class EntityError extends Error {
static ATTRIBUTE_NOT_FOUND: string;
static CANNOT_DELETE_NO_ID: string;
static MODEL_MISSING: string;

message: string;
data: ?Object;
type: ?string;
Expand Down Expand Up @@ -41,5 +44,7 @@ class EntityError extends Error {
}

EntityError.ATTRIBUTE_NOT_FOUND = "attributeNotFound";
EntityError.CANNOT_DELETE_NO_ID = "cannotDeleteNoId";
EntityError.MODEL_MISSING = "modelMissing";

export default EntityError;
4 changes: 3 additions & 1 deletion packages-api/webiny-entity/tests/driver.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assert } from "chai";
import User from "./entities/user";
import { EntityError } from "../src";

describe("default driver test", function() {
it("save method should return the same user instance", async () => {
Expand All @@ -18,7 +19,8 @@ describe("default driver test", function() {
error = e;
}

assert.instanceOf(error, Error);
assert.instanceOf(error, EntityError);
assert.equal(error.getType(), EntityError.CANNOT_DELETE_NO_ID);

user.id = "ABC";
assert.isUndefined(await user.delete());
Expand Down
4 changes: 3 additions & 1 deletion packages-api/webiny-entity/tests/entityModelMissing.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, Driver } from "./../src";
import { assert } from "chai";
import { EntityError } from "../src";

class ExtendedEntityDriver extends Driver {
constructor() {
Expand All @@ -24,6 +25,7 @@ describe("entity model missing test", function() {
error = e;
}

assert.instanceOf(error, Error);
assert.instanceOf(error, EntityError);
assert.equal(error.getType(), EntityError.MODEL_MISSING);
});
});

0 comments on commit c314f31

Please sign in to comment.