Skip to content

Commit 4c9fc36

Browse files
Add cause to GRPCStatus.descrpition if it is non-nil (#1357)
1 parent 7dc737b commit 4c9fc36

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Sources/GRPC/GRPCStatus.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,14 @@ extension GRPCStatus: Equatable {
124124

125125
extension GRPCStatus: CustomStringConvertible {
126126
public var description: String {
127-
if let message = message {
127+
switch (self.message, self.cause) {
128+
case let (.some(message), .some(cause)):
129+
return "\(self.code): \(message), cause: \(cause)"
130+
case let (.some(message), .none):
128131
return "\(self.code): \(message)"
129-
} else {
132+
case let (.none, .some(cause)):
133+
return "\(self.code), cause: \(cause)"
134+
case (.none, .none):
130135
return "\(self.code)"
131136
}
132137
}

Tests/GRPCTests/GRPCStatusTests.swift

+31-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class GRPCStatusTests: GRPCTestCase {
3434
)
3535
}
3636

37-
func testStatusDescriptionWithMessage() {
37+
func testStatusDescriptionWithWithMessageWithoutCause() {
3838
XCTAssertEqual(
3939
"ok (0): OK",
4040
String(describing: GRPCStatus(code: .ok, message: "OK"))
@@ -51,6 +51,36 @@ class GRPCStatusTests: GRPCTestCase {
5151
)
5252
}
5353

54+
func testStatusDescriptionWithMessageWithCause() {
55+
struct UnderlyingError: Error, CustomStringConvertible {
56+
var description: String { "underlying error description" }
57+
}
58+
let cause = UnderlyingError()
59+
XCTAssertEqual(
60+
"internal error (13): unknown error processing request, cause: \(cause.description)",
61+
String(describing: GRPCStatus(
62+
code: .internalError,
63+
message: "unknown error processing request",
64+
cause: cause
65+
))
66+
)
67+
}
68+
69+
func testStatusDescriptionWithoutMessageWithCause() {
70+
struct UnderlyingError: Error, CustomStringConvertible {
71+
var description: String { "underlying error description" }
72+
}
73+
let cause = UnderlyingError()
74+
XCTAssertEqual(
75+
"internal error (13), cause: \(cause.description)",
76+
String(describing: GRPCStatus(
77+
code: .internalError,
78+
message: nil,
79+
cause: cause
80+
))
81+
)
82+
}
83+
5484
func testCoWSemanticsModifyingMessage() {
5585
let nilStorageID = GRPCStatus.ok.testingOnly_storageObjectIdentifier
5686
var status = GRPCStatus(code: .resourceExhausted)

0 commit comments

Comments
 (0)