Helius
2021-06-21 5812839f3e23bb09e1a9b100b63273a646155709
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.xzx.log.util;
 
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ReflectUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
@Component
@Slf4j
public class BusinessUtil {
 
 
 
    /**
     * 根据异常获取方法注释 若是中文异常则忽略
     * @param e
     * @return
     */
    public String getMethodAnnoByEx(Exception e){
        try {
            Throwable rootCause = ExceptionUtil.getRootCause(e);
            if(rootCause!=null){
                StackTraceElement[] stackTrace = rootCause.getStackTrace();
                if(stackTrace!=null&&stackTrace.length>0){
                    StackTraceElement stackTraceElement = stackTrace[0];
                    String methodName = stackTraceElement.getMethodName();
                    String className = stackTraceElement.getClassName();
                    Method methodByName = ReflectUtil.getMethodByName(Class.forName(className), methodName);
                    ApiOperation annotation = AnnotationUtil.getAnnotation(methodByName, ApiOperation.class);
                    if(annotation!=null){
                        return annotation.value()+"失败";
                    }
                }
            }
        }catch (Exception e1) {
            return null;
        }
        return null;
    }
 
 
 
}