Skip to content

Commit

Permalink
Added error-handling for graph client
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Oct 16, 2023
1 parent 830cba2 commit abab91a
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions server/src/main/java/access/provision/graph/GraphClient.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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;
import com.microsoft.graph.requests.UserRequest;
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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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<Request> getRequestGraphServiceClient(Provisioning provisioning) {
Expand Down

0 comments on commit abab91a

Please sign in to comment.