Skip to content

Commit 93df368

Browse files
committed
check for superclass of thrown exception
1 parent cf9aaee commit 93df368

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/service/exceptionhandling/GrpcExceptionAspect.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ public <E extends Throwable> void handleExceptionInsideGrpcService(JoinPoint joi
7575
log.error("Exception caught during gRPC service execution: ", exception);
7676
this.exception = exception;
7777

78-
if (!grpcExceptionHandlerMethodResolver.isMethodMappedForException(exception.getClass())) {
78+
boolean exceptionIsMapped =
79+
grpcExceptionHandlerMethodResolver.isMethodMappedForException(exception.getClass());
80+
if (!exceptionIsMapped) {
7981
return;
8082
}
83+
8184
extractNecessaryInformation();
8285
Throwable throwable = invokeMappedMethodSafely();
8386
closeStreamObserverOnError(joinPoint.getArgs(), throwable);
@@ -118,7 +121,7 @@ private Object[] determineInstancedParameters(Method mappedMethod) {
118121

119122
for (int i = 0; i < parameters.length; i++) {
120123
Class<?> parameterClass = convertToClass(parameters[i]);
121-
if (parameterClass.equals(exception.getClass())) {
124+
if (parameterClass.isAssignableFrom(exception.getClass())) {
122125
instancedParams[i] = exception;
123126
break;
124127
}
@@ -139,7 +142,7 @@ private Throwable castToThrowable(Object statusThrowable) {
139142
.filter(thrbl -> thrbl instanceof Throwable)
140143
.map(thrbl -> (Throwable) thrbl)
141144
.orElseThrow(() -> new IllegalStateException(
142-
"Return type has to beo f type java.lang.Throable: " + statusThrowable));
145+
"Return type has to be of type java.lang.Throwable: " + statusThrowable));
143146
}
144147

145148
private void closeStreamObserverOnError(Object[] joinPointParams, Throwable throwable) {

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/service/exceptionhandling/GrpcExceptionHandlerMethodResolver.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Arrays;
2323
import java.util.HashMap;
2424
import java.util.Map;
25-
import java.util.Optional;
2625
import java.util.Set;
2726
import java.util.function.Function;
2827
import java.util.stream.Collectors;
@@ -98,7 +97,7 @@ private void checkParamTypesAreAlignedToExceptionTypes(Method method) {
9897
private Class<? extends Throwable>[] checkForExceptionType(Class<?>[] methodParamTypes) {
9998

10099
for (Class<?> methodParamType : methodParamTypes) {
101-
if (!RuntimeException.class.isAssignableFrom(methodParamType)) {
100+
if (!Throwable.class.isAssignableFrom(methodParamType)) {
102101
throw new IllegalStateException("Annotated Class is not of Type Throwable: " + methodParamType);
103102
}
104103
}
@@ -135,22 +134,34 @@ private void addExceptionMapping(Class<? extends Throwable> exceptionType, Metho
135134
}
136135
}
137136

138-
<E extends Throwable> boolean isMethodMappedForException(Class<E> exception) {
139-
return mappedMethods.get(exception) != null;
140-
}
141137

142-
Map.Entry<Object, Method> resolveMethodWithInstance(Class<? extends Throwable> exceptionType) {
138+
public <E extends Throwable> Map.Entry<Object, Method> resolveMethodWithInstance(Class<E> exceptionType) {
139+
140+
Method value = extractExtendedThrowable(exceptionType);
141+
if (value == null) {
142+
return new SimpleImmutableEntry<>(null, null);
143+
}
143144

144-
Method value = mappedMethods.get(exceptionType);
145-
Class<?> methodClass = Optional.ofNullable(value).map(Method::getDeclaringClass).orElse(null);
145+
Class<?> methodClass = value.getDeclaringClass();
146146
Object key = annotatedBeans.values()
147147
.stream()
148-
.filter(obj -> obj.getClass().equals(methodClass))
148+
.filter(obj -> methodClass.isAssignableFrom(obj.getClass()))
149149
.findFirst()
150150
.orElse(null);
151-
152151
return new SimpleImmutableEntry<>(key, value);
153152
}
154153

154+
public <E extends Throwable> boolean isMethodMappedForException(Class<E> exception) {
155+
return extractExtendedThrowable(exception) != null;
156+
}
157+
158+
private <E extends Throwable> Method extractExtendedThrowable(Class<E> exception) {
159+
return mappedMethods.keySet()
160+
.stream().filter(clazz -> clazz.isAssignableFrom(exception))
161+
.findAny()
162+
.map(mappedMethods::get)
163+
.orElse(null);
164+
}
165+
155166

156167
}

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/service/exceptionhandling/MethodExecutionException.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
117

218
package net.devh.boot.grpc.server.service.exceptionhandling;
319

0 commit comments

Comments
 (0)