package com.gkhy.assess.framework.exception;
|
|
import com.gkhy.assess.common.api.CommonResult;
|
import com.gkhy.assess.common.exception.ApiException;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.FieldError;
|
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.RestControllerAdvice;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.sql.SQLException;
|
|
/**
|
* 全局异常处理类
|
*/
|
@RestControllerAdvice
|
@Slf4j
|
public class GlobalExceptionHandler {
|
@Autowired
|
HttpServletRequest request;
|
|
@ExceptionHandler(value = ApiException.class)
|
public CommonResult handle(ApiException e){
|
// writeExceptionLogFile(e);
|
if(e.getErrorCode()!=null){
|
return CommonResult.failed(e.getErrorCode());
|
}
|
return CommonResult.failed(e.getMessage());
|
}
|
|
|
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
public CommonResult handleValidException(MethodArgumentNotValidException e) {
|
BindingResult bindingResult = e.getBindingResult();
|
String message = null;
|
if (bindingResult.hasErrors()) {
|
FieldError fieldError = bindingResult.getFieldError();
|
if (fieldError != null) {
|
message = fieldError.getField()+fieldError.getDefaultMessage();
|
}
|
}
|
writeExceptionLogFile(e);
|
return CommonResult.validateFailed(message);
|
}
|
|
@ExceptionHandler(value = BindException.class)
|
public CommonResult handleValidException(BindException e) {
|
BindingResult bindingResult = e.getBindingResult();
|
String message = null;
|
if (bindingResult.hasErrors()) {
|
FieldError fieldError = bindingResult.getFieldError();
|
if (fieldError != null) {
|
message = fieldError.getField()+fieldError.getDefaultMessage();
|
}
|
}
|
writeExceptionLogFile(e);
|
return CommonResult.validateFailed(message);
|
}
|
|
|
@ExceptionHandler(SQLException.class)
|
public CommonResult sqlExceptionHandler(Exception ex) {
|
writeExceptionLogFile(ex);
|
return CommonResult.failed( "执行sql异常");
|
}
|
|
|
/**
|
* 拦截未知的运行时异常
|
*/
|
@ExceptionHandler(RuntimeException.class)
|
public CommonResult handleRuntimeException(RuntimeException ex, HttpServletRequest request)
|
{
|
writeExceptionLogFile(ex);
|
return CommonResult.failed(ex.getMessage());
|
}
|
|
|
/**
|
* 500 其他服务器异常
|
*/
|
@ExceptionHandler(Exception.class)
|
public CommonResult otherExceptionHandler(Exception ex) {
|
String message = !StringUtils.isEmpty(ex.getMessage()) ? ex.getMessage() : ex.toString();
|
if(StringUtils.isNotBlank(message)&&message.contains("java.sql")){
|
message="执行sql异常";
|
}
|
writeExceptionLogFile(ex);
|
return CommonResult.failed(message);
|
}
|
|
private void writeExceptionLogFile(Exception ex) {
|
String url="";
|
if (request!=null) {
|
url=request.getRequestURI();
|
}
|
log.error("error={}",ex);
|
log.error(ex.getMessage()+",url={}",url);
|
}
|
}
|