[KEYCLOAK-5703] - Improving exception handling and parsing server response
This commit is contained in:
parent
4c2ddfdf54
commit
4071dd7734
6 changed files with 93 additions and 44 deletions
|
@ -21,7 +21,12 @@ package org.keycloak.authorization.client;
|
|||
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
||||
*/
|
||||
public class AuthorizationDeniedException extends RuntimeException {
|
||||
|
||||
public AuthorizationDeniedException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public AuthorizationDeniedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,11 +18,11 @@
|
|||
package org.keycloak.authorization.client.resource;
|
||||
|
||||
|
||||
import org.keycloak.authorization.client.AuthorizationDeniedException;
|
||||
import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException;
|
||||
|
||||
import org.keycloak.authorization.client.representation.AuthorizationRequest;
|
||||
import org.keycloak.authorization.client.representation.AuthorizationResponse;
|
||||
import org.keycloak.authorization.client.util.Http;
|
||||
import org.keycloak.authorization.client.util.HttpResponseException;
|
||||
import org.keycloak.util.JsonSerialization;
|
||||
|
||||
/**
|
||||
|
@ -44,13 +44,8 @@ public class AuthorizationResource {
|
|||
.authorizationBearer(this.accessToken)
|
||||
.json(JsonSerialization.writeValueAsBytes(request))
|
||||
.response().json(AuthorizationResponse.class).execute();
|
||||
} catch (HttpResponseException e) {
|
||||
if (403 == e.getStatusCode()) {
|
||||
throw new AuthorizationDeniedException(e);
|
||||
}
|
||||
throw new RuntimeException("Failed to obtain authorization data.", e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to obtain authorization data.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Failed to obtain authorization data", cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package org.keycloak.authorization.client.resource;
|
||||
|
||||
import org.keycloak.authorization.client.AuthorizationDeniedException;
|
||||
import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException;
|
||||
|
||||
import org.keycloak.authorization.client.representation.EntitlementRequest;
|
||||
import org.keycloak.authorization.client.representation.EntitlementResponse;
|
||||
import org.keycloak.authorization.client.util.Http;
|
||||
import org.keycloak.authorization.client.util.HttpResponseException;
|
||||
import org.keycloak.util.JsonSerialization;
|
||||
|
||||
/**
|
||||
|
@ -25,13 +25,8 @@ public class EntitlementResource {
|
|||
return this.http.<EntitlementResponse>get("/authz/entitlement/" + resourceServerId)
|
||||
.authorizationBearer(eat)
|
||||
.response().json(EntitlementResponse.class).execute();
|
||||
} catch (HttpResponseException e) {
|
||||
if (403 == e.getStatusCode()) {
|
||||
throw new AuthorizationDeniedException(e);
|
||||
}
|
||||
throw new RuntimeException("Failed to obtain entitlements.", e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to obtain entitlements.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Failed to obtain entitlements", cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,13 +36,8 @@ public class EntitlementResource {
|
|||
.authorizationBearer(eat)
|
||||
.json(JsonSerialization.writeValueAsBytes(request))
|
||||
.response().json(EntitlementResponse.class).execute();
|
||||
} catch (HttpResponseException e) {
|
||||
if (403 == e.getStatusCode()) {
|
||||
throw new AuthorizationDeniedException(e);
|
||||
}
|
||||
throw new RuntimeException("Failed to obtain entitlements.", e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to obtain entitlements.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Failed to obtain entitlements", cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,13 +17,13 @@
|
|||
*/
|
||||
package org.keycloak.authorization.client.resource;
|
||||
|
||||
import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException;
|
||||
|
||||
import org.keycloak.authorization.client.representation.PermissionRequest;
|
||||
import org.keycloak.authorization.client.representation.PermissionResponse;
|
||||
import org.keycloak.authorization.client.util.Http;
|
||||
import org.keycloak.util.JsonSerialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
||||
*/
|
||||
|
@ -43,8 +43,8 @@ public class PermissionResource {
|
|||
.authorizationBearer(this.pat)
|
||||
.json(JsonSerialization.writeValueAsBytes(request))
|
||||
.response().json(PermissionResponse.class).execute();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error obtaining permission ticket.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Error obtaining permission ticket", cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,13 +17,15 @@
|
|||
*/
|
||||
package org.keycloak.authorization.client.resource;
|
||||
|
||||
import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.keycloak.authorization.client.representation.RegistrationResponse;
|
||||
import org.keycloak.authorization.client.representation.ResourceRepresentation;
|
||||
import org.keycloak.authorization.client.util.Http;
|
||||
import org.keycloak.util.JsonSerialization;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
||||
*/
|
||||
|
@ -43,8 +45,8 @@ public class ProtectedResource {
|
|||
.authorizationBearer(this.pat)
|
||||
.json(JsonSerialization.writeValueAsBytes(resource))
|
||||
.response().json(RegistrationResponse.class).execute();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not create resource.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Could not create resource", cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,8 +55,8 @@ public class ProtectedResource {
|
|||
this.http.<RegistrationResponse>put("/authz/protection/resource_set/" + resource.getId())
|
||||
.authorizationBearer(this.pat)
|
||||
.json(JsonSerialization.writeValueAsBytes(resource)).execute();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not create resource.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Could not update resource", cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,8 +65,8 @@ public class ProtectedResource {
|
|||
return this.http.<RegistrationResponse>get("/authz/protection/resource_set/" + id)
|
||||
.authorizationBearer(this.pat)
|
||||
.response().json(RegistrationResponse.class).execute();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not find resource.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Could not find resource", cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,8 +76,8 @@ public class ProtectedResource {
|
|||
.authorizationBearer(this.pat)
|
||||
.param("filter", filter)
|
||||
.response().json(Set.class).execute();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not find resource.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Could not find resource", cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,8 +86,8 @@ public class ProtectedResource {
|
|||
return this.http.<Set>get("/authz/protection/resource_set")
|
||||
.authorizationBearer(this.pat)
|
||||
.response().json(Set.class).execute();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not find resource.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Could not find resource", cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,8 +96,8 @@ public class ProtectedResource {
|
|||
this.http.delete("/authz/protection/resource_set/" + id)
|
||||
.authorizationBearer(this.pat)
|
||||
.execute();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not delete resource.", e);
|
||||
} catch (Exception cause) {
|
||||
throw handleAndWrapException("Could not delete resource", cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2017 Red Hat, Inc. and/or its affiliates
|
||||
* and other contributors as indicated by the @author tags.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.keycloak.authorization.client.util;
|
||||
|
||||
import org.keycloak.authorization.client.AuthorizationDeniedException;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
||||
*/
|
||||
public final class Throwables {
|
||||
|
||||
/**
|
||||
* Handles an {@code exception} and wraps it into a {@link RuntimeException}. The resulting exception contains
|
||||
* more details in case the given {@code exception} is of a {@link HttpResponseException}.
|
||||
*
|
||||
* @param message the message
|
||||
* @param exception the root exception
|
||||
* @return a {@link RuntimeException} wrapping the given {@code exception}
|
||||
*/
|
||||
public static RuntimeException handleAndWrapException(String message, Exception exception) {
|
||||
if (exception instanceof HttpResponseException) {
|
||||
throw handleAndWrapHttpResponseException(message, HttpResponseException.class.cast(exception));
|
||||
}
|
||||
|
||||
return new RuntimeException(message, exception);
|
||||
}
|
||||
|
||||
private static RuntimeException handleAndWrapHttpResponseException(String message, HttpResponseException exception) {
|
||||
HttpResponseException hre = HttpResponseException.class.cast(exception);
|
||||
StringBuilder detail = new StringBuilder(message);
|
||||
byte[] bytes = hre.getBytes();
|
||||
|
||||
if (bytes != null) {
|
||||
detail.append(". Server message: ").append(new String(bytes));
|
||||
}
|
||||
|
||||
if (403 == exception.getStatusCode()) {
|
||||
throw new AuthorizationDeniedException(detail.toString(), exception);
|
||||
}
|
||||
|
||||
return new RuntimeException(detail.toString(), exception);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue