package com.fdkankan.common.exception; import cn.hutool.core.exceptions.ExceptionUtil; import com.fdkankan.common.constant.ServerCode; import com.fdkankan.common.exception.BusinessException; import com.fdkankan.common.response.ResultData; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; /** * 全局异常处理器 */ @ControllerAdvice @Slf4j public class GlobalExceptionHandler { /** * 处理未知异常 */ @ResponseBody @ExceptionHandler(value = Exception.class) public ResultData exceptionHandler(HttpServletRequest httpServletRequest, Exception e) { log.error("服务错误:", e); return ResultData.error(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000)); } /** * 处理业务异常 */ @ResponseBody @ExceptionHandler(value = BusinessException.class) public ResultData businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e) { log.info("业务异常code:" + e.getCode() + "msg:" + e.getMessage()); return ResultData.error(e.getCode(), e.getMessage()); } }