Skip to content

Commit

Permalink
Providing better errors for .doc() and .collection() (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 21, 2017
1 parent 86f1a08 commit 76f8e0c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -66,7 +66,7 @@
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.31",
"duplexify": "^3.5.0",
"eslint": "^4.7.2",
"eslint": "^4.10.0",
"eslint-config-prettier": "^2.6.0",
"eslint-plugin-node": "^5.2.0",
"eslint-plugin-prettier": "^2.3.1",
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Expand Up @@ -268,7 +268,9 @@ class Firestore extends commonGrpc.Service {

let path = this._referencePath.append(documentPath);
if (!path.isDocument) {
throw new Error('Argument "documentPath" must point to a document.');
throw new Error(
`Argument "documentPath" must point to a document, but was "${documentPath}". Your path does not contain an even number of components.`
);
}

return new DocumentReference(this, path);
Expand All @@ -295,7 +297,9 @@ class Firestore extends commonGrpc.Service {

let path = this._referencePath.append(collectionPath);
if (!path.isCollection) {
throw new Error('Argument "collectionPath" must point to a collection.');
throw new Error(
`Argument "collectionPath" must point to a collection, but was "${collectionPath}". Your path does not contain an odd number of components.`
);
}

return new CollectionReference(this, path);
Expand Down
8 changes: 6 additions & 2 deletions src/reference.js
Expand Up @@ -284,7 +284,9 @@ class DocumentReference {

let path = this._referencePath.append(collectionPath);
if (!path.isCollection) {
throw new Error('Argument "collectionPath" must point to a collection.');
throw new Error(
`Argument "collectionPath" must point to a collection, but was "${collectionPath}". Your path does not contain an odd number of components.`
);
}

return createCollectionReference(this._firestore, path);
Expand Down Expand Up @@ -1803,7 +1805,9 @@ class CollectionReference extends Query {

let path = this._referencePath.append(documentPath);
if (!path.isDocument) {
throw new Error('Argument "documentPath" must point to a document.');
throw new Error(
`Argument "documentPath" must point to a document, but was "${documentPath}". Your path does not contain an even number of components.`
);
}

return new DocumentReference(this._firestore, path);
Expand Down
2 changes: 1 addition & 1 deletion test/collection.js
Expand Up @@ -68,7 +68,7 @@ describe('Collection interface', function() {

assert.throws(function() {
collectionRef.doc('doc/coll');
}, /Argument "documentPath" must point to a document\./);
}, /Argument "documentPath" must point to a document, but was "doc\/coll". Your path does not contain an even number of components\./);

documentRef = collectionRef.doc('docId/colId/docId');
assert.ok(is.instance(documentRef, DocumentReference));
Expand Down
2 changes: 1 addition & 1 deletion test/document.js
Expand Up @@ -238,7 +238,7 @@ describe('DocumentReference interface', function() {

assert.throws(() => {
documentRef.collection('col/doc');
}, /Argument "collectionPath" must point to a collection\./);
}, /Argument "collectionPath" must point to a collection, but was "col\/doc". Your path does not contain an odd number of components\./);

collection = documentRef.collection('col/doc/col');
assert.equal(collection.id, 'col');
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Expand Up @@ -662,7 +662,7 @@ describe('doc() method', function() {
it('must point to document', function() {
assert.throws(function() {
firestore.doc('collectionId');
}, /Argument "documentPath" must point to a document\./);
}, /Argument "documentPath" must point to a document, but was "collectionId". Your path does not contain an even number of components\./);
});

it('exposes properties', function() {
Expand Down Expand Up @@ -693,7 +693,7 @@ describe('collection() method', function() {
it('must point to a collection', function() {
assert.throws(function() {
firestore.collection('collectionId/documentId');
}, /Argument "collectionPath" must point to a collection\./);
}, /Argument "collectionPath" must point to a collection, but was "collectionId\/documentId". Your path does not contain an odd number of components\./);
});

it('exposes properties', function() {
Expand Down

0 comments on commit 76f8e0c

Please sign in to comment.