|
| 1 | +# Errors |
| 2 | + |
| 3 | +Learn about the different error mechanisms in gRPC and how to use them. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +gRPC has a well defined error model for RPCs and a common extension to provide |
| 8 | +richer errors when using Protocol Buffers. This article explains both mechanisms |
| 9 | +and offers advice on using and handling RPC errors for service authors and |
| 10 | +clients. |
| 11 | + |
| 12 | +### Error models |
| 13 | + |
| 14 | +gRPC has two widely used error models: |
| 15 | + |
| 16 | +1. A 'standard' error model supported by all client/server gRPC libraries. |
| 17 | +2. A 'rich' error model providing more detailed error information via serialized |
| 18 | + Protocol Buffers messages. |
| 19 | + |
| 20 | +#### Standard error model |
| 21 | + |
| 22 | +In gRPC the outcome of every RPC is represented by a status made up of a code |
| 23 | +and a message. The status is propagated from the server to the client in the |
| 24 | +metadata as the final part of an RPC indicating the outcome of the RPC. |
| 25 | + |
| 26 | +You can find more information about the error codes in ``RPCError/Code`` and in |
| 27 | +the status codes guide on the |
| 28 | +[gRPC website](https://grpc.io/docs/guides/status-codes/). |
| 29 | + |
| 30 | +This mechanism is part of the gRPC protocol is supported by all client/server |
| 31 | +gRPC libraries regardless of the data format (e.g. Protocol Buffers) being used |
| 32 | +for messages. |
| 33 | + |
| 34 | +#### Rich error model |
| 35 | + |
| 36 | +The standard error model is quite limited and doesn't include the ability to |
| 37 | +communicate details about the error. If you're using the Protocol Buffers data |
| 38 | +format for messages then you may wish to use the "rich" error model. |
| 39 | + |
| 40 | +The model was developed and used by Google and is described in more detail |
| 41 | +in the [gRPC error guide](https://grpc.io/docs/guides/error/) and |
| 42 | +[Google AIP-193](https://google.aip.dev/193). |
| 43 | + |
| 44 | +While not officially part of gRPC it's a widely used convention with support in |
| 45 | +various client/server gRPC libraries, including gRPC Swift. |
| 46 | + |
| 47 | +It specifies a standard set of error message types covering the most common |
| 48 | +situations. The error details are encoded as protobuf messages in the trailing |
| 49 | +metadata of an RPC. Clients are able to deserialize and access the details as |
| 50 | +type-safe structured messages should they need to. |
| 51 | + |
| 52 | +### User guide |
| 53 | + |
| 54 | +Learn how to use both models in gRPC Swift. |
| 55 | + |
| 56 | +#### Service authors |
| 57 | + |
| 58 | +Errors thrown from an RPC handler are caught by the gRPC runtime and turned into |
| 59 | +a status. You have a two options to ensure that an appropriate status is sent to |
| 60 | +the client if your RPC handler throws an error: |
| 61 | + |
| 62 | +1. Throw an ``RPCError`` which explicitly sets the desired status code and |
| 63 | + message. |
| 64 | +2. Throw an error conforming to ``RPCErrorConvertible`` which the gRPC runtime |
| 65 | + will use to create an ``RPCError``. |
| 66 | + |
| 67 | +Any errors thrown which don't fall into these categories will result in a status |
| 68 | +code of `unknown` being sent to the client. |
| 69 | + |
| 70 | +Generally speaking expected failure scenarios should be considered as part of |
| 71 | +the API contract and each RPC should be documented accordingly. |
| 72 | + |
| 73 | +#### Clients |
| 74 | + |
| 75 | +Clients should catch ``RPCError`` if they are interested in the failures from an |
| 76 | +RPC. This is a manifestation of the error sent by the server but in some cases |
| 77 | +it may be synthesized locally. |
| 78 | + |
| 79 | +For clients using the rich error model, the ``RPCError`` can be caught and a |
| 80 | +detailed error can be extracted from it using `unpackGoogleRPCStatus()`. |
| 81 | + |
| 82 | +See [`error-details`](https://github.com/grpc/grpc-swift/tree/main/Examples) for |
| 83 | +an examples. |
0 commit comments