Skip to content

Commit a266c55

Browse files
rebello95thomasvl
authored andcommitted
Make anyTranscodeFailure more descriptive
Right now it's quite difficult to identify _which type_ is missing from the `Google_Protobuf_Any` registry when the `anyTranscodeFailure` is thrown without having Xcode and breakpoints to debug the program. This change makes the error more descriptive and actionable by including the type URL that needs to be registered.
1 parent 71f3b3b commit a266c55

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

Sources/SwiftProtobuf/AnyMessageStorage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ internal class AnyMessageStorage {
210210
case .contentJSON(let contentJSON, let options):
211211
// contentJSON requires we have the type available for decoding
212212
guard let messageType = Google_Protobuf_Any.messageType(forTypeURL: _typeURL) else {
213-
throw BinaryEncodingError.anyTranscodeFailure
213+
throw BinaryEncodingError.anyTypeURLNotRegistered(typeURL: _typeURL)
214214
}
215215
do {
216216
// Decodes the full JSON and then discard the result.
@@ -414,7 +414,7 @@ extension AnyMessageStorage {
414414
// binary value, so we're stuck. (The Google spec does not
415415
// provide a way to just package the binary value for someone
416416
// else to decode later.)
417-
throw JSONEncodingError.anyTranscodeFailure
417+
throw JSONEncodingError.anyTypeURLNotRegistered(typeURL: _typeURL)
418418
}
419419
let m = try messageType.init(serializedBytes: valueData, partial: true)
420420
return try serializeAnyJSON(for: m, typeURL: _typeURL, options: options)

Sources/SwiftProtobuf/BinaryEncodingError.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
// -----------------------------------------------------------------------------
1414

1515
/// Describes errors that can occur when decoding a message from binary format.
16-
public enum BinaryEncodingError: Error {
16+
public enum BinaryEncodingError: Error, Equatable {
1717
/// `Any` fields that were decoded from JSON cannot be re-encoded to binary
1818
/// unless the object they hold is a well-known type or a type registered via
1919
/// `Google_Protobuf_Any.register()`.
20+
case anyTypeURLNotRegistered(typeURL: String)
21+
/// An unexpected failure when deserializing a `Google_Protobuf_Any`.
2022
case anyTranscodeFailure
21-
2223
/// The definition of the message or one of its nested messages has required
2324
/// fields but the message being encoded did not include values for them. You
2425
/// must pass `partial: true` during encoding if you wish to explicitly ignore
2526
/// missing required fields.
2627
case missingRequiredFields
27-
2828
/// Messages are limited to a maximum of 2GB in encoded size.
2929
case tooLarge
3030
}

Sources/SwiftProtobuf/JSONEncodingError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
///
1313
// -----------------------------------------------------------------------------
1414

15-
public enum JSONEncodingError: Error {
15+
public enum JSONEncodingError: Error, Equatable {
1616
/// Any fields that were decoded from binary format cannot be
1717
/// re-encoded into JSON unless the object they hold is a
1818
/// well-known type or a type registered with via
1919
/// Google_Protobuf_Any.register()
20-
case anyTranscodeFailure
20+
case anyTypeURLNotRegistered(typeURL: String)
2121
/// Timestamp values can only be JSON encoded if they hold a value
2222
/// between 0001-01-01Z00:00:00 and 9999-12-31Z23:59:59.
2323
case timestampRange

0 commit comments

Comments
 (0)