diff --git a/src/main/java/org/folio/rest/core/exceptions/ExceptionUtil.java b/src/main/java/org/folio/rest/core/exceptions/ExceptionUtil.java index 6c1db5969..7c17c6967 100644 --- a/src/main/java/org/folio/rest/core/exceptions/ExceptionUtil.java +++ b/src/main/java/org/folio/rest/core/exceptions/ExceptionUtil.java @@ -30,7 +30,6 @@ public class ExceptionUtil { public static final String NOT_PROVIDED = "Not Provided"; private static final Pattern ERROR_PATTERN = Pattern.compile("(message).*(code).*(parameters)"); private static final Pattern ERRORS_PATTERN = Pattern.compile("(errors).*(message).*(code).*(parameters)"); - private static final Pattern AFFILIATION_MISSED_PATTERN = Pattern.compile("Invalid token: User with id ([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}) does not exist"); private ExceptionUtil() { } @@ -57,7 +56,8 @@ public static Errors convertToErrors(Throwable throwable) { public static HttpException getHttpException(int statusCode, String error) { if (isAffiliationMissedError(error)) { - return new HttpException(statusCode, USER_HAS_MISSED_AFFILIATIONS); + return new HttpException(statusCode, USER_HAS_MISSED_AFFILIATIONS, + List.of(new Parameter().withKey("cause").withValue(error))); } if (isErrorsMessageJson(error)) { return new HttpException(statusCode, mapToErrors(error)); @@ -90,8 +90,7 @@ public static boolean isAffiliationMissedError(String errorMessage) { if (StringUtils.isEmpty(errorMessage)) { return false; } - Matcher matcher = AFFILIATION_MISSED_PATTERN.matcher(errorMessage); - return matcher.find(); + return errorMessage.contains("Invalid token:"); } public static String errorAsString(Errors errors) { diff --git a/src/test/java/org/folio/rest/core/exceptions/ExceptionUtilTest.java b/src/test/java/org/folio/rest/core/exceptions/ExceptionUtilTest.java index 8ef0c5fa9..515107394 100644 --- a/src/test/java/org/folio/rest/core/exceptions/ExceptionUtilTest.java +++ b/src/test/java/org/folio/rest/core/exceptions/ExceptionUtilTest.java @@ -6,15 +6,19 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.folio.rest.jaxrs.model.Error; import org.folio.rest.jaxrs.model.Errors; +import org.folio.rest.jaxrs.model.Parameter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import io.vertx.pgclient.PgException; +import java.util.List; + public class ExceptionUtilTest { - private static final String INVALID_TOKEN_ERROR = """ + private static final String INVALID_TOKEN_USER_DOES_NOT_EXIST = """ { "errors" : [ { "message" : "Invalid token: User with id 858cbba3-6fd0-4430-9011-9939574677d8 does not exist", @@ -23,6 +27,15 @@ public class ExceptionUtilTest { } ], "total_records" : 1 }"""; + private static final String INVALID_TOKEN_USER_IS_NOT_ACTIVE = """ + { + "errors" : [ { + "message" : "Invalid token: user with id d107a00f-b3fe-44b2-ae88-44c43733d6cc is not active", + "code" : "genericError", + "parameters" : [ ] + } ], + "total_records" : 1 + }"""; @Test void testIfBadRequestMessageNotNull() { @@ -63,9 +76,19 @@ void testIsExceptionMessageIsJSON() { } @Test - void testGetHttpExceptionMissedAffiliationError() { - HttpException httpException = ExceptionUtil.getHttpException(401, INVALID_TOKEN_ERROR); - assertEquals(ErrorCodes.USER_HAS_MISSED_AFFILIATIONS.toError(), httpException.getError()); + void testGetHttpExceptionMissedAffiliationWhenUserDoesNotExist() { + HttpException httpException = ExceptionUtil.getHttpException(401, INVALID_TOKEN_USER_DOES_NOT_EXIST); + Error expected = ErrorCodes.USER_HAS_MISSED_AFFILIATIONS.toError() + .withParameters(List.of(new Parameter().withKey("cause").withValue(INVALID_TOKEN_USER_DOES_NOT_EXIST))); + assertEquals(expected, httpException.getError()); + } + + @Test + void testGetHttpExceptionMissedAffiliationWhenUserIsNotActive() { + HttpException httpException = ExceptionUtil.getHttpException(401, INVALID_TOKEN_USER_IS_NOT_ACTIVE); + Error expected = ErrorCodes.USER_HAS_MISSED_AFFILIATIONS.toError() + .withParameters(List.of(new Parameter().withKey("cause").withValue(INVALID_TOKEN_USER_IS_NOT_ACTIVE))); + assertEquals(expected, httpException.getError()); } @Test @@ -78,7 +101,7 @@ void testGetHttpExceptionOtherError() { @Test void testIsAffiliationMissedTrue() { - boolean act = ExceptionUtil.isAffiliationMissedError(INVALID_TOKEN_ERROR); + boolean act = ExceptionUtil.isAffiliationMissedError(INVALID_TOKEN_USER_DOES_NOT_EXIST); assertTrue(act); }