diff --git a/katharsis-core/src/main/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilder.java b/katharsis-core/src/main/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilder.java index c6de78ff..370d7a86 100644 --- a/katharsis-core/src/main/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilder.java +++ b/katharsis-core/src/main/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilder.java @@ -1,10 +1,12 @@ package io.katharsis.core.internal.exception; +import io.katharsis.errorhandling.exception.KatharsisMappableException; import io.katharsis.errorhandling.mapper.ExceptionMapper; import io.katharsis.errorhandling.mapper.JsonApiExceptionMapper; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import org.apache.commons.lang3.reflect.TypeUtils; @@ -16,15 +18,20 @@ public ExceptionMapperRegistry build(String resourceSearchPackage) { } public ExceptionMapperRegistry build(ExceptionMapperLookup exceptionMapperLookup) { - addKatharsisDefaultMappers(); for (JsonApiExceptionMapper exceptionMapper : exceptionMapperLookup.getExceptionMappers()) { registerExceptionMapper(exceptionMapper); } + addKatharsisDefaultMappers(); return new ExceptionMapperRegistry(exceptionMappers); } private void addKatharsisDefaultMappers() { - registerExceptionMapper(new KatharsisExceptionMapper()); + Optional customKatharsisMappableHandler = exceptionMappers.stream() + .filter(mapperType -> KatharsisMappableException.class.equals(mapperType.getExceptionClass())) + .findFirst(); + if(!customKatharsisMappableHandler.isPresent()) { + registerExceptionMapper(new KatharsisExceptionMapper()); + } } private void registerExceptionMapper(JsonApiExceptionMapper exceptionMapper) { diff --git a/katharsis-core/src/test/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilderTest.java b/katharsis-core/src/test/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilderTest.java index 63a13b44..f7a0e6d3 100644 --- a/katharsis-core/src/test/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilderTest.java +++ b/katharsis-core/src/test/java/io/katharsis/core/internal/exception/ExceptionMapperRegistryBuilderTest.java @@ -1,15 +1,23 @@ package io.katharsis.core.internal.exception; -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - +import io.katharsis.errorhandling.ErrorData; +import io.katharsis.errorhandling.ErrorResponse; +import io.katharsis.errorhandling.ErrorResponseBuilder; import io.katharsis.errorhandling.exception.InvalidResourceException; import io.katharsis.errorhandling.exception.KatharsisMappableException; +import io.katharsis.errorhandling.handlers.BaseExceptionMapper; import io.katharsis.errorhandling.handlers.NoAnnotationExceptionMapper; import io.katharsis.errorhandling.handlers.SomeExceptionMapper; +import io.katharsis.errorhandling.mapper.JsonApiExceptionMapper; +import io.katharsis.legacy.queryParams.errorhandling.ExceptionMapperProvider; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; public class ExceptionMapperRegistryBuilderTest { @@ -33,6 +41,26 @@ public void shouldContainDefaultKatharsisExceptionMapper() throws Exception { .contains(KatharsisMappableException.class) .contains(SomeExceptionMapper.SomeException.class) .contains(IllegalArgumentException.class); + + assertThat(registry.getExceptionMappers()) + .extracting("exceptionMapper") + .extracting("class") + .contains(KatharsisExceptionMapper.class); + } + + @Test + public void shouldNotAddDefaultExceptionMapperIfCustomMapperIsFound() throws Exception { + ExceptionMapperRegistry registry = builder.build(() -> { + Set mappers = new HashSet<>(); + mappers.add(new CustomKatharsisMappableExceptionMapper()); + return mappers; + }); + + assertThat(registry.getExceptionMappers()) + .extracting("exceptionMapper") + .extracting("class") + .contains(CustomKatharsisMappableExceptionMapper.class) + .doesNotContain(KatharsisExceptionMapper.class); } @Test @@ -65,4 +93,18 @@ public void shouldContainScannedExceptionMapperWhenMultiplePaths() throws Except .contains(IllegalArgumentException.class); } + @ExceptionMapperProvider + public static class CustomKatharsisMappableExceptionMapper extends BaseExceptionMapper { + + @Override + public ErrorResponse toErrorResponse(KatharsisMappableException exception) { + return new ErrorResponseBuilder() + .setStatus(500) + .setSingleErrorData(ErrorData.builder() + .setTitle("Custom Error") + .build()) + .build(); + } + } + }