Skip to content

Commit 70ef798

Browse files
committed
rename AuthAPIException. Handle Mgmt exceptions too
1 parent 709432b commit 70ef798

9 files changed

Lines changed: 172 additions & 117 deletions

File tree

README.md

Lines changed: 90 additions & 90 deletions
Large diffs are not rendered by default.

src/main/java/com/auth0/exception/AuthAPIException.java renamed to src/main/java/com/auth0/exception/APIException.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
import java.util.Map;
44

5-
public class AuthAPIException extends Auth0Exception {
5+
public class APIException extends Auth0Exception {
66

77
private String error;
88
private String description;
99
private int statusCode;
1010

11-
public AuthAPIException(String payload, int statusCode, Throwable cause) {
11+
public APIException(String payload, int statusCode, Throwable cause) {
1212
super(createMessage(payload, statusCode), cause);
1313
this.description = payload;
1414
this.statusCode = statusCode;
1515
}
1616

17-
public AuthAPIException(Map<String, Object> values, int statusCode) {
17+
public APIException(Map<String, Object> values, int statusCode) {
1818
super(createMessage(obtainExceptionMessage(values), statusCode));
19-
this.error = (String) (values.containsKey("error") ? values.get("error") : values.get("code"));
19+
this.error = obtainExceptionError(values);
2020
this.description = obtainExceptionMessage(values);
2121
this.statusCode = statusCode;
2222
}
@@ -34,6 +34,19 @@ private static String obtainExceptionMessage(Map<String, Object> values) {
3434
if (values.containsKey("error")) {
3535
return (String) values.get("error");
3636
}
37+
return "Unknown exception";
38+
}
39+
40+
private static String obtainExceptionError(Map<String, Object> values) {
41+
if (values.containsKey("errorCode")) {
42+
return (String) values.get("errorCode");
43+
}
44+
if (values.containsKey("error")) {
45+
return (String) values.get("error");
46+
}
47+
if (values.containsKey("code")) {
48+
return (String) values.get("code");
49+
}
3750
return "Unknown error";
3851
}
3952

@@ -50,6 +63,6 @@ public String getDescription() {
5063
}
5164

5265
private static String createMessage(String description, int statusCode) {
53-
return String.format("Authentication failed with status code %d: %s", statusCode, description);
66+
return String.format("Request failed with status code %d: %s", statusCode, description);
5467
}
5568
}

src/main/java/com/auth0/net/CustomRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.auth0.net;
22

33
import com.auth0.exception.Auth0Exception;
4-
import com.auth0.exception.AuthAPIException;
4+
import com.auth0.exception.APIException;
55
import com.fasterxml.jackson.core.JsonProcessingException;
66
import com.fasterxml.jackson.core.type.TypeReference;
77
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -61,7 +61,7 @@ protected T parseResponse(Response response) throws Auth0Exception {
6161
payload = response.body().string();
6262
return mapper.readValue(payload, tType);
6363
} catch (IOException e) {
64-
throw new AuthAPIException("Failed to parse json body", response.code(), e);
64+
throw new APIException("Failed to parse json body", response.code(), e);
6565
}
6666
}
6767

@@ -98,9 +98,9 @@ protected Auth0Exception createResponseException(Response response) {
9898
payload = response.body().string();
9999
MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
100100
Map<String, Object> values = mapper.readValue(payload, mapType);
101-
return new AuthAPIException(values, response.code());
101+
return new APIException(values, response.code());
102102
} catch (IOException e) {
103-
return new AuthAPIException(payload, response.code(), e);
103+
return new APIException(payload, response.code(), e);
104104
}
105105
}
106106
}

src/main/java/com/auth0/net/Request.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.auth0.net;
22

33
import com.auth0.exception.Auth0Exception;
4-
import com.auth0.exception.AuthAPIException;
4+
import com.auth0.exception.APIException;
55

66
public interface Request<T> {
77

88
/**
99
* Executes this request.
1010
*
1111
* @return the response body JSON decoded as T
12-
* @throws AuthAPIException if the request was executed but the response wasn't successful.
12+
* @throws APIException if the request was executed but the response wasn't successful.
1313
* @throws Auth0Exception if the request couldn't be created or executed successfully.
1414
*/
1515
T execute() throws Auth0Exception;

src/test/java/com/auth0/client/MockServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class MockServer {
2323
public static final String AUTH_ERROR_WITH_ERROR = "src/test/resources/auth/error_with_error.json";
2424
public static final String AUTH_ERROR_WITH_DESCRIPTION = "src/test/resources/auth/error_with_description.json";
2525
public static final String AUTH_ERROR_PLAINTEXT = "src/test/resources/auth/error_plaintext.json";
26+
public static final String MGMT_ERROR_WITH_MESSAGE = "src/test/resources/mgmt/error_with_message.json";
2627
public static final String MGMT_CLIENT_GRANTS_LIST = "src/test/resources/mgmt/client_grants_list.json";
2728
public static final String MGMT_CLIENT_GRANT = "src/test/resources/mgmt/client_grant.json";
2829
public static final String MGMT_CLIENTS_LIST = "src/test/resources/mgmt/clients_list.json";

src/test/java/com/auth0/client/auth/AuthAPITest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public void shouldAcceptDomainWithHttpScheme() throws Exception {
6565
assertThat(api.getBaseUrl(), isUrl("http", "me.something.com"));
6666
}
6767

68+
@Test
69+
public void shouldThrowWhenDomainIsInvalid() throws Exception {
70+
exception.expect(IllegalArgumentException.class);
71+
exception.expectMessage("The domain had an invalid format and couldn't be parsed as an URL.");
72+
new AuthAPI("", CLIENT_ID, CLIENT_SECRET);
73+
}
74+
6875
@Test
6976
public void shouldThrowWhenDomainIsNull() throws Exception {
7077
exception.expect(IllegalArgumentException.class);

src/test/java/com/auth0/client/mgmt/MgmtAPITest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public void shouldAcceptDomainWithHttpScheme() throws Exception {
4747
assertThat(api.getBaseUrl(), isUrl("http", "me.something.com"));
4848
}
4949

50+
@Test
51+
public void shouldThrowWhenDomainIsInvalid() throws Exception {
52+
exception.expect(IllegalArgumentException.class);
53+
exception.expectMessage("The domain had an invalid format and couldn't be parsed as an URL.");
54+
new MgmtAPI("", API_TOKEN);
55+
}
56+
5057
@Test
5158
public void shouldThrowWhenDomainIsNull() throws Exception {
5259
exception.expect(IllegalArgumentException.class);

src/test/java/com/auth0/net/CustomRequestTest.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.auth0.client.MockServer;
44
import com.auth0.exception.Auth0Exception;
5-
import com.auth0.exception.AuthAPIException;
5+
import com.auth0.exception.APIException;
66
import com.auth0.json.auth.TokenHolder;
77
import com.fasterxml.jackson.core.JsonParseException;
88
import 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));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"statusCode": 400,
3+
"error": "Bad Request",
4+
"message": "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.",
5+
"errorCode": "invalid_query_string"
6+
}

0 commit comments

Comments
 (0)