Skip to content

Commit 4e96f26

Browse files
committed
Improve exception handling documentation
1 parent bae60fc commit 4e96f26

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

docs/en/server/exception-handling.md

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,56 +23,61 @@ This section describes how you can handle exceptions inside GrpcService layer wi
2323

2424
## Proper exception handling
2525

26-
If you are already familiar with springs [error handling](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling),
27-
you should see some similarity with intended exception handling for gRPC.
28-
26+
If you are already familiar with spring's [error handling](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling),
27+
you should see some similarities with the exception handling for gRPC.
2928

3029
_An explanation for the following class:_
30+
3131
```java
3232
@GrpcAdvice
3333
public class GrpcExceptionAdvice {
3434

3535

3636
@GrpcExceptionHandler
3737
public Status handleInvalidArgument(IllegalArgumentException e) {
38-
return Status.INVALID_ARGUMENT.withDescription("Your description");
38+
return Status.INVALID_ARGUMENT.withDescription("Your description").withCause(e);
3939
}
4040

4141
@GrpcExceptionHandler(ResourceNotFoundException.class)
4242
public StatusException handleResourceNotFoundException(ResourceNotFoundException e) {
43-
Status status = Status.NOT_FOUND.withDescription("Your description");
43+
Status status = Status.NOT_FOUND.withDescription("Your description").withCause(e);
4444
Metadata metadata = ...
4545
return status.asException(metadata);
4646
}
4747

4848
}
4949
```
5050

51-
- `@GrpcAdvice` marks a class to be picked up for exception handling
52-
- `@GrpcExceptionHandler` maps given method to be executed, in case of _specified_ thrown exception
53-
- f.e. if your application throws `IllegalArgumentException`, then the `handleInvalidArgument(IllegalArgumentException e)` method will be is executed
54-
- `io.grpc.Status` is specified and returned response status
51+
- `@GrpcAdvice` marks a class to be checked up for exception handling methods
52+
- `@GrpcExceptionHandler` marks the annotated method to be executed, in case of the _specified_ exception being thrown
53+
- f.e. if your application throws `IllegalArgumentException`,
54+
then the `handleInvalidArgument(IllegalArgumentException e)` method will be executed
55+
- The method must either return a `io.grpc.Status`, `StatusException`, or `StatusRuntimeException`
56+
- If you handle server errors, you might want to log the exception/stacktrace inside the exception handler
5557

5658
> **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-)
59+
> So we recommend adding it to the `Status`/`StatusException` to avoid the loss of information on the server side.
5760
5861
## Detailed explanation
5962

6063
### Priority of mapped exceptions
6164

62-
Given method with specified Exception in Annotation *and* as method argument
65+
Given this method with specified exception in the annotation *and* as a method argument
6366

6467
```java
65-
@GrpcExceptionHandler(ResourceNotFoundException.class)
66-
public StatusException handleResourceNotFoundException(ResourceNotFoundException e) {
67-
// your exception handling
68-
}
68+
@GrpcExceptionHandler(ResourceNotFoundException.class)
69+
public StatusException handleResourceNotFoundException(ResourceNotFoundException e) {
70+
// your exception handling
71+
}
6972
```
70-
> **Note:** Exception type in annotation is prioritized in mapping the exception over listed method argument
71-
> **and** they _must_ match the types declared with this value.
7273

73-
_(Matching means: Exception type in annotation is superclass of listed method parameter)_
74+
If the `GrpcExceptionHandler` annotation contains at least one exception type, then only those will be
75+
considered for exception handling for that method. The method parameters must be "compatible" with the specified
76+
exception types. If the annotation does not specify any handled exception types, then all method parameters are being
77+
used instead.
7478

75-
If no annotation type is provided in the annotation, listed method parameter are being picked up.
79+
_("Compatible" means that the exception type in annotation is either the same class or a superclass of one of the
80+
listed method parameters)_
7681

7782
### Sending Metadata in response
7883

@@ -87,19 +92,18 @@ public StatusRuntimeException handleResourceNotFoundException(IllegalArgumentExc
8792
}
8893
```
8994

90-
As you do not need `Metadata` in your response, just return your specified `Status`.
95+
If you do not need `Metadata` in your response, just return your specified `Status`.
9196

9297
### Overview of returnable types
9398

94-
Here is a small overview of possible mapped return types with `@GrpcExceptionHandler` and if
95-
custom Metadata can be returned.
99+
Here is a small overview of possible mapped return types with `@GrpcExceptionHandler` and if custom `Metadata` can be
100+
returned:
96101

97-
| Return Type | Custom Metadata |
102+
| Return Type | Supports Custom Metadata |
98103
| ----------- | --------------- |
99-
| Status | ✗ |
100-
| StatusException | ✔ |
101-
| StatusRuntimeException | ✔ |
102-
| Throwable | ✗ |
104+
| `Status` | ✗ |
105+
| `StatusException` | ✔ |
106+
| `StatusRuntimeException` | ✔ |
103107

104108
## Additional Topics <!-- omit in toc -->
105109

0 commit comments

Comments
 (0)