Skip to content

Commit e431ed6

Browse files
glbrnttgjcairo
andauthored
Add detailed error example and error article (#2145)
Motivation: We should document how service authors and clients should deal with errors and what options are available to them. Modifications: - Add an article explaining what the error mechanisms available - Add an example Result: Better docs --------- Co-authored-by: Gus Cairo <[email protected]>
1 parent 13150bd commit e431ed6

File tree

9 files changed

+740
-0
lines changed

9 files changed

+740
-0
lines changed

Examples/error-details/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// swift-tools-version:6.0
2+
/*
3+
* Copyright 2024, gRPC Authors All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import PackageDescription
19+
20+
let package = Package(
21+
name: "error-details",
22+
platforms: [.macOS(.v15)],
23+
dependencies: [
24+
.package(url: "https://github.com/grpc/grpc-swift.git", branch: "main"),
25+
.package(url: "https://github.com/grpc/grpc-swift-protobuf.git", branch: "main"),
26+
],
27+
targets: [
28+
.executableTarget(
29+
name: "error-details",
30+
dependencies: [
31+
.product(name: "GRPCCore", package: "grpc-swift"),
32+
.product(name: "GRPCInProcessTransport", package: "grpc-swift"),
33+
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
34+
]
35+
)
36+
]
37+
)

Examples/error-details/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Detailed Error
2+
3+
This example demonstrates how to create and unpack detailed errors.
4+
5+
## Overview
6+
7+
A command line tool that demonstrates how a detailed error can be thrown by a
8+
service and unpacked and inspected by a client. The detailed error model is
9+
described in more detailed in the [gRPC Error
10+
Guide](https://grpc.io/docs/guides/error/) and is made available via the
11+
[grpc-swift-protobuf](https://github.com/grpc-swift-protobuf) package.
12+
13+
## Usage
14+
15+
Build and run the example using the CLI:
16+
17+
```console
18+
$ swift run
19+
Error code: resourceExhausted
20+
Error message: The greeter has temporarily run out of greetings.
21+
Error details:
22+
- Localized message (en-GB): Out of enthusiasm. The greeter is having a cup of tea, try again after that.
23+
- Localized message (en-US): Out of enthusiasm. The greeter is taking a coffee break, try again later.
24+
- Help links:
25+
- https://en.wikipedia.org/wiki/Caffeine (A Wikipedia page about caffeine including its properties and effects.)
26+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import GRPCCore
18+
import GRPCInProcessTransport
19+
import GRPCProtobuf
20+
21+
@main
22+
struct DetailedErrorExample {
23+
static func main() async throws {
24+
let inProcess = InProcessTransport()
25+
try await withGRPCServer(transport: inProcess.server, services: [Greeter()]) { server in
26+
try await withGRPCClient(transport: inProcess.client) { client in
27+
try await Self.doRPC(Helloworld_Greeter.Client(wrapping: client))
28+
}
29+
}
30+
}
31+
32+
static func doRPC(_ greeter: Helloworld_Greeter.Client) async throws {
33+
do {
34+
let reply = try await greeter.sayHello(.with { $0.name = "(ignored)" })
35+
print("Unexpected reply: \(reply.message)")
36+
} catch let error as RPCError {
37+
// Unpack the detailed from the standard 'RPCError'.
38+
guard let status = try error.unpackGoogleRPCStatus() else { return }
39+
print("Error code: \(status.code)")
40+
print("Error message: \(status.message)")
41+
print("Error details:")
42+
for detail in status.details {
43+
if let localizedMessage = detail.localizedMessage {
44+
print("- Localized message (\(localizedMessage.locale)): \(localizedMessage.message)")
45+
} else if let help = detail.help {
46+
print("- Help links:")
47+
for link in help.links {
48+
print(" - \(link.url) (\(link.linkDescription))")
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
56+
struct Greeter: Helloworld_Greeter.SimpleServiceProtocol {
57+
func sayHello(
58+
request: Helloworld_HelloRequest,
59+
context: ServerContext
60+
) async throws -> Helloworld_HelloReply {
61+
// Always throw a detailed error.
62+
throw GoogleRPCStatus(
63+
code: .resourceExhausted,
64+
message: "The greeter has temporarily run out of greetings.",
65+
details: [
66+
.localizedMessage(
67+
locale: "en-GB",
68+
message: "Out of enthusiasm. The greeter is having a cup of tea, try again after that."
69+
),
70+
.localizedMessage(
71+
locale: "en-US",
72+
message: "Out of enthusiasm. The greeter is taking a coffee break, try again later."
73+
),
74+
.help(
75+
links: [
76+
ErrorDetails.Help.Link(
77+
url: "https://en.wikipedia.org/wiki/Caffeine",
78+
description: "A Wikipedia page about caffeine including its properties and effects."
79+
)
80+
]
81+
),
82+
]
83+
)
84+
}
85+
}

0 commit comments

Comments
 (0)