GlobalExceptionHandler.java 1.29 KB
package com.viontech.fanxing.commons.base;

import com.viontech.fanxing.commons.exception.FanXingException;
import com.viontech.keliu.util.JsonMessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * .
 *
 * @author 谢明辉
 * @date 2020/6/4
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public Object exceptionHandler(Exception e) {
        log.info("", e);
        return JsonMessageUtil.getErrorJsonMsg(e.getMessage());
    }

    @ExceptionHandler(FanXingException.class)
    public Object fanXingExceptionHandler(FanXingException fanXingException) {
        JsonMessageUtil.JsonMessage errorJsonMsg = JsonMessageUtil.getErrorJsonMsg(fanXingException.getMessage());
        errorJsonMsg.setData(fanXingException.getData());
        StackTraceElement[] stackTrace = fanXingException.getStackTrace();
        log.info("\n接口调用出错\n错误信息:[{}]\n错误原因:[{}]", fanXingException.getMessage(), stackTrace.length > 0 ? stackTrace[0].toString() : "未知");
        return errorJsonMsg;
    }
}