@@ -32,9 +32,10 @@ public final class ErrorTest extends Requestor {
32
32
33
33
/**
34
34
* Set up the testing environment for this file.
35
+ *
35
36
* @throws MissingParameterError
36
37
*
37
- * @throws EasyPostException when the request fails.
38
+ * @throws EasyPostException when the request fails.
38
39
*/
39
40
@ BeforeAll
40
41
public static void setup () throws MissingParameterError {
@@ -49,7 +50,7 @@ public static void setup() throws MissingParameterError {
49
50
@ Test
50
51
public void testError () throws EasyPostException {
51
52
vcr .setUpTest ("error" );
52
-
53
+
53
54
APIException exception = assertThrows (InvalidRequestError .class , () -> vcr .client .shipment .create (null ));
54
55
55
56
assertEquals (422 , exception .getStatusCode ());
@@ -67,31 +68,31 @@ public void testError() throws EasyPostException {
67
68
@ Test
68
69
public void testKnownApiException () throws EasyPostException {
69
70
HashMap <Integer , Class <?>> apiErrorsMap = new HashMap <Integer , Class <?>>();
70
- apiErrorsMap .put (300 , RedirectError .class );
71
- apiErrorsMap .put (301 , RedirectError .class );
72
- apiErrorsMap .put (302 , RedirectError .class );
73
- apiErrorsMap .put (303 , RedirectError .class );
74
- apiErrorsMap .put (304 , RedirectError .class );
75
- apiErrorsMap .put (305 , RedirectError .class );
76
- apiErrorsMap .put (306 , RedirectError .class );
77
- apiErrorsMap .put (307 , RedirectError .class );
78
- apiErrorsMap .put (308 , RedirectError .class );
79
- apiErrorsMap .put (401 , UnauthorizedError .class );
80
- apiErrorsMap .put (402 , PaymentError .class );
81
- apiErrorsMap .put (403 , ForbiddenError .class );
82
- apiErrorsMap .put (404 , NotFoundError .class );
83
- apiErrorsMap .put (405 , MethodNotAllowedError .class );
84
- apiErrorsMap .put (408 , TimeoutError .class );
85
- apiErrorsMap .put (422 , InvalidRequestError .class );
86
- apiErrorsMap .put (429 , RateLimitError .class );
87
- apiErrorsMap .put (444 , UnknownApiError .class );
88
- apiErrorsMap .put (500 , InternalServerError .class );
89
- apiErrorsMap .put (503 , ServiceUnavailableError .class );
90
- apiErrorsMap .put (504 , GatewayTimeoutError .class );
91
-
92
- for (Map .Entry <Integer , Class <?>> entry : apiErrorsMap .entrySet ()) {
71
+ apiErrorsMap .put (300 , RedirectError .class );
72
+ apiErrorsMap .put (301 , RedirectError .class );
73
+ apiErrorsMap .put (302 , RedirectError .class );
74
+ apiErrorsMap .put (303 , RedirectError .class );
75
+ apiErrorsMap .put (304 , RedirectError .class );
76
+ apiErrorsMap .put (305 , RedirectError .class );
77
+ apiErrorsMap .put (306 , RedirectError .class );
78
+ apiErrorsMap .put (307 , RedirectError .class );
79
+ apiErrorsMap .put (308 , RedirectError .class );
80
+ apiErrorsMap .put (401 , UnauthorizedError .class );
81
+ apiErrorsMap .put (402 , PaymentError .class );
82
+ apiErrorsMap .put (403 , ForbiddenError .class );
83
+ apiErrorsMap .put (404 , NotFoundError .class );
84
+ apiErrorsMap .put (405 , MethodNotAllowedError .class );
85
+ apiErrorsMap .put (408 , TimeoutError .class );
86
+ apiErrorsMap .put (422 , InvalidRequestError .class );
87
+ apiErrorsMap .put (429 , RateLimitError .class );
88
+ apiErrorsMap .put (444 , UnknownApiError .class );
89
+ apiErrorsMap .put (500 , InternalServerError .class );
90
+ apiErrorsMap .put (503 , ServiceUnavailableError .class );
91
+ apiErrorsMap .put (504 , GatewayTimeoutError .class );
92
+
93
+ for (Map .Entry <Integer , Class <?>> entry : apiErrorsMap .entrySet ()) {
93
94
APIException exception = assertThrows (APIException .class ,
94
- () -> handleAPIError ("{}" , entry .getKey ()));
95
+ () -> handleAPIError ("{}" , entry .getKey ()));
95
96
96
97
assertEquals (Constants .ErrorMessages .API_DID_NOT_RETURN_ERROR_DETAILS , exception .getMessage ());
97
98
assertEquals ("NO RESPONSE CODE" , exception .getCode ());
@@ -107,10 +108,15 @@ public void testKnownApiException() throws EasyPostException {
107
108
*/
108
109
@ Test
109
110
public void testExceptionErrorMessageParsing () throws EasyPostException {
110
- String errorMessageStringJson =
111
- "{\" error\" : {\" code\" : \" ERROR_CODE\" , \" message\" : \" ERROR_MESSAGE_1\" , \" errors\" : []}}" ;
111
+ String errorMessageStringJson = "{\n " +
112
+ " \" error\" : {\n " +
113
+ " \" code\" : \" ERROR_CODE\" ,\n " +
114
+ " \" message\" : \" ERROR_MESSAGE_1\" ,\n " +
115
+ " \" errors\" : []\n " +
116
+ " }\n " +
117
+ "}" ;
112
118
EasyPostException exception = assertThrows (EasyPostException .class ,
113
- () -> handleAPIError (errorMessageStringJson , 400 ));
119
+ () -> handleAPIError (errorMessageStringJson , 400 ));
114
120
115
121
assertEquals ("ERROR_MESSAGE_1" , exception .getMessage ());
116
122
}
@@ -122,12 +128,79 @@ public void testExceptionErrorMessageParsing() throws EasyPostException {
122
128
*/
123
129
@ Test
124
130
public void testExceptionErrorArrayParsing () throws EasyPostException {
125
- String errorMessageArrayJson = "{\" error\" : {\" code\" : \" ERROR_CODE\" , \" message\" :" +
126
- "[\" ERROR_MESSAGE_1\" , \" ERROR_MESSAGE_2\" ], \" errors\" : []}}" ;
127
-
131
+ String errorMessageArrayJson = "{\n " +
132
+ " \" error\" : {\n " +
133
+ " \" code\" : \" ERROR_CODE\" ,\n " +
134
+ " \" message\" : [\n " +
135
+ " \" ERROR_MESSAGE_1\" ,\n " +
136
+ " \" ERROR_MESSAGE_2\" \n " +
137
+ " ],\n " +
138
+ " \" errors\" : []\n " +
139
+ " }\n " +
140
+ "}" ;
128
141
EasyPostException exception = assertThrows (EasyPostException .class ,
129
- () -> handleAPIError (errorMessageArrayJson , 400 ));
142
+ () -> handleAPIError (errorMessageArrayJson , 400 ));
130
143
131
144
assertEquals ("ERROR_MESSAGE_1, ERROR_MESSAGE_2" , exception .getMessage ());
132
145
}
146
+
147
+ /**
148
+ * Test parsing error message that is an object.
149
+ *
150
+ * @throws EasyPostException
151
+ */
152
+ @ Test
153
+ public void testExceptionErrorObjectParsing () throws EasyPostException {
154
+ String errorMessageObjectJson = "{\n " +
155
+ " \" error\" : {\n " +
156
+ " \" code\" : \" UNPROCESSABLE_ENTITY\" ,\n " +
157
+ " \" message\" : {\n " +
158
+ " \" errors\" : [\n " +
159
+ " \" bad error.\" ,\n " +
160
+ " \" second bad error.\" \n " +
161
+ " ]\n " +
162
+ " },\n " +
163
+ " \" errors\" : []\n " +
164
+ " }\n " +
165
+ "}" ;
166
+
167
+ EasyPostException exception = assertThrows (EasyPostException .class ,
168
+ () -> handleAPIError (errorMessageObjectJson , 400 ));
169
+
170
+ assertEquals ("bad error., second bad error." , exception .getMessage ());
171
+ }
172
+
173
+ /**
174
+ * Test parsing error message that has really bad format.
175
+ *
176
+ * @throws EasyPostException
177
+ */
178
+ @ Test
179
+ public void testExceptionErrorEdgeCaseParsing () throws EasyPostException {
180
+ String json = "{\n " +
181
+ " \" error\" : {\n " +
182
+ " \" code\" : \" UNPROCESSABLE_ENTITY\" ,\n " +
183
+ " \" message\" : {\n " +
184
+ " \" errors\" : [\n " +
185
+ " \" Bad format 1\" ,\n " +
186
+ " \" Bad format 2\" \n " +
187
+ " ],\n " +
188
+ " \" bad_data\" : [\n " +
189
+ " {\n " +
190
+ " \" first_message\" : \" Bad format 3\" ,\n " +
191
+ " \" second_message\" : \" Bad format 4\" ,\n " +
192
+ " \" thrid_message\" : \" Bad format 5\" \n " +
193
+ " }\n " +
194
+ " ]\n " +
195
+ " },\n " +
196
+ " \" errors\" : []\n " +
197
+ " }\n " +
198
+ "}" ;
199
+
200
+ EasyPostException exception = assertThrows (EasyPostException .class ,
201
+ () -> handleAPIError (json , 400 ));
202
+
203
+ assertEquals ("Bad format 1, Bad format 2, Bad format 3, Bad format 4, Bad format 5" ,
204
+ exception .getMessage ());
205
+ }
133
206
}
0 commit comments