You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.8 KiB

/*********************************************************
*********************************************************
******************** *******************
************* ************
******* _oo0oo_ *******
*** o8888888o ***
* 88" . "88 *
* (| -_- |) *
* 0\ = /0 *
* ___/`---'\___ *
* .' \\| |// '. *
* / \\||| : |||// \ *
* / _||||| -:- |||||- \ *
* | | \\\ - /// | | *
* | \_| ''\---/'' |_/ | *
* \ .-\__ '-' ___/-. / *
* ___'. .' /--.--\ `. .'___ *
* ."" '< `.___\_<|>_/___.' >' "". *
* | | : `- \`.;`\ _ /`;.`/ - ` : | | *
* \ \ `_. \_ __\ /__ _/ .-` / / *
* =====`-.____`.___ \_____/___.-`___.-'===== *
* `=---=' *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
*********__佛祖保佑__永无BUG__验收通过__钞票多多__*********
*********************************************************/
package com.yyd.base.handler;
import com.yyd.base.common.result.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
/**
* Project: jbsc-commons <br/>
* File: GlobalExceptionHandler.java <br/>
* Class: org.jbase.jbsc.commons.base.config.handler.GlobalExceptionHandler <br/>
* Description: <描述类的功能>. <br/>
* Copyright: Copyright (c) 2011 <br/>
* Company: https://gitee.com/liuzp315 <br/>
* Makedate: 2020/9/17 上午10:34 <br/>
*
* @author popo
* @version 1.0
* @since 1.0
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger L = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 路径错误
*
* @param e
* @return
*/
@ExceptionHandler(NoHandlerFoundException.class)
public ResultBean handlerNoFoundException(Exception e) {
L.error(e.getMessage(), e);
return ResultBean.fireFail().setCode("404").setMsg("路径不存在,请检查路径是否正确");
}
/**
* 系统异常
*
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
public ResultBean handleException(Exception e) {
L.error(e.getMessage(), e);
//发送邮件
return ResultBean.fireFail().setMsg("系统异常::" + e.getMessage());
}
/**
* 自定义验证异常
*/
@ExceptionHandler(BindException.class)
public ResultBean validatedBindException(BindException e) {
L.error(e.getMessage(), e);
String message = e.getAllErrors().get(0).getDefaultMessage();
return ResultBean.fireFail().setCode("405").setMsg(message);
}
/**
* 方法参数验证异常
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResultBean validExceptionHandler(MethodArgumentNotValidException e) {
L.error(e.getMessage(), e);
String message = e.getBindingResult().getFieldError().getDefaultMessage();
return ResultBean.fireFail().setCode("405").setMsg(message);
}
}