Skip to content

Commit 207db41

Browse files
committed
PR changes
1 parent 6c4589b commit 207db41

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

Sources/GRPCCore/RPCError.swift

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,56 @@ public struct RPCError: Sendable, Hashable, Error {
3535
/// The original error which led to this error being thrown.
3636
public var cause: (any Error)?
3737

38-
/// Create a new RPC error.
38+
/// Create a new RPC error. If the given `cause` is also an ``RPCError`` sharing the same `code`,
39+
/// then they will be flattened into a single error, by merging the messages and metadata.
3940
///
4041
/// - Parameters:
4142
/// - code: The status code.
4243
/// - message: A message providing additional context about the code.
4344
/// - metadata: Any metadata to attach to the error.
4445
/// - cause: An underlying error which led to this error being thrown.
45-
/// - flatteningCauses: Whether to flatten the `causes` as long as they're ``RPCError``s
46-
/// sharing the same ``Code-swift.struct``.
4746
public init(
4847
code: Code,
4948
message: String,
5049
metadata: Metadata = [:],
51-
cause: (any Error)? = nil,
52-
flatteningCauses: Bool = false
50+
cause: (any Error)? = nil
5351
) {
54-
if flatteningCauses {
55-
var finalMessage = message
56-
var finalMetadata = metadata
57-
var nextCause = cause
58-
while let nextRPCErrorCause = nextCause as? RPCError {
59-
if code == nextRPCErrorCause.code {
60-
finalMessage = finalMessage + " \(nextRPCErrorCause.message)"
61-
finalMetadata.add(contentsOf: nextRPCErrorCause.metadata)
62-
nextCause = nextRPCErrorCause.cause
63-
} else {
64-
nextCause = RPCError(
65-
code: nextRPCErrorCause.code,
66-
message: nextRPCErrorCause.message,
67-
metadata: nextRPCErrorCause.metadata,
68-
cause: nextRPCErrorCause.cause,
69-
flatteningCauses: true
70-
)
71-
break
72-
}
73-
}
52+
if let rpcErrorCause = cause as? RPCError, rpcErrorCause.code == code {
53+
self.code = code
54+
self.message = message + " \(rpcErrorCause.message)"
55+
var mergedMetadata = metadata
56+
mergedMetadata.add(contentsOf: rpcErrorCause.metadata)
57+
self.metadata = mergedMetadata
58+
self.cause = rpcErrorCause.cause
59+
} else {
60+
self.code = code
61+
self.message = message
62+
self.metadata = metadata
63+
self.cause = cause
64+
}
65+
}
7466

67+
/// Create a new RPC error. If the given `cause` shares the same `code`, then it will be flattened
68+
/// into a single error, by merging the messages and metadata.
69+
///
70+
/// - Parameters:
71+
/// - code: The status code.
72+
/// - message: A message providing additional context about the code.
73+
/// - metadata: Any metadata to attach to the error.
74+
/// - cause: An underlying ``RPCError`` which led to this error being thrown.
75+
public init(
76+
code: Code,
77+
message: String,
78+
metadata: Metadata = [:],
79+
cause: RPCError
80+
) {
81+
if cause.code == code {
7582
self.code = code
76-
self.message = finalMessage
77-
self.metadata = finalMetadata
78-
self.cause = nextCause
83+
self.message = message + " \(cause.message)"
84+
var mergedMetadata = metadata
85+
mergedMetadata.add(contentsOf: cause.metadata)
86+
self.metadata = mergedMetadata
87+
self.cause = cause.cause
7988
} else {
8089
self.code = code
8190
self.message = message

0 commit comments

Comments
 (0)