Java

背景

软件开发过程中,不可避免的是需要处理各种异常,所以代码中就会出现大量的 try {...} catch {...} finally {...} 代码块,不仅有大量的冗余代码,而且还影响代码的可读性。
另一个就是面对业务异常的情况,经常需要将业务异常结果组装成统一的信息返回给前端进行提示。
假如在每个接口中都去包装异常信息进行返回就会让代码变得很冗余且混乱。在实际项目开发过程中会巧用断言去简化代码。

业务异常处理示例

假设定义的标准接口响应实体为 ApiResult:

  1. @Data
  2. @Builder
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class ApiResult<T> implements Serializable {
  6. private static final long serialVersionUID = 411731814484355577L;
  7. private int responseCode;
  8. private String responseMsg;
  9. private boolean isSuccess;
  10. private T data;
  11. public String toString() {
  12. return "ApiResult(responseCode=" + this.getResponseCode() + ", responseMsg=" + this.getResponseMsg() + ", isSuccess=" + this.isSuccess() + ", data=" + this.getData() + ")";
  13. }
  14. }

那么接口处理业务逻辑时代码就会变成这样,看起来非常多代码:

  1. public ApiResult cancelService(@PathVariable Long serviceOrderId){
  2. ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);
  3. ApiResult result = new ApiResult<>();
  4. if (ObjectUtil.isNull(serviceOrder)) {
  5. result.setSuccess(false);
  6. result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
  7. result.setResponseMsg("查无此服务单");
  8. return result;
  9. }
  10. if(serviceOrder.getOrderStatus().equals(cancelOrderStatus)){
  11. result.setSuccess(false);
  12. result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
  13. result.setResponseMsg("已取消的服务单不允许再次取消");
  14. return result;
  15. }
  16. if(serviceOrder.getSortOrderId() != null){
  17. result.setSuccess(false);
  18. result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
  19. result.setResponseMsg("已配置物料的服务单不允许取消");
  20. return result;
  21. }
  22. // ...other check
  23. // ...do something
  24. return result;
  25. }

然后在上面这个代码基础上,可以观察到,里面其实有非常多的重复代码,完全可以把它们装到 ApiResult 里面。
这也是看到很多开源框架的处理方式(PS:所以第一个自己写的框架也是这么处理的)

在原 ApiResult 实体中增加一些公用的处理方法

  1. public static ApiResult<String> success() {
  2. return success("success");
  3. }
  4. public static <T> ApiResult<T> success(T data) {
  5. return (new ApiResult()).setResponseCode(0).setResponseMsg("操作成功").setSuccess(true).setData(data);
  6. }
  7. public static ApiResult<String> fail() {
  8. return fail(-1);
  9. }
  10. public static ApiResult<String> fail(int code) {
  11. return fail(code, "fail");
  12. }
  13. public static <T> ApiResult<T> fail(T data) {
  14. return fail(-1, data);
  15. }
  16. public static <T> ApiResult<T> fail(int code, T data) {
  17. return (new ApiResult()).setResponseCode(code).setResponseMsg("操作失败").setSuccess(false).setData(data);
  18. }
  19. public static <T> ApiResult<T> success(int code, String message, T data) {
  20. return (new ApiResult()).setResponseCode(code).setResponseMsg(message).setSuccess(true).setData(data);
  21. }
  22. public static <T> ApiResult<T> fail(int code, String message, T data) {
  23. return (new ApiResult()).setResponseCode(code).setResponseMsg(message).setSuccess(false).setData(data);
  24. }

然后业务逻辑处理就变成这样了,看起来还不错是不是:

  1. /**
  2. * 取消服务单(不用断言)
  3. */
  4. public ApiResult cancelService(Long serviceOrderId){
  5. ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);
  6. ApiResult result = new ApiResult<>();
  7. if (ObjectUtil.isNull(serviceOrder)) {
  8. result = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(), "查无此服务单");
  9. return result;
  10. }
  11. if(serviceOrder.getOrderStatus().equals(cancelOrderStatus)){
  12. result = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(), "已取消的服务单不允许再次取消");
  13. return result;
  14. }
  15. if(serviceOrder.getSortOrderId() != null){
  16. result = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(), "已配置物料的服务单不允许取消");
  17. return result;
  18. }
  19. // ...other check
  20. // ...do something
  21. return result;
  22. }

但是可以用异常处理类+断言处理得更加简化。

增加异常处理类

  1. @Slf4j
  2. @ControllerAdvice
  3. public class GlobalExceptionHandler {
  4. @ExceptionHandler(value = BusinessException.class)
  5. @ResponseBody
  6. public ResponseBean businessExceptionHandler(BusinessException e) {
  7. log.info("business error : {}",e.getMessage(),e);
  8. if (e.getCode() == -1) {
  9. return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
  10. }
  11. return ResponseBean.error(e.getCode(), e.getMessage());
  12. }
  13. }

增加异常类 BusinessException

  1. /**
  2. * 业务异常,异常信息会返回到前端展示给用户
  3. *
  4. * @date 2020/12/15 14:18
  5. */
  6. public class BusinessException extends RuntimeException {
  7. private static final long serialVersionUID = -5770538329754222306L;
  8. private int code = 1;
  9. private Level level;
  10. public BusinessException(int code, String message, Throwable cause) {
  11. super(message, cause);
  12. this.code = code;
  13. }
  14. public BusinessException(String message) {
  15. super(message);
  16. }
  17. public BusinessException(Level level, String message) {
  18. super(message);
  19. this.level = level;
  20. }
  21. public BusinessException(Throwable cause) {
  22. super(cause);
  23. }
  24. public BusinessException(int code, String message) {
  25. super(message);
  26. this.code = code;
  27. }
  28. public int getCode() {
  29. return this.code;
  30. }
  31. public final Level getLevel() {
  32. return this.level;
  33. }
  34. }

增加断言工具类 AssertUtil

  1. public class AssertUtil extends cn.com.bluemoon.common.web.exception.AssertUtil {
  2. public AssertUtil() {
  3. }
  4. /**
  5. * 服务调用异常
  6. * @param expression
  7. * @param message
  8. */
  9. public static void isTrueServiceInvoke(boolean expression, String message) {
  10. if (!expression) {
  11. throw new ServiceInvokeException(message);
  12. }
  13. }
  14. /**
  15. * 抛出异常(默认错误1000)
  16. * @param message
  17. */
  18. public static void businessInvalid(String message) {
  19. throw new BusinessException(ApiCode.SERVICE_ERROR.getValue(), message);
  20. }
  21. /**
  22. * 表达式为真即抛出异常(默认错误1000)
  23. *
  24. * @param expression
  25. * @param message
  26. */
  27. public static void businessInvalid(boolean expression, String message) {
  28. if (expression) {
  29. throw new BusinessException(ApiCode.SERVICE_ERROR.getValue(), message);
  30. }
  31. }
  32. /**
  33. * 表达式为真即抛出异常
  34. *
  35. * @param expression
  36. * @param message
  37. */
  38. public static void businessInvalid(boolean expression, int code, String message) {
  39. if (expression) {
  40. throw new BusinessException(code, message);
  41. }
  42. }
  43. }

最后优化的结果:

  1. /**
  2. * 取消服务单
  3. */
  4. public ApiResult cancelService(@PathVariable Long serviceOrderId){
  5. ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);
  6. AssertUtil.businessInvalid(ObjectUtil.isNull(serviceOrder),"查无此服务单");
  7. AssertUtil.businessInvalid(serviceOrder.getOrderStatus().equals(cancelOrderStatus),"查无此服务单");
  8. AssertUtil.businessInvalid(serviceOrder.getSortOrderId() != null,"查无此服务单");
  9. // ...other check
  10. // ...do something
  11. return ApiResult.success();
  12. }

最后可以看到接口由 19 行的业务检查代码简化到了 3 行。这只是单接口的情况下,在业务多且复杂的情况下能节省更多的开发时间,把精力集中在核心业务上。

附上代码

统一异常处理类:

  1. /**
  2. * 统一异常处理
  3. */
  4. @Slf4j
  5. @ControllerAdvice
  6. public class GlobalExceptionHandler {
  7. @ExceptionHandler(value = AssertException.class)
  8. @ResponseBody
  9. public ResponseBean bootExceptionHandler(AssertException e) {
  10. ApiCode apiCode = ApiCode.getObjectByValue(e.getCode());
  11. log.error("business error : {}", e.getMessage(), e);
  12. if (e.getCode() == -1) {
  13. return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
  14. }
  15. return ResponseBean.error(apiCode.getValue(), e.getMessage());
  16. }
  17. @ExceptionHandler(value = com.alibaba.fastjson.JSONException.class)
  18. public ResponseBean alibabaJsonExceptionHandler(com.alibaba.fastjson.JSONException e) {
  19. ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), ApiCode.PARAM_FORMAT_INCORR.getMessage() + e.getMessage(), null);
  20. log.error("1102", e);
  21. return response;
  22. }
  23. @ExceptionHandler(value = JSONException.class)
  24. @ResponseBody
  25. public ResponseBean jsonExceptionHandler(JSONException e) {
  26. ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), ApiCode.PARAM_FORMAT_INCORR.getMessage() + e.getMessage(), null);
  27. log.error(ApiCode.PARAM_FORMAT_INCORR.getValue() + "", e);
  28. return response;
  29. }
  30. @ExceptionHandler(value = JsonParseException.class)
  31. @ResponseBody
  32. public ResponseBean jsonParseExceptionHandler(JsonParseException e) {
  33. ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), String.format(ApiCode.PARAM_FORMAT_INCORR.getMessage() + ":%s", e.getMessage()), null);
  34. log.error(ApiCode.PARAM_FORMAT_INCORR.getValue() + "", e);
  35. return response;
  36. }
  37. @ExceptionHandler(value = Exception.class)
  38. @ResponseBody
  39. public ResponseBean exceptionHandler(Exception e) {
  40. ResponseBean response = new ResponseBean(false, ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage(), null);
  41. log.error(ApiCode.SERVICE_ERROR.getValue() + "", e);
  42. return response;
  43. }
  44. @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
  45. @ResponseBody
  46. public ResponseBean exceptionHandle(MethodArgumentTypeMismatchException e) {
  47. ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), String.format(ApiCode.PARAM_FORMAT_INCORR.getMessage() + ":%s", e.getMessage()), null);
  48. log.error(ApiCode.PARAM_FORMAT_INCORR.getValue() + "", e);
  49. return response;
  50. }
  51. @ExceptionHandler(value = WebException.class)
  52. @ResponseBody
  53. public ResponseBean exceptionHandler(WebException e) {
  54. ResponseBean response = new ResponseBean(e.getIsSuccess(), e.getResponseCode(), e.getResponseMsg(), null);
  55. log.error(e.getResponseCode() + "", e);
  56. return response;
  57. }
  58. @ExceptionHandler(value = IllegalArgumentException.class)
  59. @ResponseBody
  60. public ResponseBean exceptionHandler(IllegalArgumentException e) {
  61. log.error("illegal request : {}", e.getMessage(), e);
  62. return ResponseBean.error(ApiCode.PARAM_INVALID.getValue(), ApiCode.PARAM_INVALID.getMessage());
  63. }
  64. @ExceptionHandler(value = ServiceInvokeException.class)
  65. @ResponseBody
  66. public ResponseBean exceptionHandler(ServiceInvokeException e) {
  67. log.error("serviceInvoke error request : {}", e.getMessage(), e);
  68. return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
  69. }
  70. @ExceptionHandler(value = BusinessException.class)
  71. @ResponseBody
  72. public ResponseBean businessExceptionHandler(BusinessException e) {
  73. log.info("business error : {}",e.getMessage(),e);
  74. if (e.getCode() == -1) {
  75. return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
  76. }
  77. return ResponseBean.error(e.getCode(), e.getMessage());
  78. }
  79. @ResponseBody
  80. @ExceptionHandler(MethodArgumentNotValidException.class)
  81. public ResponseBean exceptionHandler(MethodArgumentNotValidException e) {
  82. log.info("req params error", e);
  83. String message = e.getBindingResult().getFieldError().getDefaultMessage();
  84. if (StringUtils.isNotBlank(message) && !"不能为空".equals(message)) {
  85. return ResponseBean.error(ApiCode.PARAM_INVALID.getValue(), message);
  86. }
  87. return ResponseBean.error(ApiCode.PARAM_INVALID.getValue(), ApiCode.PARAM_INVALID.getMessage());
  88. }
  89. @ExceptionHandler(value = TokenErrorException.class)
  90. @ResponseBody
  91. public ResponseBean tokenErrorExceptionHandler(TokenErrorException e) {
  92. log.info("登录失效 : {}",e.getMessage(),e);
  93. return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), "登录已失效,请重新登录!");
  94. }
  95. @ExceptionHandler(value = ServiceException.class)
  96. @ResponseBody
  97. public ResponseBean businessExceptionHandler(ServiceException e) {
  98. log.info("service error : {}",e.getMessage(),e);
  99. return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), e.getMessage());
  100. }
  101. }

异常情况枚举,仅作参考:

  1. public enum ErrorCodeEnum implements EnumBase{
  2. FAIL(-1, "网络异常,请稍后再试"),
  3. SUCCESS(0, "请求成功"),
  4. MAX_UPLOAD_SIZE_ERROR(1000, "上传文件不能超过20M"),
  5. SERVICE_BUSY_ERROR(1000, "服务器正在繁忙,请稍后再试哦~"),
  6. REQUEST_PARAMS_FAIL(1001, "参数错误"),
  7. USER_NOT_LOGIN(1002, "用户未登录,请重新登录"),
  8. USER_HAS_EXIST_LOGIN(1007, "用户已经存在,请检查!"),
  9. USER_CODE_NOT_EXIST(1008, "用户编码不存在,请检查!"),
  10. REQUEST_PARAMS_FORMAT_ERROR(1102, "请求参数格式异常"),
  11. PASSWORD_SAFETY_ERROE(2204, "密码不符合安全规则,请通过忘记密码重新设置8-18位数字+字母组合密码"),
  12. TOKEN_EXPIRED(2301, "token过期"),
  13. TOKEN_ERROR(2302, "token验证失败"),
  14. INTERFACE_ERROR(10000, "接口服务器异常");
  15. private final int code;
  16. private final String msg;
  17. ErrorCodeEnum(int code, String msg) {
  18. this.code = code;
  19. this.msg = msg;
  20. }
  21. @Override
  22. public int getCode() {
  23. return this.code;
  24. }
  25. @Override
  26. public String getMsg() {
  27. return this.msg;
  28. }
  29. }