|
| 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 | + */ |
| 17 | + |
| 18 | +package net.devh.boot.grpc.server.advice; |
| 19 | + |
| 20 | +import java.lang.reflect.InvocationTargetException; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.lang.reflect.Parameter; |
| 23 | +import java.lang.reflect.Type; |
| 24 | +import java.util.Map.Entry; |
| 25 | + |
| 26 | +import org.aspectj.lang.annotation.Aspect; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | + |
| 29 | +import lombok.extern.slf4j.Slf4j; |
| 30 | + |
| 31 | +/** |
| 32 | + * As part of {@link GrpcAdvice @GrpcAdvice}, when a thrown exception is caught during gRPC calls (via global |
| 33 | + * interceptor {@link GrpcAdviceExceptionInterceptor}, then this thrown exception is being handled. By |
| 34 | + * {@link GrpcExceptionHandlerMethodResolver} is a mapping between exception and the in case to be executed method |
| 35 | + * provided. <br> |
| 36 | + * Returned object is declared in {@link GrpcAdvice @GrpcAdvice} classes with annotated methods |
| 37 | + * {@link GrpcExceptionHandler @GrpcExceptionHandler}. |
| 38 | + * <p> |
| 39 | + * |
| 40 | + * @author Andjelko Perisic ([email protected]) |
| 41 | + * @see GrpcExceptionHandlerMethodResolver |
| 42 | + * @see GrpcAdviceExceptionInterceptor |
| 43 | + */ |
| 44 | +@Slf4j |
| 45 | +@Aspect |
| 46 | +public class GrpcAdviceExceptionHandler { |
| 47 | + |
| 48 | + private final GrpcExceptionHandlerMethodResolver grpcExceptionHandlerMethodResolver; |
| 49 | + |
| 50 | + public GrpcAdviceExceptionHandler( |
| 51 | + final GrpcExceptionHandlerMethodResolver grpcExceptionHandlerMethodResolver) { |
| 52 | + this.grpcExceptionHandlerMethodResolver = grpcExceptionHandlerMethodResolver; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Given an exception, a lookup is performed to retrieve mapped method. <br> |
| 57 | + * In case of successful returned method, and matching exception parameter type for given exception, the exception |
| 58 | + * is handed over to retrieved method. Retrieved method is then being invoked. |
| 59 | + * |
| 60 | + * @param exception exception to search for |
| 61 | + * @param <E> type of exception |
| 62 | + * @return result of invoked mapped method to given exception |
| 63 | + * @throws Throwable rethrows exception if no mapping existent or exceptions raised by implementation |
| 64 | + */ |
| 65 | + @Nullable |
| 66 | + public <E extends Throwable> Object handleThrownException(E exception) throws Throwable { |
| 67 | + |
| 68 | + final Class<? extends Throwable> exceptionClass = exception.getClass(); |
| 69 | + boolean exceptionIsMapped = |
| 70 | + grpcExceptionHandlerMethodResolver.isMethodMappedForException(exceptionClass); |
| 71 | + if (!exceptionIsMapped) { |
| 72 | + throw exception; |
| 73 | + } |
| 74 | + |
| 75 | + Entry<Object, Method> methodWithInstance = |
| 76 | + grpcExceptionHandlerMethodResolver.resolveMethodWithInstance(exceptionClass); |
| 77 | + Method mappedMethod = methodWithInstance.getValue(); |
| 78 | + Object instanceOfMappedMethod = methodWithInstance.getKey(); |
| 79 | + Object[] instancedParams = determineInstancedParameters(mappedMethod, exception); |
| 80 | + |
| 81 | + return invokeMappedMethodSafely(mappedMethod, instanceOfMappedMethod, instancedParams); |
| 82 | + } |
| 83 | + |
| 84 | + private <E extends Throwable> Object[] determineInstancedParameters(Method mappedMethod, E exception) { |
| 85 | + |
| 86 | + Parameter[] parameters = mappedMethod.getParameters(); |
| 87 | + Object[] instancedParams = new Object[parameters.length]; |
| 88 | + |
| 89 | + for (int i = 0; i < parameters.length; i++) { |
| 90 | + Class<?> parameterClass = convertToClass(parameters[i]); |
| 91 | + if (parameterClass.isAssignableFrom(exception.getClass())) { |
| 92 | + instancedParams[i] = exception; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + return instancedParams; |
| 97 | + } |
| 98 | + |
| 99 | + private Class<?> convertToClass(Parameter parameter) { |
| 100 | + Type paramType = parameter.getParameterizedType(); |
| 101 | + if (paramType instanceof Class) { |
| 102 | + return (Class<?>) paramType; |
| 103 | + } |
| 104 | + throw new IllegalStateException("Parameter type of method has to be from Class, it was: " + paramType); |
| 105 | + } |
| 106 | + |
| 107 | + private Object invokeMappedMethodSafely( |
| 108 | + Method mappedMethod, |
| 109 | + Object instanceOfMappedMethod, |
| 110 | + Object[] instancedParams) throws Throwable { |
| 111 | + Object statusThrowable = null; |
| 112 | + try { |
| 113 | + statusThrowable = mappedMethod.invoke(instanceOfMappedMethod, instancedParams); |
| 114 | + } catch (InvocationTargetException | IllegalAccessException e) { |
| 115 | + throw e.getCause(); // throw the exception thrown by implementation |
| 116 | + } |
| 117 | + return statusThrowable; |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments