Skip to content

Commit 8c519b1

Browse files
committed
Remove breaking errors
1 parent 0b656a7 commit 8c519b1

File tree

7 files changed

+17
-118
lines changed

7 files changed

+17
-118
lines changed

Sources/SwiftProtobuf/AnyMessageStorage.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ internal class AnyMessageStorage {
218218
case .contentJSON(let contentJSON, let options):
219219
// contentJSON requires we have the type available for decoding
220220
guard let messageType = Google_Protobuf_Any.messageType(forTypeURL: _typeURL) else {
221-
throw SwiftProtobufError.BinaryEncoding.anyTypeURLNotRegistered(typeURL: _typeURL)
221+
throw BinaryEncodingError.anyTranscodeFailure
222222
}
223223
do {
224224
// Decodes the full JSON and then discard the result.
@@ -230,7 +230,7 @@ internal class AnyMessageStorage {
230230
options: options,
231231
as: messageType)
232232
} catch {
233-
throw SwiftProtobufError.BinaryEncoding.anyTypeURLNotRegistered(typeURL: _typeURL)
233+
throw BinaryEncodingError.anyTranscodeFailure
234234
}
235235
}
236236
}
@@ -422,7 +422,7 @@ extension AnyMessageStorage {
422422
// binary value, so we're stuck. (The Google spec does not
423423
// provide a way to just package the binary value for someone
424424
// else to decode later.)
425-
throw SwiftProtobufError.JSONEncoding.anyTypeURLNotRegistered(typeURL: _typeURL)
425+
throw JSONEncodingError.anyTranscodeFailure
426426
}
427427
let m = try messageType.init(serializedBytes: valueData, partial: true)
428428
return try serializeAnyJSON(for: m, typeURL: _typeURL, options: options)

Sources/SwiftProtobuf/BinaryDecoder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,8 @@ internal struct BinaryDecoder: Decoder {
14241424
// that is length delimited on the wire, so the spec would imply
14251425
// the limit still applies.
14261426
guard length < 0x7fffffff else {
1427-
throw SwiftProtobufError.BinaryDecoding.tooLarge()
1427+
// Reuse existing error to avoid breaking change of changing thrown error
1428+
throw BinaryDecodingError.malformedProtobuf
14281429
}
14291430

14301431
guard length <= UInt64(available) else {

Sources/SwiftProtobuf/BinaryDelimited.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public enum BinaryDelimited {
158158
return
159159
}
160160
guard unsignedLength <= 0x7fffffff else {
161-
throw SwiftProtobufError.BinaryStreamDecoding.tooLarge()
161+
// Adding a new case is a breaking change, reuse malformedProtobuf.
162+
throw BinaryDecodingError.malformedProtobuf
162163
}
163164
let length = Int(unsignedLength)
164165

@@ -248,7 +249,7 @@ internal func decodeVarint(_ stream: InputStream) throws -> UInt64 {
248249
}
249250
shift += 7
250251
if shift > 63 {
251-
throw SwiftProtobufError.BinaryStreamDecoding.malformedLength()
252+
throw BinaryDecodingError.malformedProtobuf
252253
}
253254
}
254255
}

Sources/SwiftProtobuf/Message+BinaryAdditions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ extension Message {
4848
// the places that encode message fields (or strings/bytes fields), keeping
4949
// the overhead of the check to a minimum.
5050
guard requiredSize < 0x7fffffff else {
51-
throw SwiftProtobufError.BinaryEncoding.tooLarge()
51+
// Adding a new error is a breaking change.
52+
throw BinaryEncodingError.missingRequiredFields
5253
}
5354

5455
var data = Bytes(repeating: 0, count: requiredSize)

Sources/SwiftProtobuf/SwiftProtobufError.swift

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,15 @@ extension SwiftProtobufError {
8989
/// A high level indication of the kind of error being thrown.
9090
public struct Code: Hashable, Sendable, CustomStringConvertible {
9191
private enum Wrapped: Hashable, Sendable, CustomStringConvertible {
92-
case binaryEncodingError
9392
case binaryDecodingError
9493
case binaryStreamDecodingError
95-
case jsonEncodingError
96-
97-
// These are not domains, but rather specific errors for which we
98-
// want to have associated types, and thus require special treatment.
99-
case anyTypeURLNotRegistered(typeURL: String)
10094

10195
var description: String {
10296
switch self {
103-
case .binaryEncodingError:
104-
return "Binary encoding error"
10597
case .binaryDecodingError:
10698
return "Binary decoding error"
10799
case .binaryStreamDecodingError:
108100
return "Stream decoding error"
109-
case .jsonEncodingError:
110-
return "JSON encoding error"
111-
case .anyTypeURLNotRegistered(let typeURL):
112-
return "Type URL not registered: \(typeURL)"
113101
}
114102
}
115103
}
@@ -123,11 +111,6 @@ extension SwiftProtobufError {
123111
private init(_ code: Wrapped) {
124112
self.code = code
125113
}
126-
127-
/// Errors arising from encoding protobufs into binary data.
128-
public static var binaryEncodingError: Self {
129-
Self(.binaryEncodingError)
130-
}
131114

132115
/// Errors arising from binary decoding of data into protobufs.
133116
public static var binaryDecodingError: Self {
@@ -139,35 +122,6 @@ extension SwiftProtobufError {
139122
public static var binaryStreamDecodingError: Self {
140123
Self(.binaryStreamDecodingError)
141124
}
142-
143-
/// Errors arising from encoding protobufs into JSON.
144-
public static var jsonEncodingError: Self {
145-
Self(.jsonEncodingError)
146-
}
147-
148-
/// `Any` fields that were decoded from JSON cannot be re-encoded to binary
149-
/// unless the object they hold is a well-known type or a type registered via
150-
/// `Google_Protobuf_Any.register()`.
151-
/// This Code refers to errors that arise from this scenario.
152-
///
153-
/// - Parameter typeURL: The URL for the unregistered type.
154-
/// - Returns: A `SwiftProtobufError.Code`.
155-
public static func anyTypeURLNotRegistered(typeURL: String) -> Self {
156-
Self(.anyTypeURLNotRegistered(typeURL: typeURL))
157-
}
158-
159-
/// The unregistered type URL that caused the error, if any is associated with this `Code`.
160-
public var unregisteredTypeURL: String? {
161-
switch self.code {
162-
case .anyTypeURLNotRegistered(let typeURL):
163-
return typeURL
164-
case .binaryEncodingError,
165-
.binaryDecodingError,
166-
.binaryStreamDecodingError,
167-
.jsonEncodingError:
168-
return nil
169-
}
170-
}
171125
}
172126

173127
/// A location within source code.
@@ -213,42 +167,6 @@ extension SwiftProtobufError: CustomDebugStringConvertible {
213167
// - MARK: Common errors
214168

215169
extension SwiftProtobufError {
216-
/// Errors arising from encoding protobufs into binary data.
217-
public enum BinaryEncoding {
218-
/// Messages are limited to a maximum of 2GB in encoded size.
219-
public static func tooLarge(
220-
function: String = #function,
221-
file: String = #fileID,
222-
line: Int = #line
223-
) -> SwiftProtobufError {
224-
SwiftProtobufError(
225-
code: .binaryEncodingError,
226-
message: "Messages are limited to a maximum of 2GB in encoded size.",
227-
location: SourceLocation(function: function, file: file, line: line)
228-
)
229-
}
230-
231-
/// `Any` fields that were decoded from JSON cannot be re-encoded to binary
232-
/// unless the object they hold is a well-known type or a type registered via
233-
/// `Google_Protobuf_Any.register()`.
234-
public static func anyTypeURLNotRegistered(
235-
typeURL: String,
236-
function: String = #function,
237-
file: String = #fileID,
238-
line: Int = #line
239-
) -> SwiftProtobufError {
240-
SwiftProtobufError(
241-
code: .anyTypeURLNotRegistered(typeURL: typeURL),
242-
message: """
243-
Any fields that were decoded from JSON format cannot be re-encoded to binary \
244-
unless the object they hold is a well-known type or a type registered via \
245-
`Google_Protobuf_Any.register()`. Type URL is \(typeURL).
246-
""",
247-
location: SourceLocation(function: function, file: file, line: line)
248-
)
249-
}
250-
}
251-
252170
/// Errors arising from binary decoding of data into protobufs.
253171
public enum BinaryDecoding {
254172
/// Message is too large. Bytes and Strings have a max size of 2GB.
@@ -320,26 +238,4 @@ extension SwiftProtobufError {
320238
)
321239
}
322240
}
323-
324-
/// Errors arising from encoding protobufs into JSON.
325-
public enum JSONEncoding {
326-
/// Any fields that were decoded from binary format cannot be re-encoded into JSON unless the
327-
/// object they hold is a well-known type or a type registered via `Google_Protobuf_Any.register()`.
328-
public static func anyTypeURLNotRegistered(
329-
typeURL: String,
330-
function: String = #function,
331-
file: String = #fileID,
332-
line: Int = #line
333-
) -> SwiftProtobufError {
334-
SwiftProtobufError(
335-
code: .anyTypeURLNotRegistered(typeURL: typeURL),
336-
message: """
337-
Any fields that were decoded from binary format cannot be re-encoded into JSON \
338-
unless the object they hold is a well-known type or a type registered via \
339-
`Google_Protobuf_Any.register()`. Type URL is \(typeURL).
340-
""",
341-
location: SourceLocation(function: function, file: file, line: line)
342-
)
343-
}
344-
}
345241
}

Tests/SwiftProtobufTests/Test_AllTypes.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ final class Test_AllTypes: XCTestCase, PBTestHelpers {
824824
// Don't need all the bytes, want some to let the length issue trigger.
825825
0x01, 0x02, 0x03,
826826
])) {
827-
XCTAssertTrue(self.isSwiftProtobufErrorEqual($0 as! SwiftProtobufError, .BinaryDecoding.tooLarge()))
827+
XCTAssertEqual($0 as! BinaryDecodingError, .malformedProtobuf)
828828
}
829829

830830
let empty = MessageTestType()
@@ -944,11 +944,11 @@ final class Test_AllTypes: XCTestCase, PBTestHelpers {
944944

945945
// Ensure bytes over 2GB fail to decode according to spec.
946946
XCTAssertThrowsError(try MessageTestType(serializedBytes: [
947-
122, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
947+
122, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
948948
// Don't need all the bytes, want some to let the length issue trigger.
949949
0x01, 0x02, 0x03,
950950
])) {
951-
XCTAssertTrue(self.isSwiftProtobufErrorEqual($0 as! SwiftProtobufError, .BinaryDecoding.tooLarge()))
951+
XCTAssertEqual($0 as! BinaryDecodingError, .malformedProtobuf)
952952
}
953953

954954
let empty = MessageTestType()
@@ -1000,7 +1000,7 @@ final class Test_AllTypes: XCTestCase, PBTestHelpers {
10001000
// Don't need all the bytes, want some to let the length issue trigger.
10011001
0x01, 0x02, 0x03,
10021002
])) {
1003-
XCTAssertTrue(self.isSwiftProtobufErrorEqual($0 as! SwiftProtobufError, .BinaryDecoding.tooLarge()))
1003+
XCTAssertEqual($0 as! BinaryDecodingError, .malformedProtobuf)
10041004
}
10051005

10061006
// Ensure storage is uniqued for clear.
@@ -1756,7 +1756,7 @@ final class Test_AllTypes: XCTestCase, PBTestHelpers {
17561756
// Don't need all the bytes, want some to let the length issue trigger.
17571757
0x01, 0x02, 0x03,
17581758
])) {
1759-
XCTAssertTrue(self.isSwiftProtobufErrorEqual($0 as! SwiftProtobufError, .BinaryDecoding.tooLarge()))
1759+
XCTAssertEqual($0 as! BinaryDecodingError, .malformedProtobuf)
17601760
}
17611761

17621762
assertDebugDescription("SwiftProtobufTests.SwiftProtoTesting_TestAllTypes:\nrepeated_nested_message {\n bb: 1\n}\nrepeated_nested_message {\n bb: 2\n}\n") {(o: inout MessageTestType) in

Tests/SwiftProtobufTests/Test_BinaryDelimited.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ final class Test_BinaryDelimited: XCTestCase {
9090

9191
XCTAssertThrowsError(try BinaryDelimited.parse(messageType: SwiftProtoTesting_TestAllTypes.self,
9292
from: istream)) { error in
93-
XCTAssertTrue(self.isSwiftProtobufErrorEqual(error as! SwiftProtobufError, .BinaryStreamDecoding.tooLarge()))
93+
XCTAssertEqual(error as! BinaryDecodingError, .malformedProtobuf)
9494
}
9595
}
9696

@@ -99,7 +99,7 @@ final class Test_BinaryDelimited: XCTestCase {
9999

100100
XCTAssertThrowsError(try BinaryDelimited.parse(messageType: SwiftProtoTesting_TestAllTypes.self,
101101
from: istream)) { error in
102-
XCTAssertTrue(self.isSwiftProtobufErrorEqual(error as! SwiftProtobufError, .BinaryStreamDecoding.malformedLength()))
102+
XCTAssertEqual(error as! BinaryDecodingError, .malformedProtobuf)
103103
}
104104
}
105105

0 commit comments

Comments
 (0)