优惠券+异常全局处理
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package com.chaozhanggui.system.cashierservice.exception;
|
||||
|
||||
|
||||
public enum DefaultError implements IError {
|
||||
/**
|
||||
* 系统内部错误
|
||||
*/
|
||||
SYSTEM_INTERNAL_ERROR("0000", "系统错误"),
|
||||
/**
|
||||
* 无效参数
|
||||
*/
|
||||
INVALID_PARAMETER("0001", "参数验证失败"),
|
||||
/**
|
||||
* 服务不存在
|
||||
*/
|
||||
SERVICE_NOT_FOUND("0002", "服务不存在"),
|
||||
/**
|
||||
* 参数不全
|
||||
*/
|
||||
PARAMETER_REQUIRED("0003", "参数不全"),
|
||||
/**
|
||||
* 参数过长
|
||||
*/
|
||||
PARAMETER_MAX_LENGTH("0004", "参数过长"),
|
||||
/**
|
||||
* 参数过短
|
||||
*/
|
||||
PARAMETER_MIN_LENGTH("0005", "参数过短"),
|
||||
/**
|
||||
* 认证失败
|
||||
*/
|
||||
AUTHENTICATION_ERROR("0006", "认证失败"),
|
||||
/**
|
||||
* 认证动作失败
|
||||
*/
|
||||
AUTHENTICATION_OPTION_ERROR("0007", "认证失败"),
|
||||
/**
|
||||
* 请求方法出错
|
||||
*/
|
||||
METHOD_NOT_SUPPORTED("0008", "请求方法出错"),
|
||||
/**
|
||||
* 不支持的content类型
|
||||
*/
|
||||
CONTENT_TYPE_NOT_SUPPORT("0009", "不支持的content类型"),
|
||||
/**
|
||||
* json格式化出错
|
||||
*/
|
||||
JSON_FORMAT_ERROR("0010", "json格式化出错"),
|
||||
/**
|
||||
* 远程调用出错
|
||||
*/
|
||||
CALL_REMOTE_ERROR("0011", "远程调用出错"),
|
||||
/**
|
||||
* 服务运行SQLException异常
|
||||
*/
|
||||
SQL_EXCEPTION("0012", "服务运行SQL异常"),
|
||||
/**
|
||||
* 客户端异常 给调用者 app,移动端调用
|
||||
*/
|
||||
CLIENT_EXCEPTION("0013", "客户端异常"),
|
||||
/**
|
||||
* 服务端异常, 微服务服务端产生的异常
|
||||
*/
|
||||
SERVER_EXCEPTION("0014", "服务端异常"),
|
||||
/**
|
||||
* 授权失败 禁止访问
|
||||
*/
|
||||
ACCESS_DENIED("0015", "没有访问权限"),
|
||||
/**
|
||||
* 演示环境 没有权限访问
|
||||
*/
|
||||
SHOW_AUTH_CONTROL("0016", "演示环境,没有权限访问"),
|
||||
/**
|
||||
* 业务异常
|
||||
*/
|
||||
BUSINESS_ERROR("0017", "业务异常"),
|
||||
|
||||
NO_USER("0018","用户不存在");
|
||||
|
||||
String errorCode;
|
||||
String errorMessage;
|
||||
private static final String NS = "SYS";
|
||||
|
||||
DefaultError(String errorCode, String errorMessage) {
|
||||
this.errorCode = errorCode;
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameSpace() {
|
||||
return NS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrorCode() {
|
||||
return NS + "." + this.errorCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrorMessage() {
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package com.chaozhanggui.system.cashierservice.exception;
|
||||
|
||||
import com.chaozhanggui.system.cashierservice.sign.Result;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
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.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.UnexpectedTypeException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@ControllerAdvice
|
||||
@ResponseBody
|
||||
public class DefaultExceptionAdvice {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionAdvice.class);
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler({HttpMessageNotReadableException.class, })
|
||||
public ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||
LOGGER.error("参数解析失败", e);
|
||||
Result response = Result.failure(DefaultError.INVALID_PARAMETER);
|
||||
response.setMsg(e.getMessage());
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
||||
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
|
||||
public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
||||
LOGGER.error("不支持当前请求方法", e);
|
||||
Result response = Result.failure(DefaultError.METHOD_NOT_SUPPORTED);
|
||||
response.setMsg(e.getMessage());
|
||||
return new ResponseEntity<>(response, HttpStatus.METHOD_NOT_ALLOWED);
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
|
||||
@ExceptionHandler({HttpMediaTypeNotSupportedException.class})
|
||||
public ResponseEntity handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
|
||||
LOGGER.error("不支持当前媒体类型", e);
|
||||
Result response = Result.failure(DefaultError.CONTENT_TYPE_NOT_SUPPORT);
|
||||
response.setMsg(e.getMessage());
|
||||
return new ResponseEntity<>(response, HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler({SQLException.class})
|
||||
public ResponseEntity handleSQLException(SQLException e) {
|
||||
LOGGER.error("服务运行SQLException异常", e);
|
||||
Result response = Result.failure(DefaultError.SQL_EXCEPTION);
|
||||
response.setMsg(e.getMessage());
|
||||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 所有异常统一处理
|
||||
*
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity handleException(Exception ex) {
|
||||
LOGGER.error("未知异常", ex);
|
||||
IError error;
|
||||
String extMessage = null;
|
||||
if (ex instanceof BindException) {
|
||||
error = DefaultError.INVALID_PARAMETER;
|
||||
List<ObjectError> errors = ((BindException) ex).getAllErrors();
|
||||
if (errors.size() != 0) {
|
||||
StringBuilder msg = new StringBuilder();
|
||||
for (ObjectError objectError : errors) {
|
||||
msg.append("Field error in object '").append(objectError.getObjectName()).append(" ");
|
||||
if (objectError instanceof FieldError) {
|
||||
msg.append("on field ").append(((FieldError) objectError).getField()).append(" ");
|
||||
}
|
||||
msg.append(objectError.getDefaultMessage()).append(" ");
|
||||
}
|
||||
extMessage = msg.toString();
|
||||
}
|
||||
} else if (ex instanceof MissingServletRequestParameterException) {
|
||||
error = DefaultError.INVALID_PARAMETER;
|
||||
extMessage = ex.getMessage();
|
||||
} else if (ex instanceof ConstraintViolationException) {
|
||||
error = DefaultError.INVALID_PARAMETER;
|
||||
Set<ConstraintViolation<?>> violations = ((ConstraintViolationException) ex).getConstraintViolations();
|
||||
final StringBuilder msg = new StringBuilder();
|
||||
for (ConstraintViolation<?> constraintViolation : violations) {
|
||||
msg.append(constraintViolation.getPropertyPath()).append(":").append(constraintViolation.getMessage()).append("\n");
|
||||
}
|
||||
extMessage = msg.toString();
|
||||
} else if (ex instanceof HttpMediaTypeNotSupportedException) {
|
||||
error = DefaultError.CONTENT_TYPE_NOT_SUPPORT;
|
||||
extMessage = ex.getMessage();
|
||||
} else if (ex instanceof HttpMessageNotReadableException) {
|
||||
error = DefaultError.INVALID_PARAMETER;
|
||||
extMessage = ex.getMessage();
|
||||
} else if (ex instanceof MethodArgumentNotValidException) {
|
||||
error = DefaultError.INVALID_PARAMETER;
|
||||
final BindingResult result = ((MethodArgumentNotValidException) ex).getBindingResult();
|
||||
if (result.hasErrors()) {
|
||||
extMessage = result.getAllErrors().get(0).getDefaultMessage();
|
||||
} else {
|
||||
extMessage = ex.getMessage();
|
||||
}
|
||||
} else if (ex instanceof HttpRequestMethodNotSupportedException) {
|
||||
error = DefaultError.METHOD_NOT_SUPPORTED;
|
||||
extMessage = ex.getMessage();
|
||||
} else if (ex instanceof UnexpectedTypeException) {
|
||||
error = DefaultError.INVALID_PARAMETER;
|
||||
extMessage = ex.getMessage();
|
||||
} else if (ex instanceof NoHandlerFoundException) {
|
||||
error = DefaultError.SERVICE_NOT_FOUND;
|
||||
extMessage = ex.getMessage();
|
||||
} else {
|
||||
error = DefaultError.SYSTEM_INTERNAL_ERROR;
|
||||
extMessage = ex.getMessage();
|
||||
}
|
||||
|
||||
|
||||
Result response = Result.failure(error);
|
||||
response.setMsg(extMessage);
|
||||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* BusinessException 业务异常处理
|
||||
*
|
||||
* @return ResponseEntity
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ExceptionHandler(MsgException.class)
|
||||
public ResponseEntity handleException(MsgException e) {
|
||||
LOGGER.error("业务异常", e);
|
||||
Result response = Result.failure(DefaultError.BUSINESS_ERROR);
|
||||
response.setMsg(e.getMessage());
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.chaozhanggui.system.cashierservice.exception;
|
||||
|
||||
public interface IError {
|
||||
|
||||
/**
|
||||
* 获取nameSpace
|
||||
*
|
||||
* @return nameSpace
|
||||
*/
|
||||
String getNameSpace();
|
||||
|
||||
/**
|
||||
* 获取错误码
|
||||
|
||||
* @return 错误码
|
||||
*/
|
||||
String getErrorCode();
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
|
||||
* @return 错误信息
|
||||
*/
|
||||
String getErrorMessage();
|
||||
|
||||
/**
|
||||
* 设置错误信息
|
||||
*
|
||||
* @param errorMessage 错误信息
|
||||
*/
|
||||
void setErrorMessage(String errorMessage);
|
||||
}
|
||||
Reference in New Issue
Block a user