Blame view

src/main/java/com/zteits/oa/api/base/exception/AppException.java 2.57 KB
1b9e8898   王富生   提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  package com.zteits.oa.api.base.exception;
  
  import com.zteits.oa.api.base.bean.ErrorCode;
  import com.zteits.oa.api.base.constants.ErrorType;
  
  /**
   * Copyright: Copyright (c) 2017  zteits
   *
   * @ClassName: AppException.java
   * @Description:
   * @version: v1.0.0
   * @author: liuzl
   * @date: 2017424   下午4:51:41
   * Modification History:
   * Date             Author          Version            Description
   * ---------------------------------------------------------*
   * 2017424      liuzl           v1.0.0               创建
   *
   *
   * 应用级异常
   */
  public class AppException extends RuntimeException {
  
      private static final long serialVersionUID = 1L;
      /**
       * 错误编码
       */
      protected ErrorCode errCode;
  
      /**
       * 错误信息
       */
      protected String errMsg;
  
      /**
       * 无参构造函数
       */
      public AppException() {
          super();
      }
  
      public AppException(ErrorCode errCode, String... errMsg) {
          super(errCode.getMsg());
          this.errCode = errCode;
          setErrMsg(errMsg,true);
      }
      
      public AppException(ErrorCode errCode, String errMsg,Boolean isTransfer) {
          super(errMsg);
          this.errCode = errCode;
          setErrMsg(new String[]{errMsg},isTransfer);
      }
      
      public AppException(Throwable cause) {
          super(cause);
          this.errCode = ErrorType.SYSTEM_ERROR;
          this.errMsg = cause.getMessage();
      }
  
      /**
       * 构造函数
       *
       * @param cause 异常
       */
      public AppException(ErrorCode errCode, Throwable cause, String... errMsg) {
          super(errCode.getCode() + errCode.getMsg(), cause);
          this.errCode = errCode;
          setErrMsg(errMsg,true);
      }
  
      public ErrorCode getErrCode() {
          return errCode;
      }
  
      public void setErrCode(ErrorCode errCode) {
          this.errCode = errCode;
      }
  
      public String getErrMsg() {
          return this.errMsg;
      }
  
      public void setErrMsg(String[] errMsg,Boolean isTransfer) {
  
          if (null != errMsg &&errMsg.length>0) {
          	if(errCode.getMsg().contains("%s") && isTransfer){
          		this.errMsg = String.format(errCode.getMsg(), errMsg);
          	}else{
          		StringBuffer sf = new StringBuffer();
          		for (String msg : errMsg) {
  					sf.append(msg+";");
  				}
          		this.errMsg = sf.toString();
          	}
          }else{
          	this.errMsg = errCode.getMsg();
          }
  
      }
  
      public static void main(String[] args) {
          String str = "ERRCode:1004--对象不存在:[%s]";
          if (str.contains("%s")){
           System.out.println("包含");
          }
      }
  
  }