Skip to content

Commit

Permalink
🎨 🔥 🐛 Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Jun 12, 2024
1 parent 4daac2d commit aa54c1e
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.function.Predicate;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -248,7 +247,7 @@ public Response execQuerySync(String query, Map<String, Object> variables) {
null);
errors.addAll(response.getErrors());
}
} catch (ExecutionException | InterruptedException | RuntimeException e) {
} catch (Throwable e) {
logAndSendEmail(getLogId(), "Error executing GraphQL query", e);
exceptions.add(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public enum MemberStatus {
DECLINED,
COMMITTEE,
ACTIVE,
PENDING,
INACTIVE,
PENDING,
SPONSOR,
UNKNOWN;

Expand Down Expand Up @@ -327,4 +327,8 @@ public CommonhausUser build() {
return new CommonhausUser(this);
}
}

public void updateApplicationStatus(MemberSession session) {
session.applicationStatus(this.application != null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class GitHubUser {
String avatarUrl;
String url;
Set<String> roles = new HashSet<>();
boolean hasApplication = false;

public GitHubUser(long id, String login, String nodeId) {
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public Response getAliases(@DefaultValue("false") @QueryParam("refresh") boolean
} catch (WebApplicationException e) {
return e.getResponse();
} catch (Throwable e) {
ctx.logAndSendEmail("getAliases", "Unable to fetch user aliases for " + session.login(), e, null);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponseWithEmail("getAliases", "Unable to fetch user aliases for " + session.login(), e);
}
}

Expand Down Expand Up @@ -122,8 +121,7 @@ public Response updateAliases(Map<String, Set<String>> aliases) {
} catch (WebApplicationException e) {
return e.getResponse();
} catch (Throwable e) {
ctx.logAndSendEmail("updateAliases", "Unable to update user aliases for " + session.login(), e, null);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponseWithEmail("updateAliases", "Unable to update user aliases for " + session.login(), e);
}
}

Expand Down Expand Up @@ -183,8 +181,7 @@ public Response generatePassword(AliasRequest alias) {
} catch (WebApplicationException e) {
return e.getResponse();
} catch (Throwable e) {
ctx.logAndSendEmail("generatePassword", "Unable to generate SMTP password for " + alias.email(), e, null);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponseWithEmail("generatePassword", "Unable to generate SMTP password for " + alias.email(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@ public ApplicationData userUpdateApplicationIssue(
: new ApplicationData(session.login(), item);
}

ApplicationData findUserApplication(MemberSession session, String applicationId, boolean withComments) {
ApplicationData findUserApplication(MemberSession session, String applicationId, boolean withComments) throws Throwable {
ScopedQueryContext qc = ctx.getDatastoreContext();
DataCommonItem issue = qc.getItem(EventType.issue, applicationId);
if (qc.hasErrors()) {
throw qc.bundleExceptions();
}
ApplicationData application = new ApplicationData(session.login(), issue);
if (application.isValid() && withComments) {
Feedback feedback = getFeedback(qc, applicationId, issue.mostRecentEdit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public Response getApplication() {
.setData(Type.APPLY, applicationData)
.finish();
} catch (Throwable e) {
Log.errorf(e, "getApplication: Unable to retrieve application for %s: %s", session.login(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponseWithEmail("getApplication",
"getApplication: Unable to retrieve application for " + session.login(), e);
}
}

Expand All @@ -91,8 +91,7 @@ public Response setApplication(ApplicationPost applicationPost) {
}
return doUserApplicationUpdate(user, applicationData, applicationPost); // WRITE
} catch (Throwable e) {
Log.errorf(e, "getApplication: Unable to retrieve application for %s: %s", session.login(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponseWithEmail("setApplication", "Unable to retrieve application for " + session.login(), e);
}
}

Expand Down Expand Up @@ -145,8 +144,8 @@ private Response doUserApplicationUpdate(CommonhausUser user, ApplicationData ap
if (qc.hasErrors()) {
Throwable e = qc.bundleExceptions();
qc.clearErrors();
ctx.logAndSendEmail(qc.getLogId(), "Failed to update MembershipApplication issue", e, null);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponseWithEmail(qc.getLogId(),
"doUserApplicationUpdate: Failed to update MembershipApplication issue", e);
}
if (updated == null) {
Log.errorf("doUserApplicationUpdate|%s: Updated data was not returned", session.login());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public Response updateAttestation(AttestationPost post) {

return user.toResponse().finish();
} catch (Throwable e) {
Log.errorf(e, "updateAttestation: Unable to update attestation for %s: %s", session.login(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponse("updateAttestation", "Unable to update attestation for " + session.login(), e);
}
}

Expand Down Expand Up @@ -105,8 +104,7 @@ public Response updateAttestations(List<AttestationPost> postList) {

return user.toResponse().finish();
} catch (Throwable e) {
Log.errorf(e, "updateAttestations: Unable to update attestations for %s: %s", session.login(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponse("updateAttestations", "Unable to update attestations for " + session.login(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public Response getUserInfo(@DefaultValue("false") @QueryParam("refresh") boolea
if (session.hasConnection()) {
return Response.ok(new ApiResponse(ApiResponse.Type.INFO, session.getUserData())).build();
} else {
Log.errorf("getUserInfo: Unable to establish connection to GH for %s", session.login());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponseWithEmail("getUserInfo", "Unable to establish connection to GH for " + session.login(),
session.connectionError());
}
}

Expand All @@ -100,10 +100,12 @@ public Response getCommonhausUser(@DefaultValue("false") @QueryParam("refresh")
if (user == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return user.toResponse().finish();
user.updateApplicationStatus(session);
return user.toResponse()
.setData(ApiResponse.Type.INFO, session.getUserData())
.finish();
} catch (Exception e) {
Log.errorf(e, "getCommonhausUser: Unable to get user data for %s: %s", session.login(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponse("getCommonhausUser", "Unable to get user data for " + session.login(), e);
}
}

Expand All @@ -124,10 +126,12 @@ public Response updateUserStatus() {
false,
false));
}
return user.toResponse().finish();
user.updateApplicationStatus(session);
return user.toResponse()
.setData(ApiResponse.Type.INFO, session.getUserData())
.finish();
} catch (Exception e) {
Log.errorf(e, "updateUserStatus: Unable to update status for %s: %s", session.login(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
return ctx.toResponse("updateUserStatus", "Unable to update status for " + session.login(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.commonhaus.automation.admin.api;

import java.io.IOException;
import java.util.Set;

import org.commonhaus.automation.admin.AdminDataCache;
Expand All @@ -22,14 +23,19 @@ static MemberSession getMemberSession(AppContextService ctx, UserInfo userInfo,
});

// Create/renew GitHub connection
memberProfile.connection = ctx.getUserConnection(memberProfile.nodeId(), identity);
try {
memberProfile.connection = ctx.getUserConnection(memberProfile.nodeId(), identity);
} catch (IOException e) {
memberProfile.connectionError = e;
}
return memberProfile;
}

private final String nodeId;
private final UserInfo userInfo;

private transient GitHub connection;
private transient IOException connectionError;
private GitHubUser userData;

private MemberSession(UserInfo userInfo) {
Expand All @@ -50,14 +56,6 @@ public GitHubUser getUserData() {
return user;
}

public GitHub connection() {
return connection;
}

public boolean hasConnection() {
return connection != null;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down Expand Up @@ -118,4 +116,20 @@ public Set<String> roles() {
public String url() {
return getUserData().url;
}

public void applicationStatus(boolean b) {
this.getUserData().hasApplication = b;
}

public GitHub connection() {
return connection;
}

public boolean hasConnection() {
return connection != null;
}

public Throwable connectionError() {
return connectionError;
}
}
Loading

0 comments on commit aa54c1e

Please sign in to comment.