Skip to content

Add cause to GRPCStatus.descrpition if it is non-nil #1357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sources/GRPC/GRPCStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@ extension GRPCStatus: Equatable {

extension GRPCStatus: CustomStringConvertible {
public var description: String {
if let message = message {
switch (self.message, self.cause) {
case let (.some(message), .some(cause)):
return "\(self.code): \(message), cause: \(cause)"
case let (.some(message), .none):
return "\(self.code): \(message)"
} else {
case let (.none, .some(cause)):
return "\(self.code), cause: \(cause)"
case (.none, .none):
return "\(self.code)"
}
}
Expand Down
32 changes: 31 additions & 1 deletion Tests/GRPCTests/GRPCStatusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GRPCStatusTests: GRPCTestCase {
)
}

func testStatusDescriptionWithMessage() {
func testStatusDescriptionWithWithMessageWithoutCause() {
XCTAssertEqual(
"ok (0): OK",
String(describing: GRPCStatus(code: .ok, message: "OK"))
Expand All @@ -51,6 +51,36 @@ class GRPCStatusTests: GRPCTestCase {
)
}

func testStatusDescriptionWithMessageWithCause() {
struct UnderlyingError: Error, CustomStringConvertible {
var description: String { "underlying error description" }
}
let cause = UnderlyingError()
XCTAssertEqual(
"internal error (13): unknown error processing request, cause: \(cause.description)",
String(describing: GRPCStatus(
code: .internalError,
message: "unknown error processing request",
cause: cause
))
)
}

func testStatusDescriptionWithoutMessageWithCause() {
struct UnderlyingError: Error, CustomStringConvertible {
var description: String { "underlying error description" }
}
let cause = UnderlyingError()
XCTAssertEqual(
"internal error (13), cause: \(cause.description)",
String(describing: GRPCStatus(
code: .internalError,
message: nil,
cause: cause
))
)
}

func testCoWSemanticsModifyingMessage() {
let nilStorageID = GRPCStatus.ok.testingOnly_storageObjectIdentifier
var status = GRPCStatus(code: .resourceExhausted)
Expand Down