Skip to content

Commit f289a76

Browse files
author
Brian Chen
committed
propagate converter into error message
1 parent bcc09ba commit f289a76

File tree

6 files changed

+55
-49
lines changed

6 files changed

+55
-49
lines changed

packages/firestore/lite/src/api/reference.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,7 @@ export function setDoc<T>(
456456
): Promise<void> {
457457
const ref = cast(reference, DocumentReference);
458458

459-
const [convertedValue] = applyFirestoreDataConverter(
460-
ref._converter,
461-
data,
462-
'setDoc'
463-
);
459+
const convertedValue = applyFirestoreDataConverter(ref._converter, data);
464460
const dataReader = newUserDataReader(ref.firestore);
465461
const parsed = dataReader.parseSetData(
466462
'setDoc',
@@ -548,14 +544,16 @@ export function addDoc<T>(
548544
const collRef = cast(reference, CollectionReference);
549545
const docRef = doc(collRef);
550546

551-
const [convertedValue] = applyFirestoreDataConverter(
552-
collRef._converter,
553-
data,
554-
'addDoc'
555-
);
547+
const convertedValue = applyFirestoreDataConverter(collRef._converter, data);
556548

557549
const dataReader = newUserDataReader(collRef.firestore);
558-
const parsed = dataReader.parseSetData('addDoc', docRef._key, convertedValue);
550+
const parsed = dataReader.parseSetData(
551+
'addDoc',
552+
docRef._key,
553+
convertedValue,
554+
{},
555+
docRef._converter !== null
556+
);
559557

560558
return collRef.firestore
561559
._getDatastore()

packages/firestore/lite/src/api/transaction.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,13 @@ export class Transaction implements firestore.Transaction {
9595
options?: firestore.SetOptions
9696
): Transaction {
9797
const ref = validateReference(documentRef, this._firestore);
98-
const [convertedValue] = applyFirestoreDataConverter(
99-
ref._converter,
100-
value,
101-
'Transaction.set'
102-
);
98+
const convertedValue = applyFirestoreDataConverter(ref._converter, value);
10399
const parsed = this._dataReader.parseSetData(
104100
'Transaction.set',
105101
ref._key,
106102
convertedValue,
107-
options
103+
options,
104+
ref._converter !== null
108105
);
109106
this._transaction.set(ref._key, parsed);
110107
return this;

packages/firestore/lite/src/api/write_batch.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,14 @@ export class WriteBatch implements firestore.WriteBatch {
5959
this.verifyNotCommitted();
6060
const ref = validateReference(documentRef, this._firestore);
6161

62-
const [convertedValue] = applyFirestoreDataConverter(
63-
ref._converter,
64-
value,
65-
'WriteBatch.set'
66-
);
62+
const convertedValue = applyFirestoreDataConverter(ref._converter, value);
6763

6864
const parsed = this._dataReader.parseSetData(
6965
'WriteBatch.set',
7066
ref._key,
7167
convertedValue,
72-
options
68+
options,
69+
ref._converter !== null
7370
);
7471
this._mutations = this._mutations.concat(
7572
parsed.toMutations(ref._key, Precondition.none())

packages/firestore/src/api/database.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -744,17 +744,17 @@ export class Transaction implements firestore.Transaction {
744744
this._firestore
745745
);
746746
options = validateSetOptions('Transaction.set', options);
747-
const [convertedValue, functionName] = applyFirestoreDataConverter(
747+
const convertedValue = applyFirestoreDataConverter(
748748
ref._converter,
749749
value,
750-
'Transaction.set',
751750
options
752751
);
753752
const parsed = this._firestore._dataReader.parseSetData(
754-
functionName,
753+
'Transaction.set',
755754
ref._key,
756755
convertedValue,
757-
options
756+
options,
757+
ref._converter !== null
758758
);
759759
this._transaction.set(ref._key, parsed);
760760
return this;
@@ -851,17 +851,17 @@ export class WriteBatch implements firestore.WriteBatch {
851851
this._firestore
852852
);
853853
options = validateSetOptions('WriteBatch.set', options);
854-
const [convertedValue, functionName] = applyFirestoreDataConverter(
854+
const convertedValue = applyFirestoreDataConverter(
855855
ref._converter,
856856
value,
857-
'WriteBatch.set',
858857
options
859858
);
860859
const parsed = this._firestore._dataReader.parseSetData(
861-
functionName,
860+
'WriteBatch.set',
862861
ref._key,
863862
convertedValue,
864-
options
863+
options,
864+
ref._converter !== null
865865
);
866866
this._mutations = this._mutations.concat(
867867
parsed.toMutations(ref._key, Precondition.none())
@@ -1051,17 +1051,17 @@ export class DocumentReference<T = firestore.DocumentData>
10511051
set(value: T | Partial<T>, options?: firestore.SetOptions): Promise<void> {
10521052
validateBetweenNumberOfArgs('DocumentReference.set', arguments, 1, 2);
10531053
options = validateSetOptions('DocumentReference.set', options);
1054-
const [convertedValue, functionName] = applyFirestoreDataConverter(
1054+
const convertedValue = applyFirestoreDataConverter(
10551055
this._converter,
10561056
value,
1057-
'DocumentReference.set',
10581057
options
10591058
);
10601059
const parsed = this.firestore._dataReader.parseSetData(
1061-
functionName,
1060+
'DocumentReference.set',
10621061
this._key,
10631062
convertedValue,
1064-
options
1063+
options,
1064+
this._converter !== null
10651065
);
10661066
return this._firestoreClient.write(
10671067
parsed.toMutations(this._key, Precondition.none())
@@ -2597,9 +2597,8 @@ function resultChangeType(type: ChangeType): firestore.DocumentChangeType {
25972597
export function applyFirestoreDataConverter<T>(
25982598
converter: UntypedFirestoreDataConverter<T> | null,
25992599
value: T,
2600-
functionName: string,
26012600
options?: firestore.SetOptions
2602-
): [firestore.DocumentData, string] {
2601+
): firestore.DocumentData {
26032602
let convertedValue;
26042603
if (converter) {
26052604
if (options && (options.merge || options.mergeFields)) {
@@ -2610,11 +2609,10 @@ export function applyFirestoreDataConverter<T>(
26102609
} else {
26112610
convertedValue = converter.toFirestore(value);
26122611
}
2613-
functionName = 'toFirestore() in ' + functionName;
26142612
} else {
26152613
convertedValue = value as firestore.DocumentData;
26162614
}
2617-
return [convertedValue, functionName];
2615+
return convertedValue;
26182616
}
26192617

26202618
function contains(obj: object, key: string): obj is { key: unknown } {

packages/firestore/src/api/user_data_reader.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ interface ContextSettings {
173173
* If not set, elements are treated as if they were outside of arrays.
174174
*/
175175
readonly arrayElement?: boolean;
176+
/**
177+
* Whether or not a converter was specified in this context. If set, error
178+
* messages will reference the converter when invalid data is provided.
179+
*/
180+
readonly hasConverter?: boolean;
176181
}
177182

178183
/** A "context" object passed around while parsing user data. */
@@ -259,7 +264,8 @@ export class ParseContext {
259264
reason,
260265
this.settings.methodName,
261266
this.path,
262-
this.settings.targetDoc
267+
this.settings.targetDoc,
268+
this.settings.hasConverter
263269
);
264270
}
265271

@@ -314,14 +320,16 @@ export class UserDataReader {
314320
methodName: string,
315321
targetDoc: DocumentKey,
316322
input: unknown,
317-
options: firestore.SetOptions = {}
323+
options: firestore.SetOptions = {},
324+
hasConverter: boolean
318325
): ParsedSetData {
319326
const context = this.createContext(
320327
options.merge || options.mergeFields
321328
? UserDataSource.MergeSet
322329
: UserDataSource.Set,
323330
methodName,
324-
targetDoc
331+
targetDoc,
332+
hasConverter
325333
);
326334
validatePlainObject('Data must be an object, but it was:', context, input);
327335
const updateData = parseObject(input, context)!;
@@ -494,15 +502,17 @@ export class UserDataReader {
494502
private createContext(
495503
dataSource: UserDataSource,
496504
methodName: string,
497-
targetDoc?: DocumentKey
505+
targetDoc?: DocumentKey,
506+
hasConverter = false
498507
): ParseContext {
499508
return new ParseContext(
500509
{
501510
dataSource,
502511
methodName,
503512
targetDoc,
504513
path: FieldPath.emptyPath(),
505-
arrayElement: false
514+
arrayElement: false,
515+
hasConverter
506516
},
507517
this.databaseId,
508518
this.serializer,
@@ -803,10 +813,16 @@ function createError(
803813
reason: string,
804814
methodName: string,
805815
path?: FieldPath,
806-
targetDoc?: DocumentKey
816+
targetDoc?: DocumentKey,
817+
hasConverter = false
807818
): Error {
808819
const hasPath = path && !path.isEmpty();
809820
const hasDocument = targetDoc !== undefined;
821+
let message = `Function ${methodName}() called with invalid data`;
822+
if (hasConverter) {
823+
message += ' (via `toFirestore()`)';
824+
}
825+
message += '. ';
810826

811827
let description = '';
812828
if (hasPath || hasDocument) {
@@ -823,7 +839,7 @@ function createError(
823839

824840
return new FirestoreError(
825841
Code.INVALID_ARGUMENT,
826-
`Function ${methodName}() called with invalid data. ` + reason + description
842+
message + reason + description
827843
);
828844
}
829845

packages/firestore/test/integration/api/database.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,9 +1379,9 @@ apiDescribe('Database', (persistence: boolean) => {
13791379
expect(() =>
13801380
batch.set(ref, { title: 'olive' }, { merge: true })
13811381
).to.throw(
1382-
'Function toFirestore() in WriteBatch.set() called with invalid ' +
1383-
'data. Unsupported field value: undefined (found in field author ' +
1384-
'in document posts/some-post)'
1382+
'Function WriteBatch.set() called with invalid ' +
1383+
'data (via `toFirestore()`). Unsupported field value: undefined ' +
1384+
'(found in field author in document posts/some-post)'
13851385
);
13861386
});
13871387
});

0 commit comments

Comments
 (0)