Skip to content

Commit

Permalink
EDGPATRON-149 Formatting error response
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh-kalyanasundaram committed Oct 16, 2024
1 parent ec360d3 commit 364129b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
3 changes: 0 additions & 3 deletions src/main/java/org/folio/edge/patron/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public Router defineRoutes() {
router.route(HttpMethod.GET, "/patron/account/:patronId/external-patrons")
.handler(patronHandler::handleGetExtPatronsAccounts);

router.route(HttpMethod.GET, "/patron/account/:patronId/by-email/:emailId")
.handler(patronHandler::handleGetExtPatronAccountByEmail);

router.route(HttpMethod.PUT, "/patron/account/:patronId/by-email/:emailId")
.handler(patronHandler::handlePutExtPatronAccountByEmail);

Expand Down
39 changes: 26 additions & 13 deletions src/main/java/org/folio/edge/patron/PatronHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -42,6 +43,7 @@
import org.apache.logging.log4j.Logger;
import org.folio.edge.core.Handler;
import org.folio.edge.core.security.SecureStore;
import org.folio.edge.core.utils.Mappers;
import org.folio.edge.core.utils.OkapiClient;
import org.folio.edge.core.utils.OkapiClientFactory;
import org.folio.edge.patron.model.error.Error;
Expand Down Expand Up @@ -139,16 +141,6 @@ public void handleRenew(RoutingContext ctx) {

}

public void handleGetExtPatronAccountByEmail(RoutingContext ctx) {
handleCommon(ctx,
new String[] { PARAM_PATRON_ID, PARAM_EMAIL_ID },
new String[] {},
(client, params) -> ((PatronOkapiClient) client).getExtPatronAccountByEmail(
params.get(PARAM_EMAIL_ID),
resp -> handleProxyResponse(ctx, resp),
t -> handleProxyException(ctx, t)));
}

public void handlePutExtPatronAccountByEmail(RoutingContext ctx) {
if (ctx.body().asJsonObject() == null) {
badRequest(ctx, MSG_EXTERNAL_NOBODY);
Expand Down Expand Up @@ -265,7 +257,7 @@ public void handleGetPatronRegistrationStatus(RoutingContext ctx) {
super.handleCommon(ctx, new String[]{PARAM_EMAIL_ID}, new String[]{}, (client, params) -> {
String alternateTenantId = ctx.request().getParam("alternateTenantId", client.tenant);
final PatronOkapiClient patronClient = new PatronOkapiClient(client, alternateTenantId);
patronClient.getExtPatronAccountByEmail(params.get(PARAM_EMAIL_ID),
patronClient.getPatronRegistrationStatus(params.get(PARAM_EMAIL_ID),
resp -> handleRegistrationStatusResponse(ctx, resp),
t -> handleProxyException(ctx, t));
});
Expand Down Expand Up @@ -361,8 +353,8 @@ protected void handleRegistrationStatusResponse(RoutingContext ctx, HttpResponse
serverResponse.end(respBody); //not an error case, pass on the response body as received
}
else {
String errorMsg = (statusCode == 404 || statusCode == 400 || statusCode == 422)
? get422ErrorMsg(statusCode, respBody)
String errorMsg = (statusCode == 404 || statusCode == 400)
? getFormattedErrorMsg(statusCode, respBody)
: getStructuredErrorMessage(statusCode, respBody);
setContentType(serverResponse, APPLICATION_JSON);
serverResponse.end(errorMsg);
Expand Down Expand Up @@ -472,6 +464,27 @@ private String get422ErrorMsg(int statusCode, String respBody){
return errorMessage;
}

private String getFormattedErrorMsg(int statusCode, String respBody){
logger.debug("getFormattedErrorMsg:: respBody {}", respBody);
String errorMessage = "";
try {
var errors = Json.decodeValue(respBody, Errors.class).getErrors();
if (errors != null && !errors.isEmpty()) {
var error = errors.get(0);
Map<String, String> errorMap = new HashMap<>();
errorMap.put("message", error.getMessage());
errorMap.put("code", error.getCode());
errorMessage = Mappers.jsonMapper.writeValueAsString(errorMap);
} else {
errorMessage = getStructuredErrorMessage(statusCode, "No error message found in response");
}
} catch(Exception ex) {
logger.warn(ex.getMessage());
errorMessage = getStructuredErrorMessage(statusCode, "A problem encountered when extracting error message");
}
return errorMessage;
}

private String getErrorMessage(int statusCode, String respBody){

if (statusCode == 422)
Expand Down

0 comments on commit 364129b

Please sign in to comment.