Commit 6ce95a7a by guojuxing

common全局异常处理注视

parent 88eec371
package com.gic.enterprise.exception;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.error.ErrorCode;
/**
* 全局异常处理类
*
* @author hua
*/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
public RestResponse controllerException(HttpServletResponse response, Exception ex) {
logger.error("err", ex);
RestResponse failureResponse = getRestResponse(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMsg());
StringBuilder sb = new StringBuilder();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PrintWriter printWriter = new PrintWriter(baos)) {
ex.printStackTrace(printWriter);
}
try {
sb.append(baos.toString());
} catch (Exception ignored) {
}
if (sb.length() == 0) {
sb.append(ex.getMessage());
}
// 输出详细错误信息,便于调试
failureResponse.setDetailError(sb.toString());
return failureResponse;
}
/**
* 参数校验异常统一处理
* @param e
* @return
*/
@ExceptionHandler(BindException.class)
public RestResponse customException(BindException e) {
List<FieldError> fieldErrors = e.getFieldErrors();
StringBuilder errorMessage = new StringBuilder();
fieldErrors.forEach(fieldError -> {
errorMessage
.append(fieldError.getDefaultMessage())
.append(",");
});
String error = errorMessage.toString();
return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), error.substring(0, error.length() - 1));
}
@ExceptionHandler(ConstraintViolationException.class)
public RestResponse constraintViolationException(ConstraintViolationException e) {
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
String paramName = constraintViolations.iterator().next().getPropertyPath().toString();
String paramError = constraintViolations.iterator().next().getMessage();
return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), getFailFastMsg(paramName, paramError));
}
@ResponseBody
@ExceptionHandler(CommonException.class)
public RestResponse customException(CommonException e) {
return getRestResponse(e.getErrorCode(), e.getMessage());
}
private RestResponse getRestResponse(String errorCode, String message) {
return RestResponse.failure(errorCode, message);
}
private static String getFailFastMsg(String paramName, String paramError) {
return String.format("%s:%s", paramName, paramError);
}
}
//package com.gic.enterprise.exception;
//
//import java.io.ByteArrayOutputStream;
//import java.io.PrintWriter;
//import java.util.List;
//import java.util.Set;
//
//import javax.servlet.http.HttpServletResponse;
//import javax.validation.ConstraintViolation;
//import javax.validation.ConstraintViolationException;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.validation.BindException;
//import org.springframework.validation.FieldError;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import com.gic.commons.webapi.reponse.RestResponse;
//import com.gic.enterprise.error.ErrorCode;
//
///**
// * 全局异常处理类
// *
// * @author hua
// */
//@ControllerAdvice
//@ResponseBody
//public class GlobalExceptionHandler {
// private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
//
// @ExceptionHandler(Exception.class)
// public RestResponse controllerException(HttpServletResponse response, Exception ex) {
// logger.error("err", ex);
// RestResponse failureResponse = getRestResponse(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMsg());
// StringBuilder sb = new StringBuilder();
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// try (PrintWriter printWriter = new PrintWriter(baos)) {
// ex.printStackTrace(printWriter);
// }
// try {
// sb.append(baos.toString());
// } catch (Exception ignored) {
// }
// if (sb.length() == 0) {
// sb.append(ex.getMessage());
// }
// // 输出详细错误信息,便于调试
// failureResponse.setDetailError(sb.toString());
// return failureResponse;
// }
//
// /**
// * 参数校验异常统一处理
// * @param e
// * @return
// */
// @ExceptionHandler(BindException.class)
// public RestResponse customException(BindException e) {
// List<FieldError> fieldErrors = e.getFieldErrors();
// StringBuilder errorMessage = new StringBuilder();
// fieldErrors.forEach(fieldError -> {
// errorMessage
// .append(fieldError.getDefaultMessage())
// .append(",");
// });
// String error = errorMessage.toString();
// return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), error.substring(0, error.length() - 1));
// }
//
// @ExceptionHandler(ConstraintViolationException.class)
// public RestResponse constraintViolationException(ConstraintViolationException e) {
// Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
// String paramName = constraintViolations.iterator().next().getPropertyPath().toString();
// String paramError = constraintViolations.iterator().next().getMessage();
// return getRestResponse(ErrorCode.PARAMETER_ERROR.getCode(), getFailFastMsg(paramName, paramError));
// }
//
//
// @ResponseBody
// @ExceptionHandler(CommonException.class)
// public RestResponse customException(CommonException e) {
// return getRestResponse(e.getErrorCode(), e.getMessage());
// }
//
//
// private RestResponse getRestResponse(String errorCode, String message) {
// return RestResponse.failure(errorCode, message);
// }
//
// private static String getFailFastMsg(String paramName, String paramError) {
// return String.format("%s:%s", paramName, paramError);
// }
//
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment