From abab91abbe2834298fce72d9563dfd39ac495889 Mon Sep 17 00:00:00 2001 From: Okke Harsta Date: Mon, 16 Oct 2023 11:02:31 +0200 Subject: [PATCH] Added error-handling for graph client --- .../access/provision/graph/GraphClient.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/access/provision/graph/GraphClient.java b/server/src/main/java/access/provision/graph/GraphClient.java index 264cecb9..fc94a7de 100644 --- a/server/src/main/java/access/provision/graph/GraphClient.java +++ b/server/src/main/java/access/provision/graph/GraphClient.java @@ -1,11 +1,13 @@ package access.provision.graph; +import access.exception.RemoteException; import access.model.User; import access.provision.Provisioning; import access.provision.ProvisioningServiceDefault; import com.azure.identity.ClientSecretCredential; import com.azure.identity.ClientSecretCredentialBuilder; import com.microsoft.graph.authentication.TokenCredentialAuthProvider; +import com.microsoft.graph.core.ClientException; import com.microsoft.graph.http.BaseRequest; import com.microsoft.graph.requests.GraphServiceClient; import com.microsoft.graph.requests.InvitationCollectionRequest; @@ -13,8 +15,8 @@ import okhttp3.Request; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.http.HttpStatus; import org.springframework.util.ReflectionUtils; -import org.springframework.util.StringUtils; import java.lang.reflect.Field; @@ -50,8 +52,18 @@ public GraphResponse newUserRequest(Provisioning provisioning, User user) { buildRequest.getBaseRequest().getRequestUrl(), provisioning.getGraphClientId(), user.getEduPersonPrincipalName())); - com.microsoft.graph.models.Invitation newInvitation = buildRequest.post(invitation); - return new GraphResponse(newInvitation.invitedUser.id, newInvitation.inviteRedeemUrl); + try { + + com.microsoft.graph.models.Invitation newInvitation = buildRequest.post(invitation); + return new GraphResponse(newInvitation.invitedUser.id, newInvitation.inviteRedeemUrl); + + } catch (ClientException e) { + String errorMessage = String.format("Error Graph request (entityID %s) to %s for user %s", + provisioning.getEntityId(), + graphUrl, + user.getEmail()); + throw new RemoteException(HttpStatus.BAD_REQUEST, errorMessage, e); + } } public void updateUserRequest(User user, Provisioning provisioning, String remoteUserIdentifier) { @@ -60,11 +72,18 @@ public void updateUserRequest(User user, Provisioning provisioning, String remot String graphUrl = provisioning.getGraphUrl(); replaceGraphUrl(graphUrl, userRequest); - - com.microsoft.graph.models.User graphUser = userRequest.get(); - - graphUser.mail = user.getEmail(); - userRequest.patch(graphUser); + try { + com.microsoft.graph.models.User graphUser = userRequest.get(); + + graphUser.mail = user.getEmail(); + userRequest.patch(graphUser); + } catch (ClientException e) { + String errorMessage = String.format("Error Graph request (entityID %s) to %s for user %s", + provisioning.getEntityId(), + graphUrl, + user.getEmail()); + throw new RemoteException(HttpStatus.BAD_REQUEST, errorMessage, e); + } } private GraphServiceClient getRequestGraphServiceClient(Provisioning provisioning) {