GlobalExceptionHandler.java 1.39 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;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * .
 *
 * @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, HttpServletRequest request) {
        log.error(request.getRequestURI() + "?" + Optional.ofNullable(request.getQueryString()).orElse(""), e);
        return JsonMessageUtil.getErrorJsonMsg(e.getMessage());
    }

    @ExceptionHandler(FanXingException.class)
    public Object fanXingExceptionHandler(FanXingException fanXingException, HttpServletRequest request) {
        JsonMessageUtil.JsonMessage errorJsonMsg = JsonMessageUtil.getErrorJsonMsg(fanXingException.getMessage());
        errorJsonMsg.setData(fanXingException.getData());
        log.error(request.getRequestURI() + "?" + Optional.ofNullable(request.getQueryString()).orElse(""), fanXingException);
        return errorJsonMsg;
    }
}