Skip to content

Commit c89db83

Browse files
committed
Introduce castOrConvertRPCError helper
1 parent b201e42 commit c89db83

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

Sources/GRPCCore/Call/Client/Internal/ClientStreamExecutor.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal enum ClientStreamExecutor {
2626
/// - attempt: The attempt number for the RPC that will be executed.
2727
/// - serializer: A request serializer.
2828
/// - deserializer: A response deserializer.
29-
/// - stream: The stream to excecute the RPC on.
29+
/// - stream: The stream to execute the RPC on.
3030
/// - Returns: A streamed response.
3131
@inlinable
3232
static func execute<Input: Sendable, Output: Sendable, Bytes: GRPCContiguousBytes>(
@@ -95,12 +95,8 @@ internal enum ClientStreamExecutor {
9595
let result = await Result {
9696
try await stream.write(.metadata(request.metadata))
9797
try await request.producer(.map(into: stream) { .message(try serializer.serialize($0)) })
98-
}.castError(to: RPCError.self) { other in
99-
if let convertible = other as? any RPCErrorConvertible {
100-
RPCError(convertible)
101-
} else {
102-
RPCError(code: .unknown, message: "Write failed.", cause: other)
103-
}
98+
}.castOrConvertRPCError { other in
99+
RPCError(code: .unknown, message: "Write failed.", cause: other)
104100
}
105101

106102
switch result {

Sources/GRPCCore/Call/Server/Internal/ServerRPCExecutor.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,8 @@ struct ServerRPCExecutor {
213213
return try await contents.producer(
214214
.serializingToRPCResponsePart(into: outbound, with: serializer)
215215
)
216-
}.castError(to: RPCError.self) { error in
217-
if let convertible = error as? (any RPCErrorConvertible) {
218-
return RPCError(convertible)
219-
} else {
220-
return RPCError(code: .unknown, message: "", cause: error)
221-
}
216+
}.castOrConvertRPCError { error in
217+
RPCError(code: .unknown, message: "", cause: error)
222218
}
223219

224220
switch result {

Sources/GRPCCore/Internal/Result+Catching.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,25 @@ extension Result {
4545
return (error as? NewError) ?? buildError(error)
4646
}
4747
}
48+
49+
/// Attempt to map or convert the error to an `RPCError`.
50+
///
51+
/// If the cast or conversion is not possible then the provided closure is used to create an error of the given type.
52+
///
53+
/// - Parameter buildError: A closure which constructs the desired error if conversion is not possible.
54+
@inlinable
55+
@available(gRPCSwift 2.0, *)
56+
func castOrConvertRPCError(
57+
or buildError: (any Error) -> RPCError
58+
) -> Result<Success, RPCError> {
59+
return self.mapError { error in
60+
if let rpcError = error as? RPCError {
61+
return rpcError
62+
} else if let convertibleError = error as? any RPCErrorConvertible {
63+
return RPCError(convertibleError)
64+
} else {
65+
return buildError(error)
66+
}
67+
}
68+
}
4869
}

0 commit comments

Comments
 (0)