22
33import com .auth0 .client .MockServer ;
44import com .auth0 .exception .Auth0Exception ;
5- import com .auth0 .exception .AuthAPIException ;
5+ import com .auth0 .exception .APIException ;
66import com .auth0 .json .auth .TokenHolder ;
77import com .fasterxml .jackson .core .JsonParseException ;
88import com .fasterxml .jackson .core .JsonProcessingException ;
@@ -163,10 +163,10 @@ public void shouldThrowOnParseInvalidSuccessfulResponse() throws Exception {
163163 exception = e ;
164164 }
165165 assertThat (exception , is (notNullValue ()));
166- assertThat (exception , is (instanceOf (AuthAPIException .class )));
166+ assertThat (exception , is (instanceOf (APIException .class )));
167167 assertThat (exception .getCause (), is (instanceOf (JsonMappingException .class )));
168- assertThat (exception .getMessage (), is ("Authentication failed with status code 200: Failed to parse json body" ));
169- AuthAPIException authException = (AuthAPIException ) exception ;
168+ assertThat (exception .getMessage (), is ("Request failed with status code 200: Failed to parse json body" ));
169+ APIException authException = (APIException ) exception ;
170170 assertThat (authException .getDescription (), is ("Failed to parse json body" ));
171171 assertThat (authException .getError (), is (nullValue ()));
172172 assertThat (authException .getStatusCode (), is (200 ));
@@ -184,10 +184,10 @@ public void shouldParseJSONErrorResponseWithErrorDescription() throws Exception
184184 exception = e ;
185185 }
186186 assertThat (exception , is (notNullValue ()));
187- assertThat (exception , is (instanceOf (AuthAPIException .class )));
187+ assertThat (exception , is (instanceOf (APIException .class )));
188188 assertThat (exception .getCause (), is (nullValue ()));
189- assertThat (exception .getMessage (), is ("Authentication failed with status code 400: the connection was not found" ));
190- AuthAPIException authException = (AuthAPIException ) exception ;
189+ assertThat (exception .getMessage (), is ("Request failed with status code 400: the connection was not found" ));
190+ APIException authException = (APIException ) exception ;
191191 assertThat (authException .getDescription (), is ("the connection was not found" ));
192192 assertThat (authException .getError (), is ("invalid_request" ));
193193 assertThat (authException .getStatusCode (), is (400 ));
@@ -205,10 +205,10 @@ public void shouldParseJSONErrorResponseWithError() throws Exception {
205205 exception = e ;
206206 }
207207 assertThat (exception , is (notNullValue ()));
208- assertThat (exception , is (instanceOf (AuthAPIException .class )));
208+ assertThat (exception , is (instanceOf (APIException .class )));
209209 assertThat (exception .getCause (), is (nullValue ()));
210- assertThat (exception .getMessage (), is ("Authentication failed with status code 400: missing username for Username-Password-Authentication connection with requires_username enabled" ));
211- AuthAPIException authException = (AuthAPIException ) exception ;
210+ assertThat (exception .getMessage (), is ("Request failed with status code 400: missing username for Username-Password-Authentication connection with requires_username enabled" ));
211+ APIException authException = (APIException ) exception ;
212212 assertThat (authException .getDescription (), is ("missing username for Username-Password-Authentication connection with requires_username enabled" ));
213213 assertThat (authException .getError (), is ("missing username for Username-Password-Authentication connection with requires_username enabled" ));
214214 assertThat (authException .getStatusCode (), is (400 ));
@@ -226,15 +226,36 @@ public void shouldParseJSONErrorResponseWithDescription() throws Exception {
226226 exception = e ;
227227 }
228228 assertThat (exception , is (notNullValue ()));
229- assertThat (exception , is (instanceOf (AuthAPIException .class )));
229+ assertThat (exception , is (instanceOf (APIException .class )));
230230 assertThat (exception .getCause (), is (nullValue ()));
231- assertThat (exception .getMessage (), is ("Authentication failed with status code 400: The user already exists." ));
232- AuthAPIException authException = (AuthAPIException ) exception ;
231+ assertThat (exception .getMessage (), is ("Request failed with status code 400: The user already exists." ));
232+ APIException authException = (APIException ) exception ;
233233 assertThat (authException .getDescription (), is ("The user already exists." ));
234234 assertThat (authException .getError (), is ("user_exists" ));
235235 assertThat (authException .getStatusCode (), is (400 ));
236236 }
237237
238+ @ Test
239+ public void shouldParseJSONErrorResponseWithMessage () throws Exception {
240+ CustomRequest <List > request = new CustomRequest <>(client , server .getBaseUrl (), "GET" , listType );
241+ server .jsonResponse (MGMT_ERROR_WITH_MESSAGE , 400 );
242+ Exception exception = null ;
243+ try {
244+ request .execute ();
245+ server .takeRequest ();
246+ } catch (Exception e ) {
247+ exception = e ;
248+ }
249+ assertThat (exception , is (notNullValue ()));
250+ assertThat (exception , is (instanceOf (APIException .class )));
251+ assertThat (exception .getCause (), is (nullValue ()));
252+ assertThat (exception .getMessage (), is ("Request failed with status code 400: Query validation error: 'String 'invalid_field' does not match pattern. Must be a comma separated list of the following values: allowed_logout_urls,change_password." ));
253+ APIException authException = (APIException ) exception ;
254+ assertThat (authException .getDescription (), is ("Query validation error: 'String 'invalid_field' does not match pattern. Must be a comma separated list of the following values: allowed_logout_urls,change_password." ));
255+ assertThat (authException .getError (), is ("invalid_query_string" ));
256+ assertThat (authException .getStatusCode (), is (400 ));
257+ }
258+
238259 @ Test
239260 public void shouldParsePlainTextErrorResponse () throws Exception {
240261 CustomRequest <List > request = new CustomRequest <>(client , server .getBaseUrl (), "GET" , listType );
@@ -247,10 +268,10 @@ public void shouldParsePlainTextErrorResponse() throws Exception {
247268 exception = e ;
248269 }
249270 assertThat (exception , is (notNullValue ()));
250- assertThat (exception , is (instanceOf (AuthAPIException .class )));
271+ assertThat (exception , is (instanceOf (APIException .class )));
251272 assertThat (exception .getCause (), is (instanceOf (JsonParseException .class )));
252- assertThat (exception .getMessage (), is ("Authentication failed with status code 400: A plain-text error response" ));
253- AuthAPIException authException = (AuthAPIException ) exception ;
273+ assertThat (exception .getMessage (), is ("Request failed with status code 400: A plain-text error response" ));
274+ APIException authException = (APIException ) exception ;
254275 assertThat (authException .getDescription (), is ("A plain-text error response" ));
255276 assertThat (authException .getError (), is (nullValue ()));
256277 assertThat (authException .getStatusCode (), is (400 ));
0 commit comments