OSDN Git Service

adjust structure
[bytom/bytom-java-sdk.git] / java-sdk / src / main / java / io / bytom / exception / APIException.java
1 package io.bytom.exception;
2
3 import com.google.gson.annotations.SerializedName;
4
5 /**
6  * APIException wraps errors returned by the API.
7  * Each error contains a brief description in addition to a unique error code.<br>
8  * The error code can be used by Chain Support to diagnose the exact cause of the error.
9  * The mapping of error codes to messages is as follows:<br><br>
10  *
11  * <h2>General errors</h2>
12  * CH001 - Request timed out
13  * CH002 - Not found
14  * CH003 - Invalid request body
15  * CH004 - Invalid request header
16  * CH006 - Not found
17  *
18  * <h2>Account/Asset errors</h2>
19  * CH200 - Quorum must be greater than 1 and less than or equal to the length of xpubs
20  * CH201 - Invalid xpub format
21  * CH202 - At least one xpub is required
22  * CH203 - Retrieved type does not match expected type
23  *
24  * <h2>Access token errors</h2>
25  * CH300 - Malformed or empty access token id
26  * CH301 - Access tokens must be type client or network
27  * CH302 - Access token id is already in use
28  * CH310 - The access token used to authenticate this request cannot be deleted
29  *
30  * <h2>Query errors</h2>
31  * CH600 - Malformed pagination parameter `after`
32  * CH601 - Incorrect number of parameters to filter
33  * CH602 - Malformed query filter
34  *
35  * <h2>Transaction errors</h2>
36  * CH700 - Reference data does not match previous transaction's reference data<br>
37  * CH701 - Invalid action type<br>
38  * CH702 - Invalid alias on action<br>
39  * CH730 - Missing raw transaction<br>
40  * CH731 - Too many signing instructions in template for transaction<br>
41  * CH732 - Invalid transaction input index<br>
42  * CH733 - Invalid signature script component<br>
43  * CH734 - Missing signature in template<br>
44  * CH735 - Transaction rejected<br>
45  * CH760 - Insufficient funds for tx<br>
46  * CH761 - Some outputs are reserved; try again<br>
47  */
48 public class APIException extends BytomException {
49   /**
50    * Unique identifier for the error.
51    */
52   public String code;
53
54   /**
55    * Message describing the general nature of the error.
56    */
57   @SerializedName("message")
58   public String chainMessage;
59
60   /**
61    * Additional information about the error (possibly null).
62    */
63   public String detail;
64
65   /**
66    * Specifies whether the error is temporary, or a change to the request is necessary.
67    */
68   public boolean temporary;
69
70   /**
71    * Unique identifier of the request to the server.
72    */
73   public String requestId;
74
75   /**
76    * HTTP status code returned by the server.
77    */
78   public int statusCode;
79
80   /**
81    * Initializes exception with its message and requestId attributes.
82    * @param message error message
83    * @param requestId unique identifier of the request
84    */
85   public APIException(String message, String requestId) {
86     super(message);
87     this.requestId = requestId;
88   }
89
90   /**
91    * Intitializes exception with its code, message, detail &amp; temporary field set.
92    * @param code error code
93    * @param message error message
94    * @param detail additional error information
95    * @param temporary unique identifier of the request
96    */
97   public APIException(String code, String message, String detail, boolean temporary) {
98     super(message);
99     this.chainMessage = message;
100     this.code = code;
101     this.detail = detail;
102     this.temporary = temporary;
103   }
104
105   /**
106    * Initializes exception with its code, message, detail &amp; requestId attributes.
107    * @param code error code
108    * @param message error message
109    * @param detail additional error information
110    * @param requestId unique identifier of the request
111    */
112   public APIException(String code, String message, String detail, String requestId) {
113     super(message);
114     this.chainMessage = message;
115     this.code = code;
116     this.detail = detail;
117     this.requestId = requestId;
118   }
119
120   /**
121    * Initializes exception with all of its attributes.
122    * @param code error code
123    * @param message error message
124    * @param detail additional error information
125    * @param temporary specifies if the error is temporary
126    * @param requestId unique identifier of the request
127    * @param statusCode HTTP status code
128    */
129   public APIException(
130       String code,
131       String message,
132       String detail,
133       boolean temporary,
134       String requestId,
135       int statusCode) {
136     super(message);
137     this.chainMessage = message;
138     this.code = code;
139     this.detail = detail;
140     this.temporary = temporary;
141     this.requestId = requestId;
142     this.statusCode = statusCode;
143   }
144
145   /**
146    * Constructs a detailed message of the error.
147    * @return detailed error message
148    */
149   @Override
150   public String getMessage() {
151     String s = "";
152
153     if (this.code != null && this.code.length() > 0) {
154       s += "Code: " + this.code + " ";
155     }
156
157     s += "Message: " + this.chainMessage;
158
159     if (this.detail != null && this.detail.length() > 0) {
160       s += " Detail: " + this.detail;
161     }
162
163     if (this.requestId != null) {
164       s += " Request-ID: " + this.requestId;
165     }
166
167     return s;
168   }
169 }