All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tencent.trpc.spring.exception.support.DefaultExceptionHandlerResolver Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/*
 * Tencent is pleased to support the open source community by making tRPC available.
 *
 * Copyright (C) 2023 THL A29 Limited, a Tencent company.
 * All rights reserved.
 *
 * If you have downloaded a copy of the tRPC source code from Tencent,
 * please note that tRPC source code is licensed under the Apache 2.0 License,
 * A copy of the Apache 2.0 License can be found in the LICENSE file.
 */

package com.tencent.trpc.spring.exception.support;

import com.tencent.trpc.spring.exception.annotation.TRpcExceptionHandler;
import com.tencent.trpc.spring.exception.annotation.TRpcExceptionHandlerRegistry;
import com.tencent.trpc.spring.exception.api.ExceptionHandler;
import com.tencent.trpc.spring.exception.api.ExceptionHandlerResolver;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;

/**
 * Default implementation of tRPC {@link ExceptionHandlerResolver}.
 * 

Resolve ExceptionHandler by scan methods annotated with {@link TRpcExceptionHandler}, in order of:

*
    *
  1. scan annotated methods in target bean
  2. *
  3. scan annotated methods in {@link Configuration} * class annotated with {@link TRpcExceptionHandlerRegistry}
  4. *
* * @see TRpcExceptionHandler * @see TRpcExceptionHandlerRegistry * @see AnnotationExceptionHandlerResolver */ public class DefaultExceptionHandlerResolver extends AnnotationExceptionHandlerResolver { private final Map, ExceptionHandlerResolver> specificResolvers = new ConcurrentHashMap<>(8); @Override public ExceptionHandler resolveExceptionHandler(Throwable e, Object target, Method targetMethod) { ExceptionHandler exceptionHandler = null; // scan annotated methods in target bean ExceptionHandlerResolver specificResolver = getSpecificExceptionHandlerResolver(target); if (specificResolver != null) { exceptionHandler = specificResolver.resolveExceptionHandler(e, target, targetMethod); } // if no annotated methods found in target bean, fallback to scan TRpcExceptionHandlerRegistry annotated class if (exceptionHandler == null) { exceptionHandler = super.resolveExceptionHandler(e, target, targetMethod); } return exceptionHandler; } private ExceptionHandlerResolver getSpecificExceptionHandlerResolver(Object bean) { if (bean == null) { return null; } Class targetType = ClassUtils.getUserClass(bean); return specificResolvers.computeIfAbsent(targetType, type -> new AnnotationExceptionHandlerResolver(bean)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy