作者 王勇

简单封装自定义异常,未完善

  1 +package com.sunyo.wlpt.message.bus.service.exception;
  2 +
  3 +/**
  4 + * @author 子诚
  5 + * Description:
  6 + * 时间:2020/7/17 16:43
  7 + */
  8 +public class CustomException extends RuntimeException {
  9 + private static final long serialVersionUID = 6098063244016154220L;
  10 +
  11 + /**
  12 + * 异常错误编码
  13 + */
  14 + private String code;
  15 +
  16 + /**
  17 + * 异常信息
  18 + */
  19 + private String message;
  20 +
  21 + public CustomException(CustomExceptionType exceptionTypeEnum, String message) {
  22 + this.code = exceptionTypeEnum.getCode();
  23 + this.message = message;
  24 + }
  25 +
  26 + public String getCode() {
  27 + return code;
  28 + }
  29 +
  30 + @Override
  31 + public String getMessage() {
  32 + return message;
  33 + }
  34 +
  35 +}
  1 +package com.sunyo.wlpt.message.bus.service.exception;
  2 +
  3 +/**
  4 + * @author 子诚
  5 + * Description:枚举,定制异常类型
  6 + * 时间:2020/7/17 16:27
  7 + */
  8 +
  9 +public enum CustomExceptionType {
  10 +
  11 + CLIENT_ERROR("400", "客户端异常"),
  12 + SYSTEM_ERROR("500", "系统服务异常"),
  13 + OTHER_ERROR("999", "其他未知异常");
  14 +
  15 + /**
  16 + * 响应业务状态
  17 + */
  18 + private String code;
  19 + /**
  20 + * 响应消息
  21 + */
  22 + private String msg;
  23 +
  24 + CustomExceptionType(String code, String msg) {
  25 + this.code = code;
  26 + this.msg = msg;
  27 + }
  28 +
  29 + public String getCode() {
  30 + return code;
  31 + }
  32 +
  33 + public String getMsg() {
  34 + return msg;
  35 + }
  36 +
  37 +}
  1 +package com.sunyo.wlpt.message.bus.service.exception;
  2 +
  3 +import org.slf4j.Logger;
  4 +import org.slf4j.LoggerFactory;
  5 +import org.springframework.web.bind.annotation.ControllerAdvice;
  6 +
  7 +/**
  8 + * @author 子诚
  9 + * Description:自定义全局异常处理类
  10 + * 时间:2020/7/17 17:44
  11 + */
  12 +@ControllerAdvice
  13 +public class GlobalExceptionHandler {
  14 + private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  15 +
  16 +}
1 package com.sunyo.wlpt.message.bus.service.response; 1 package com.sunyo.wlpt.message.bus.service.response;
2 2
  3 +import com.sunyo.wlpt.message.bus.service.exception.CustomException;
  4 +import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
3 import lombok.Data; 5 import lombok.Data;
4 6
5 import java.io.Serializable; 7 import java.io.Serializable;
@@ -46,25 +48,76 @@ public class ResultJson<T> implements Serializable { @@ -46,25 +48,76 @@ public class ResultJson<T> implements Serializable {
46 } 48 }
47 49
48 /** 50 /**
49 - * 有参,构造方法;  
50 - * 重载 51 + * 定义有参构造器
  52 + *
  53 + * @param code 响应状态
  54 + * @param msg 响应消息
51 */ 55 */
52 - public ResultJson(String code) {  
53 - this.code = code;  
54 - }  
55 -  
56 - public ResultJson(T data) {  
57 - this.data = data;  
58 - }  
59 -  
60 public ResultJson(String code, String msg) { 56 public ResultJson(String code, String msg) {
61 this.code = code; 57 this.code = code;
62 this.msg = msg; 58 this.msg = msg;
63 } 59 }
64 60
  61 + /**
  62 + * 定义有参构造器
  63 + *
  64 + * @param code 响应状态
  65 + * @param msg 响应消息
  66 + * @param data 响应数据
  67 + */
65 public ResultJson(String code, String msg, T data) { 68 public ResultJson(String code, String msg, T data) {
66 this.code = code; 69 this.code = code;
67 this.msg = msg; 70 this.msg = msg;
68 this.data = data; 71 this.data = data;
69 } 72 }
  73 +
  74 + /**
  75 + * 定义静态、成功方法(重载)
  76 + *
  77 + * @return 成功(没有响应数据)
  78 + */
  79 + public static ResultJson success() {
  80 + return new ResultJson<>("200", "success");
  81 + }
  82 +
  83 + /**
  84 + * 定义静态、成功方法(重载)
  85 + *
  86 + * @return 成功(响应数据)
  87 + */
  88 + public static ResultJson success(Object data) {
  89 + return new ResultJson<>("200", "success", data);
  90 + }
  91 +
  92 + /**
  93 + * 请求出现异常时的响应数据封装
  94 + *
  95 + * @param e 自定义异常类
  96 + * @return 返回异常信息
  97 + */
  98 + public static ResultJson error(CustomException e) {
  99 + ResultJson result = new ResultJson<>();
  100 + result.setCode(e.getCode());
  101 + if (e.getCode() == CustomExceptionType.CLIENT_ERROR.getCode()) {
  102 + result.setMsg(e.getMessage());
  103 + } else if (e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()) {
  104 + result.setMsg(e.getMessage() + ";请将该异常发送给管理员");
  105 + } else {
  106 + result.setMsg("系统出现未知异常,请联系管理员!");
  107 + }
  108 + // 可以尝试着做异常信息持久化
  109 + return result;
  110 + }
  111 +
  112 +
  113 + /**
  114 + * 请求出现异常时的响应数据封装
  115 + *
  116 + * @param customExceptionType 自定义异常枚举中的异常
  117 + * @param errorMessage 错误信息
  118 + * @return 返回异常信息
  119 + */
  120 + public static ResultJson error(CustomExceptionType customExceptionType, String errorMessage) {
  121 + return new ResultJson<>(customExceptionType.getCode(), errorMessage);
  122 + }
70 } 123 }