Skip to content

Commit 07b266c

Browse files
committed
added github doc - grpc exception handling
1 parent 912941e commit 07b266c

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

docs/en/server/exception-handling.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ This section describes how you can handle exceptions inside GrpcService layer wi
66

77
## Table of Contents <!-- omit in toc -->
88

9-
- [A Word of Warning](#a-word-of-warning)
10-
- [grpcRequest Scope](#grpcrequest-scope)
9+
- [Proper exception handling](#proper-exception-handling)
10+
- [Detailed explanation](#detailed-explanation)
11+
- [Priority of mapped exceptions](#priority-of-mapped-exceptions)
12+
- [Sending Metadata in response](#sending-metadata-in-response)
13+
- [Overview of returnable types](#overview-of-returnable-types)
1114

1215
## Additional Topics <!-- omit in toc -->
1316

@@ -18,9 +21,78 @@ This section describes how you can handle exceptions inside GrpcService layer wi
1821
- [Testing the Service](testing.md)
1922
- [Security](security.md)
2023

21-
## TODO ...
24+
## Proper exception handling
25+
If you are already familiar with springs [error handling](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling),
26+
you should see some similarity with intended exception handling for gRPC.
2227

23-
> TODO ...
28+
29+
_An explanation for the following class:_
30+
```java
31+
@GrpcAdvice
32+
public class GrpcExceptionAdvice {
33+
34+
35+
@GrpcExceptionHandler
36+
public Status handleIllegalArgumentException(IllegalArgumentException e) {
37+
return Status.INVALID_ARGUMENT.withDescription(e.getMessage());
38+
}
39+
40+
@GrpcExceptionHandler(ResourceNotFoundException.class)
41+
public StatusException handleResourceNotFoundException(ResourceNotFoundException e) {
42+
Status status = Status.NOT_FOUND.withDescription(e.getMessage());
43+
Metadata metadata = ...
44+
return status.asException(metadata);
45+
}
46+
47+
}
48+
```
49+
50+
- `@GrpcAdvice` marks a class to be picked up for exception handling
51+
- `@GrpcExceptionHandler` maps given method to be executed, in case of _specified_ thrown exception
52+
- f.e. if your application throws `IllegalArgumentException`, method `handleIllegalArgumentException(..)` is executed
53+
- `io.grpc.Status` is specified and returned response status
54+
55+
> **Note:** Cause is not transmitted from server to client - as stated in [official docs](https://grpc.github.io/grpc-java/javadoc/io/grpc/Status.html#withCause-java.lang.Throwable-)
56+
57+
## Detailed explanation
58+
59+
### Priority of mapped exceptions
60+
Given method with specified Exception in Annotation *and* as method argument
61+
62+
```java
63+
@GrpcExceptionHandler(ResourceNotFoundException.class)
64+
public StatusException handleResourceNotFoundException(ResourceNotFoundException e) {
65+
// your exception handling
66+
}
67+
```
68+
> **Note:** Exception type in annotation is prioritized in mapping the exception over listed method argument
69+
> **and** they _must_ match the types declared with this value.
70+
71+
_(Matching means: Exception type in annotation is superclass of listed method parameter)_
72+
73+
If no annotation type is provided in the annotation, listed method parameter are being picked up.
74+
75+
### Sending Metadata in response
76+
In case you want to send metadata in your exception response, let's have a look at the following example.
77+
78+
```java
79+
@GrpcExceptionHandler
80+
public StatusRuntimeException handleResourceNotFoundException(IllegalArgumentException e) {
81+
Status status = Status.INVALID_ARGUMENT.withDescription(e.getMessage()).withCause(e);
82+
Metadata metadata = ...
83+
return status.asRuntimeException(metadata);
84+
}
85+
```
86+
87+
As you do not need `Metadata` in your response, just return your specified `Status`.
88+
89+
### Overview of returnable types
90+
Here is a small overview of possible mapped return types with `@GrpcExceptionHandler` and if
91+
custom Metadata can be returned.
92+
93+
| return type | Status | StatusException | StatusRuntimeException | Throwable |
94+
|-----------------|---------|----------|----------|---------|
95+
| custom Metadata | &cross; | &#10004; | &#10004; | &cross; |
2496

2597
## Additional Topics <!-- omit in toc -->
2698

0 commit comments

Comments
 (0)