提交 3f08f6a1 编写于 作者: 武汉红喜's avatar 武汉红喜

AppExceptionHandler

上级 78c91c7f
package com.whatsmars.spring.boot.controller;
import com.whatsmars.spring.boot.exception.BusinessException;
import com.whatsmars.spring.boot.exception.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -47,7 +47,7 @@ public class NewController {
@RequestMapping(value = "/e", method = RequestMethod.GET)
public HttpStatus e(@RequestParam(required = false) String error) {
if (error != null) throw BusinessException.build(error);
if (error != null) throw AppException.build(error);
return HttpStatus.OK;
}
}
......@@ -7,12 +7,12 @@ import org.springframework.validation.ObjectError;
/**
* Created by shenhongxi on 2017/11/16.
*/
public class BusinessException extends RuntimeException {
public class AppException extends RuntimeException {
private Code errorCode;
private String errorMsg;
private BusinessException() {
private AppException() {
}
@Override
......@@ -21,27 +21,27 @@ public class BusinessException extends RuntimeException {
return this.getErrorMsg();
}
public static BusinessException build(Code errorCode, String errorMsg) {
return new BusinessException().setErrorCode(errorCode).setErrorMsg(errorMsg);
public static AppException build(Code errorCode, String errorMsg) {
return new AppException().setErrorCode(errorCode).setErrorMsg(errorMsg);
}
public static BusinessException build(String errorMsg) {
return new BusinessException().setErrorCode(Code.ERROR).setErrorMsg(errorMsg);
public static AppException build(String errorMsg) {
return new AppException().setErrorCode(Code.ERROR).setErrorMsg(errorMsg);
}
public static BusinessException build(BindingResult bindingResult) {
public static AppException build(BindingResult bindingResult) {
StringBuilder sb = new StringBuilder();
for (ObjectError e : bindingResult.getAllErrors()) {
sb.append(e.getDefaultMessage()).append(";");
}
return new BusinessException().setErrorCode(Code.ERROR).setErrorMsg(sb.toString());
return new AppException().setErrorCode(Code.ERROR).setErrorMsg(sb.toString());
}
public Code getErrorCode() {
return errorCode;
}
public BusinessException setErrorCode(Code errorCode) {
public AppException setErrorCode(Code errorCode) {
this.errorCode = errorCode;
return this;
}
......@@ -50,7 +50,7 @@ public class BusinessException extends RuntimeException {
return errorMsg;
}
public BusinessException setErrorMsg(String errorMsg) {
public AppException setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
return this;
}
......
......@@ -23,15 +23,15 @@ import java.util.Map;
* Created by shenhongxi on 2017/11/16.
*/
@ControllerAdvice(annotations = { RestController.class, Controller.class})
public class BusinessExceptionHandler {
public class AppExceptionHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 业务异常处理,直接返回异常信息提示
*/
@ExceptionHandler(BusinessException.class)
public ResponseEntity<ReturnItem> businessExceptionHandle(BusinessException exception, HttpServletRequest request) {
@ExceptionHandler(AppException.class)
public ResponseEntity<ReturnItem> businessExceptionHandle(AppException exception, HttpServletRequest request) {
logError(exception, request, LogLevel.WARN);
return new ResponseEntity<ReturnItem>(exception.toResultItem(), HttpStatus.OK);
}
......@@ -42,7 +42,7 @@ public class BusinessExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ReturnItem> defaultExceptionHandle(Exception exception, HttpServletRequest request) {
logError(exception, request, LogLevel.ERROR);
return new ResponseEntity<ReturnItem>(new ReturnItem(BusinessException.Code.ERROR.getValue(), ProfileUtils.isDev() ? exception.getMessage() : ReturnMessage.Message.OPERATION_ERROR.getValue()), HttpStatus.OK);
return new ResponseEntity<ReturnItem>(new ReturnItem(AppException.Code.ERROR.getValue(), ProfileUtils.isDev() ? exception.getMessage() : ReturnMessage.Message.OPERATION_ERROR.getValue()), HttpStatus.OK);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册