Skip to content

Commit

Permalink
[MODORDERS-1183] Make error pattern more generic (#1020)
Browse files Browse the repository at this point in the history
* MODORDERS-1183. Introduce new error code for missed affiliations
  • Loading branch information
SerhiiNosko authored Sep 17, 2024
1 parent 2be25d4 commit fe1e614
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand All @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down

0 comments on commit fe1e614

Please sign in to comment.