Skip to content

Commit

Permalink
Make anyTranscodeFailure more descriptive
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rebello95 authored and thomasvl committed Jan 3, 2024
1 parent 71f3b3b commit a266c55
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/SwiftProtobuf/AnyMessageStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ internal class AnyMessageStorage {
case .contentJSON(let contentJSON, let options):
// contentJSON requires we have the type available for decoding
guard let messageType = Google_Protobuf_Any.messageType(forTypeURL: _typeURL) else {
throw BinaryEncodingError.anyTranscodeFailure
throw BinaryEncodingError.anyTypeURLNotRegistered(typeURL: _typeURL)
}
do {
// Decodes the full JSON and then discard the result.
Expand Down Expand Up @@ -414,7 +414,7 @@ extension AnyMessageStorage {
// binary value, so we're stuck. (The Google spec does not
// provide a way to just package the binary value for someone
// else to decode later.)
throw JSONEncodingError.anyTranscodeFailure
throw JSONEncodingError.anyTypeURLNotRegistered(typeURL: _typeURL)
}
let m = try messageType.init(serializedBytes: valueData, partial: true)
return try serializeAnyJSON(for: m, typeURL: _typeURL, options: options)
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftProtobuf/BinaryEncodingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
// -----------------------------------------------------------------------------

/// Describes errors that can occur when decoding a message from binary format.
public enum BinaryEncodingError: Error {
public enum BinaryEncodingError: Error, Equatable {
/// `Any` fields that were decoded from JSON cannot be re-encoded to binary
/// unless the object they hold is a well-known type or a type registered via
/// `Google_Protobuf_Any.register()`.
case anyTypeURLNotRegistered(typeURL: String)
/// An unexpected failure when deserializing a `Google_Protobuf_Any`.
case anyTranscodeFailure

/// The definition of the message or one of its nested messages has required
/// fields but the message being encoded did not include values for them. You
/// must pass `partial: true` during encoding if you wish to explicitly ignore
/// missing required fields.
case missingRequiredFields

/// Messages are limited to a maximum of 2GB in encoded size.
case tooLarge
}
4 changes: 2 additions & 2 deletions Sources/SwiftProtobuf/JSONEncodingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
///
// -----------------------------------------------------------------------------

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

0 comments on commit a266c55

Please sign in to comment.