Skip to content

Commit

Permalink
improved exception handling during profile creation to prevent profil…
Browse files Browse the repository at this point in the history
…e overwrites
  • Loading branch information
albogdano committed Nov 11, 2023
1 parent 3561d9a commit 20201aa
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main/java/com/erudika/scoold/utils/ScooldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public final class ScooldUtils {
}

private final ParaClient pc;
private final ParaClient pcThrows;
private final LanguageUtils langutils;
private final AvatarRepository avatarRepository;
private final GravatarAvatarGenerator gravatarAvatarGenerator;
Expand All @@ -198,7 +199,10 @@ public ScooldUtils(ParaClient pc, LanguageUtils langutils, AvatarRepositoryProxy
this.langutils = langutils;
this.avatarRepository = avatarRepository;
this.gravatarAvatarGenerator = gravatarAvatarGenerator;
this.pcThrows = new ParaClient(CONF.paraAccessKey(), CONF.paraSecretKey());
API_USER.setPicture(avatarRepository.getAnonymizedLink(CONF.supportEmail()));
setParaEndpointAndApiPath(pcThrows);
pcThrows.throwExceptionOnHTTPError(true);
}

public ParaClient getParaClient() {
Expand Down Expand Up @@ -342,10 +346,18 @@ private boolean promoteOrDemoteUser(Profile authUser, User u) {
}

private Profile getOrCreateProfile(User u, HttpServletRequest req) {
Profile authUser = pc.read(Profile.id(u.getId())); // what if this request fails (server down, OS frozen, etc)?
Profile authUser;
try {
authUser = pcThrows.read(Profile.id(u.getId())); // what if this request fails (server down, OS frozen, etc)?
} catch (Exception e) {
logger.error(e.getMessage(), e);
authUser = pcThrows.read(Profile.id(u.getId())); // try again
if (authUser != null) {
return authUser;
}
}
if (authUser == null) {
authUser = pc.read(Profile.id(u.getId()));
authUser = (authUser == null) ? Profile.fromUser(u) : authUser;
authUser = Profile.fromUser(u);
authUser.create();
if (!u.getIdentityProvider().equals("generic")) {
sendWelcomeEmail(u, false, req);
Expand Down

0 comments on commit 20201aa

Please sign in to comment.