You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/server/exception-handling.md
+30-26Lines changed: 30 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,56 +23,61 @@ This section describes how you can handle exceptions inside GrpcService layer wi
23
23
24
24
## Proper exception handling
25
25
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.
Status status =Status.NOT_FOUND.withDescription("Your description");
43
+
Status status =Status.NOT_FOUND.withDescription("Your description").withCause(e);
44
44
Metadata metadata =...
45
45
return status.asException(metadata);
46
46
}
47
47
48
48
}
49
49
```
50
50
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
55
57
56
58
> **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.
57
60
58
61
## Detailed explanation
59
62
60
63
### Priority of mapped exceptions
61
64
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
0 commit comments