|
| 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