增加异常处理
This commit is contained in:
parent
6f7262ca3e
commit
4dfed91457
@ -0,0 +1,81 @@
|
|||||||
|
package com.yovinchen.train.common.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.yovinchen.train.common.exception.BusinessException;
|
||||||
|
import com.yovinchen.train.common.resp.CommonResp;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.validation.BindException;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: ControllerExceptionHandler
|
||||||
|
* Package: com.yovinchen.train.common.controller
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2024/1/21 10:34
|
||||||
|
*/
|
||||||
|
@ControllerAdvice
|
||||||
|
public class ControllerExceptionHandler {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionHandler.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 所有异常统一处理
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(value = Exception.class)
|
||||||
|
@ResponseBody
|
||||||
|
public CommonResp exceptionHandler(Exception e) {
|
||||||
|
CommonResp commonResp = new CommonResp();
|
||||||
|
LOG.error("系统异常:", e);
|
||||||
|
commonResp.setSuccess(false);
|
||||||
|
commonResp.setMessage("系统出现异常,请联系管理员");
|
||||||
|
return commonResp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 业务异常统一处理
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(value = BusinessException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public CommonResp exceptionHandler(BusinessException e) {
|
||||||
|
CommonResp commonResp = new CommonResp();
|
||||||
|
LOG.error("业务异常:{}", e.getE()
|
||||||
|
.getDesc());
|
||||||
|
commonResp.setSuccess(false);
|
||||||
|
commonResp.setMessage(e.getE()
|
||||||
|
.getDesc());
|
||||||
|
return commonResp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验异常统一处理
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(value = BindException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public CommonResp exceptionHandler(BindException e) {
|
||||||
|
CommonResp commonResp = new CommonResp();
|
||||||
|
LOG.error("校验异常:{}", e.getBindingResult()
|
||||||
|
.getAllErrors()
|
||||||
|
.get(0)
|
||||||
|
.getDefaultMessage());
|
||||||
|
commonResp.setSuccess(false);
|
||||||
|
commonResp.setMessage(e.getBindingResult()
|
||||||
|
.getAllErrors()
|
||||||
|
.get(0)
|
||||||
|
.getDefaultMessage());
|
||||||
|
return commonResp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -5,9 +5,8 @@
|
|||||||
<property name="PATH" value="./log/${APP_NAME}"/>
|
<property name="PATH" value="./log/${APP_NAME}"/>
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
<encoder>
|
<encoder>
|
||||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %blue(%-50logger{50}:%-4line) %thread
|
@formatter:off
|
||||||
%green(%-18X{LOG_ID}) %msg%n
|
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %blue(%-50logger{50}:%-4line) %thread %green(%-18X{LOG_ID}) %msg%n</Pattern>
|
||||||
</Pattern>
|
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user