Skip to content

Commit

Permalink
Move all errors to extendable struct-based SwiftProtobufError
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo committed Apr 5, 2024
1 parent 41492ea commit 33cf83a
Show file tree
Hide file tree
Showing 44 changed files with 1,175 additions and 451 deletions.
16 changes: 8 additions & 8 deletions Sources/SwiftProtobuf/AnyMessageStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ fileprivate func unpack(contentJSON: [UInt8],
}
if !options.ignoreUnknownFields {
// The only thing within a WKT should be "value".
throw AnyUnpackError.malformedWellKnownTypeJSON
throw SwiftProtobufError.AnyUnpack.malformedWellKnownTypeJSON
}
let _ = try scanner.skip()
try scanner.skipRequiredComma()
}
if !options.ignoreUnknownFields && !scanner.complete {
// If that wasn't the end, then there was another key, and WKTs should
// only have the one when not skipping unknowns.
throw AnyUnpackError.malformedWellKnownTypeJSON
throw SwiftProtobufError.AnyUnpack.malformedWellKnownTypeJSON
}
}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ internal class AnyMessageStorage {
options: BinaryDecodingOptions
) throws {
guard isA(M.self) else {
throw AnyUnpackError.typeMismatch
throw SwiftProtobufError.AnyUnpack.typeMismatch
}

switch state {
Expand Down Expand Up @@ -218,7 +218,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.anyTypeURLNotRegistered(typeURL: _typeURL)
throw SwiftProtobufError.BinaryEncoding.anyTypeURLNotRegistered(typeURL: _typeURL)
}
do {
// Decodes the full JSON and then discard the result.
Expand All @@ -230,7 +230,7 @@ internal class AnyMessageStorage {
options: options,
as: messageType)
} catch {
throw BinaryEncodingError.anyTranscodeFailure
throw SwiftProtobufError.BinaryEncoding.anyTypeURLNotRegistered(typeURL: _typeURL)
}
}
}
Expand All @@ -243,7 +243,7 @@ extension AnyMessageStorage {
_typeURL = url
guard let messageType = Google_Protobuf_Any.messageType(forTypeURL: url) else {
// The type wasn't registered, can't parse it.
throw TextFormatDecodingError.malformedText
throw SwiftProtobufError.TextFormatDecoding.malformedText
}
let terminator = try decoder.scanner.skipObjectStart()
var subDecoder = try TextFormatDecoder(messageType: messageType, scanner: decoder.scanner, terminator: terminator)
Expand All @@ -259,7 +259,7 @@ extension AnyMessageStorage {
decoder.scanner = subDecoder.scanner
if try decoder.nextFieldNumber() != nil {
// Verbose any can never have additional keys.
throw TextFormatDecodingError.malformedText
throw SwiftProtobufError.TextFormatDecoding.malformedText
}
}

Expand Down Expand Up @@ -422,7 +422,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.anyTypeURLNotRegistered(typeURL: _typeURL)
throw SwiftProtobufError.JSONEncoding.anyTypeURLNotRegistered(typeURL: _typeURL)
}
let m = try messageType.init(serializedBytes: valueData, partial: true)
return try serializeAnyJSON(for: m, typeURL: _typeURL, options: options)
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftProtobuf/AnyUnpackError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/// message. At this time, any error can occur that might have occurred from a
/// regular decoding operation. There are also other errors that can occur due
/// to problems with the `Any` value's structure.
@available(*, deprecated, message: "This error type has been deprecated and won't be thrown anymore; it has been replaced by `SwiftProtobufError`.")
public enum AnyUnpackError: Error {
/// The `type_url` field in the `Google_Protobuf_Any` message did not match
/// the message type provided to the `unpack()` method.
Expand Down
34 changes: 16 additions & 18 deletions Sources/SwiftProtobuf/AsyncMessageSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ extension AsyncSequence where Element == UInt8 {
///
/// - Parameters:
/// - messageType: The type of message to read.
/// - extensions: An `ExtensionMap` used to look up and decode any extensions in
/// - extensions: An ``ExtensionMap`` used to look up and decode any extensions in
/// messages encoded by this sequence, or in messages nested within these messages.
/// - partial: If `false` (the default), after decoding a message, `Message.isInitialized`
/// - partial: If `false` (the default), after decoding a message, ``Message/isInitialized-6abgi`
/// will be checked to ensure all fields are present. If any are missing,
/// `BinaryDecodingError.missingRequiredFields` will be thrown.
/// - options: The BinaryDecodingOptions to use.
/// ``SwiftProtobufError/BinaryDecoding/missingRequiredFields`` will be thrown.
/// - options: The ``BinaryDecodingOptions`` to use.
/// - Returns: An asynchronous sequence of messages read from the `AsyncSequence` of bytes.
/// - Throws: `BinaryDelimited.Error` for errors in the framing of the messages
/// in the sequence, `BinaryDecodingError` for errors while decoding
/// messages.
/// - Throws: ``SwiftProtobufError`` for errors in the framing of the messages in the sequence
/// or while decoding messages.
@inlinable
public func binaryProtobufDelimitedMessages<M: Message>(
of messageType: M.Type = M.self,
Expand Down Expand Up @@ -68,16 +67,15 @@ public struct AsyncMessageSequence<
///
/// - Parameters:
/// - baseSequence: The `AsyncSequence` to read messages from.
/// - extensions: An `ExtensionMap` used to look up and decode any extensions in
/// - extensions: An ``ExtensionMap`` used to look up and decode any extensions in
/// messages encoded by this sequence, or in messages nested within these messages.
/// - partial: If `false` (the default), after decoding a message, `Message.isInitialized`
/// - partial: If `false` (the default), after decoding a message, ``Message/isInitialized-6abgi``
/// will be checked to ensure all fields are present. If any are missing,
/// `BinaryDecodingError.missingRequiredFields` will be thrown.
/// - options: The BinaryDecodingOptions to use.
/// ``SwiftProtobufError/BinaryDecoding/missingRequiredFields`` will be thrown.
/// - options: The ``BinaryDecodingOptions`` to use.
/// - Returns: An asynchronous sequence of messages read from the `AsyncSequence` of bytes.
/// - Throws: `BinaryDelimited.Error` for errors in the framing of the messages
/// in the sequence, `BinaryDecodingError` for errors while decoding
/// messages.
/// - Throws: ``SwiftProtobufError`` for errors in the framing of the messages in the sequence
/// or while decoding messages.
public init(
base: Base,
extensions: (any ExtensionMap)? = nil,
Expand Down Expand Up @@ -124,7 +122,7 @@ public struct AsyncMessageSequence<
shift += UInt64(7)
if shift > 35 {
iterator = nil
throw BinaryDelimited.Error.malformedLength
throw SwiftProtobufError.BinaryDecoding.malformedLength
}
if (byte & 0x80 == 0) {
return messageSize
Expand All @@ -133,7 +131,7 @@ public struct AsyncMessageSequence<
if (shift > 0) {
// The stream has ended inside a varint.
iterator = nil
throw BinaryDelimited.Error.truncated
throw SwiftProtobufError.BinaryDecoding.truncated
}
return nil // End of stream reached.
}
Expand All @@ -155,7 +153,7 @@ public struct AsyncMessageSequence<
guard let byte = try await iterator?.next() else {
// The iterator hit the end, but the chunk wasn't filled, so the full
// payload wasn't read.
throw BinaryDelimited.Error.truncated
throw SwiftProtobufError.BinaryDecoding.truncated
}
chunk[consumedBytes] = byte
consumedBytes += 1
Expand Down Expand Up @@ -183,7 +181,7 @@ public struct AsyncMessageSequence<
}
guard messageSize <= UInt64(0x7fffffff) else {
iterator = nil
throw BinaryDecodingError.tooLarge
throw SwiftProtobufError.BinaryDecoding.tooLarge
}
if messageSize == 0 {
return try M(
Expand Down
Loading

0 comments on commit 33cf83a

Please sign in to comment.