码迷,mamicode.com
首页 > 其他好文 > 详细

统一异常管理

时间:2021-07-21 17:30:16      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:port   author   get   自定义异常   message   gre   hand   slf4j   ret   



import com.logistics.channel.constant.BizException;
import com.logistics.channel.tool.CommonErrorEnum;
import com.logistics.channel.tool.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Slf4j
@ResponseBody
public class UnifiedExceptionHandler {


/**
* @Author: ljh
* @Description: 系统异常捕获处理
* @Date: 16:07 2018/5/30
*/
@ExceptionHandler(value = Exception.class)
public ResultBody javaExceptionHandler(HttpServletRequest re , Exception ex) {
log.error("捕获到Exception异常", ex);
//异常日志入库
return ResultBody.error(CommonErrorEnum.SERVER_BUSY);
}

/**
* @Author: ljh
* @Description: 自定义异常捕获处理
* @Date: 16:08 2018/5/30
*/
@ExceptionHandler(value = BizException.class)//BizException是自定义的一个异常
public ResultBody bizExceptionHandler(BizException ex) {
log.error("捕获到MessageCenterException异常", ex.getMessage());
//异常日志入库

return ResultBody.error(ex.getMessage());
}

/**
* @Author: ljh
* @Description: 自定义异常捕获处理
* @Date: 16:08 2021/7/20
*/
@ExceptionHandler(value = IllegalArgumentException.class)//MessageCenterException是自定义的一个异常
public ResultBody illegalArgumentExceptionHandler(IllegalArgumentException ex) {
log.error("捕获到IllegalArgumentException异常", ex.getMessage());
//异常日志入库
return ResultBody.error(ex.getMessage());
}

/**
* @Author: ljh
* @Description: 自定义异常捕获处理
* @Date: 16:08 2021/7/20
*/
@ExceptionHandler(value = NullPointerException.class)
public ResultBody nullPointerExceptionHandler(NullPointerException ex) {
log.error("捕获到NullPointerException异常", ex.getMessage());
//异常日志入库
return ResultBody.error(CommonErrorEnum.SERVER_BUSY);
}

/**
* @Author: ljh
* @Description: @NotBlank,@NotNull注解的message信息异常捕获处理
* @Date: 16:08 2021/7/20
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public ResultBody exceptionHandler(BindException ex) {
log.error("捕获到BindException异常", ex.getMessage());
return ResultBody.error(ex.getBindingResult().getFieldError().getDefaultMessage());
}

/**
* @Author: ljh
* @Description: restful
* @Date: 16:08 2021/7/20
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(NoHandlerFoundException.class)
public ResultBody noHandlerFoundExceptionHandler(NoHandlerFoundException ex) {
log.error("捕获到BindException异常", ex.getMessage());
return ResultBody.error(CommonErrorEnum.URL_ERROR);
}

/** @NotBlank,
* @NotNull注解的message信息异常捕获处理
* */
@ExceptionHandler(value= MethodArgumentNotValidException.class)
public ResultBody MethodArgumentNotValidHandler(HttpServletRequest request, MethodArgumentNotValidException ex) {

log.error("捕获到MethodArgumentNotValidHandler异常:",ex.getBindingResult().getFieldError().getDefaultMessage());

return ResultBody.error(ex.getBindingResult().getFieldError().getDefaultMessage());
}

}

统一异常管理

标签:port   author   get   自定义异常   message   gre   hand   slf4j   ret   

原文地址:https://www.cnblogs.com/jiahaoJAVA/p/15035449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!