Skip to content

Commit 3c41198

Browse files
committed
Add new Codes, getters, and documentation
1 parent d017b13 commit 3c41198

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

Sources/SwiftProtobuf/SwiftProtobufError.swift

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ extension SwiftProtobufError {
9797
case textFormatDecodingError
9898
case invalidArgument
9999
case internalError
100+
101+
// These are not domains, but rather specific errors for which we
102+
// want to have associated types, and thus require special treatment.
103+
case anyTypeURLNotRegistered(typeURL: String)
104+
case unknownField(name: String)
100105

101106
var description: String {
102107
switch self {
@@ -116,10 +121,15 @@ extension SwiftProtobufError {
116121
return "An argument provided by the user is invalid"
117122
case .internalError:
118123
return "Other internal error"
124+
case .anyTypeURLNotRegistered(let typeURL):
125+
return "Type URL not registered: \(typeURL)"
126+
case .unknownField(let name):
127+
return "Unknown field: \(name)"
119128
}
120129
}
121130
}
122131

132+
/// This Code's description.
123133
public var description: String {
124134
String(describing: self.code)
125135
}
@@ -129,37 +139,101 @@ extension SwiftProtobufError {
129139
self.code = code
130140
}
131141

142+
/// Errors arising from encoding protobufs into binary data.
132143
public static var binaryEncodingError: Self {
133144
Self(.binaryEncodingError)
134145
}
135146

147+
/// Errors arising from binary decoding of data into protobufs.
136148
public static var binaryDecodingError: Self {
137149
Self(.binaryDecodingError)
138150
}
139151

152+
/// Errors arising from decoding streams of binary messages. These errors have to do with the framing
153+
/// of the messages in the stream, or the stream as a whole.
140154
public static var binaryStreamDecodingError: Self {
141155
Self(.binaryStreamDecodingError)
142156
}
143157

158+
/// Errors arising from encoding protobufs into JSON.
144159
public static var jsonEncodingError: Self {
145160
Self(.jsonEncodingError)
146161
}
147162

163+
/// Errors arising from JSON decoding of data into protobufs.
148164
public static var jsonDecodingError: Self {
149165
Self(.jsonDecodingError)
150166
}
151167

168+
/// Errors arising from text format decoding of data into protobufs.
152169
public static var textFormatDecodingError: Self {
153170
Self(.textFormatDecodingError)
154171
}
155172

173+
/// Errors arising from an invalid argument being passed by the caller.
156174
public static var invalidArgument: Self {
157175
Self(.invalidArgument)
158176
}
159177

178+
/// Errors arising from some invalid internal state.
160179
public static var internalError: Self {
161180
Self(.internalError)
162181
}
182+
183+
/// `Any` fields that were decoded from JSON cannot be re-encoded to binary
184+
/// unless the object they hold is a well-known type or a type registered via
185+
/// `Google_Protobuf_Any.register()`.
186+
/// This Code refers to errors that arise from this scenario.
187+
///
188+
/// - Parameter typeURL: The URL for the unregistered type.
189+
/// - Returns: A `SwiftProtobufError.Code`.
190+
public static func anyTypeURLNotRegistered(typeURL: String) -> Self {
191+
Self(.anyTypeURLNotRegistered(typeURL: typeURL))
192+
}
193+
194+
/// Errors arising from decoding JSON objects and encountering an unknown field.
195+
///
196+
/// - Parameter name: The name of the encountered unknown field.
197+
/// - Returns: A `SwiftProtobufError.Code`.
198+
public static func unknownField(name: String) -> Self {
199+
Self(.unknownField(name: name))
200+
}
201+
202+
/// The unregistered type URL that caused the error, if any is associated with this `Code`.
203+
public var unregisteredTypeURL: String? {
204+
switch self.code {
205+
case .anyTypeURLNotRegistered(let typeURL):
206+
return typeURL
207+
case .binaryEncodingError,
208+
.binaryDecodingError,
209+
.binaryStreamDecodingError,
210+
.jsonEncodingError,
211+
.jsonDecodingError,
212+
.textFormatDecodingError,
213+
.invalidArgument,
214+
.internalError,
215+
.unknownField:
216+
return nil
217+
}
218+
}
219+
220+
/// The unknown field name that caused the error, if any is associated with this `Code`.
221+
public var unknownFieldName: String? {
222+
switch self.code {
223+
case .unknownField(let name):
224+
return name
225+
case .binaryEncodingError,
226+
.binaryDecodingError,
227+
.binaryStreamDecodingError,
228+
.jsonEncodingError,
229+
.jsonDecodingError,
230+
.textFormatDecodingError,
231+
.invalidArgument,
232+
.internalError,
233+
.anyTypeURLNotRegistered:
234+
return nil
235+
}
236+
}
163237
}
164238

165239
/// A location within source code.
@@ -250,7 +324,7 @@ extension SwiftProtobufError {
250324
line: Int = #line
251325
) -> SwiftProtobufError {
252326
SwiftProtobufError(
253-
code: .binaryEncodingError,
327+
code: .anyTypeURLNotRegistered(typeURL: typeURL),
254328
message: """
255329
Any fields that were decoded from JSON format cannot be re-encoded to binary \
256330
unless the object they hold is a well-known type or a type registered via \
@@ -582,7 +656,7 @@ extension SwiftProtobufError {
582656
line: Int = #line
583657
) -> SwiftProtobufError {
584658
SwiftProtobufError(
585-
code: .jsonEncodingError,
659+
code: .anyTypeURLNotRegistered(typeURL: typeURL),
586660
message: """
587661
Any fields that were decoded from binary format cannot be re-encoded into JSON \
588662
unless the object they hold is a well-known type or a type registered via \
@@ -867,7 +941,7 @@ extension SwiftProtobufError {
867941
line: Int = #line
868942
) -> SwiftProtobufError {
869943
SwiftProtobufError(
870-
code: .jsonDecodingError,
944+
code: .unknownField(name: name),
871945
message: "Encountered an unknown field with name '\(name)'.",
872946
location: SourceLocation(function: function, file: file, line: line)
873947
)

Tests/SwiftProtobufTests/Test_JSONDecodingOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ final class Test_JSONDecodingOptions: XCTestCase {
137137
let _ = try SwiftProtoTesting_TestEmptyMessage(jsonString: jsonInput)
138138
XCTFail("Input \(i): Should not have gotten here! Input: \(jsonInput)")
139139
} catch let error as SwiftProtobufError {
140-
XCTAssertEqual(error.code, .jsonDecodingError)
140+
XCTAssertEqual(error.code, .unknownField(name: "unknown"))
141141
XCTAssertEqual(error.message, "Encountered an unknown field with name 'unknown'.")
142142
} catch let e {
143143
XCTFail("Input \(i): Error \(e) decoding into an empty message \(jsonInput)")

0 commit comments

Comments
 (0)