diff --git a/src/main/java/org/folio/edge/patron/MainVerticle.java b/src/main/java/org/folio/edge/patron/MainVerticle.java index 91252bb..528c2c2 100644 --- a/src/main/java/org/folio/edge/patron/MainVerticle.java +++ b/src/main/java/org/folio/edge/patron/MainVerticle.java @@ -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); diff --git a/src/main/java/org/folio/edge/patron/PatronHandler.java b/src/main/java/org/folio/edge/patron/PatronHandler.java index 2863f17..89ad616 100644 --- a/src/main/java/org/folio/edge/patron/PatronHandler.java +++ b/src/main/java/org/folio/edge/patron/PatronHandler.java @@ -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; @@ -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; @@ -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); @@ -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)); }); @@ -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); @@ -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 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)