Skip to content

Commit 228537c

Browse files
authored
Provide better error messages when encoding is not available (#3953)
* Rewrite FirestoreEncoderTests using assertThat * Throw invalidValue for use of invalid top-level values * Provide an message for not supported errors
1 parent c19227d commit 228537c

File tree

6 files changed

+248
-263
lines changed

6 files changed

+248
-263
lines changed

Firestore/Swift/Source/Codable/CodableErrors.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616

1717
public enum FirestoreDecodingError: Error {
18-
case decodingIsNotSupported
18+
case decodingIsNotSupported(String)
1919
case fieldNameConfict(String)
2020
}
2121

2222
public enum FirestoreEncodingError: Error {
23-
case encodingIsNotSupported
23+
case encodingIsNotSupported(String)
2424
}

Firestore/Swift/Source/Codable/DocumentReference+Codable.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ private protocol CodableDocumentReference: Codable {}
3030

3131
/**
3232
* DocumentReference's codable implmentation will just throw for most
33-
* encoder/decoder however. It is only meant to be encoded by FirestoreEncoder/FirestoreDecoder.
33+
* encoder/decoder however. It is only meant to be encoded by Firestore.Encoder/Firestore.Decoder.
3434
*/
3535
extension CodableDocumentReference {
3636
public init(from decoder: Decoder) throws {
37-
throw FirestoreDecodingError.decodingIsNotSupported
37+
throw FirestoreDecodingError.decodingIsNotSupported(
38+
"DocumentReference values can only be decoded with Firestore.Decoder"
39+
)
3840
}
3941

4042
public func encode(to encoder: Encoder) throws {
41-
throw FirestoreEncodingError.encodingIsNotSupported
43+
throw FirestoreEncodingError.encodingIsNotSupported(
44+
"DocumentReference values can only be encoded with Firestore.Encoder"
45+
)
4246
}
4347
}
4448

Firestore/Swift/Source/Codable/FieldValue+Encodable.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import FirebaseFirestore
1919
/** Extends FieldValue to conform to Encodable. */
2020
extension FieldValue: Encodable {
2121
/// Encoding a FieldValue will throw by default unless the encoder implementation
22-
/// explicitly handles it, which is what FirestoreEncoder does.
22+
/// explicitly handles it, which is what Firestore.Encoder does.
2323
public func encode(to encoder: Encoder) throws {
24-
throw FirestoreEncodingError.encodingIsNotSupported
24+
throw FirestoreEncodingError.encodingIsNotSupported(
25+
"FieldValue values can only be encoded with Firestore.Encoder")
2526
}
2627
}
2728

Firestore/Swift/Source/Codable/SelfDocumentId.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import FirebaseFirestore
1818

1919
/// A value that is populated in Codable objects with a `DocumentReference` by
20-
/// the FirestoreDecoder when a document is read.
20+
/// the Firestore.Decoder when a document is read.
2121
///
2222
/// Note that limitations in Swift compiler-generated Codable implementations
2323
/// prevent using this type wrapped in an Optional. Optional SelfDocumentIDs
@@ -33,7 +33,7 @@ import FirebaseFirestore
3333
/// another without adjusting the value here.
3434
///
3535
/// NOTE: Trying to encode/decode this type using encoders/decoders other than
36-
/// FirestoreEncoder leads to an error.
36+
/// Firestore.Encoder leads to an error.
3737
public final class SelfDocumentID: Equatable, Codable {
3838
// MARK: - Initializers
3939

@@ -48,11 +48,13 @@ public final class SelfDocumentID: Equatable, Codable {
4848
// MARK: - `Codable` implemention.
4949

5050
public init(from decoder: Decoder) throws {
51-
throw FirestoreDecodingError.decodingIsNotSupported
51+
throw FirestoreDecodingError.decodingIsNotSupported(
52+
"SelfDocumentID values can only be decoded with Firestore.Decoder")
5253
}
5354

5455
public func encode(to encoder: Encoder) throws {
55-
throw FirestoreEncodingError.encodingIsNotSupported
56+
throw FirestoreEncodingError.encodingIsNotSupported(
57+
"SelfDocumentID values can only be encoded with Firestore.Encoder")
5658
}
5759

5860
// MARK: - Properties

0 commit comments

Comments
 (0)