Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#444 : Add default exception mapper only if custom mapper not found for KatharsisMappableException #447

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<ExceptionMapperType> customKatharsisMappableHandler = exceptionMappers.stream()
.filter(mapperType -> KatharsisMappableException.class.equals(mapperType.getExceptionClass()))
.findFirst();
if(!customKatharsisMappableHandler.isPresent()) {
registerExceptionMapper(new KatharsisExceptionMapper());
}
}

private void registerExceptionMapper(JsonApiExceptionMapper<? extends Throwable> exceptionMapper) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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<JsonApiExceptionMapper> mappers = new HashSet<>();
mappers.add(new CustomKatharsisMappableExceptionMapper());
return mappers;
});

assertThat(registry.getExceptionMappers())
.extracting("exceptionMapper")
.extracting("class")
.contains(CustomKatharsisMappableExceptionMapper.class)
.doesNotContain(KatharsisExceptionMapper.class);
}

@Test
Expand Down Expand Up @@ -65,4 +93,18 @@ public void shouldContainScannedExceptionMapperWhenMultiplePaths() throws Except
.contains(IllegalArgumentException.class);
}

@ExceptionMapperProvider
public static class CustomKatharsisMappableExceptionMapper extends BaseExceptionMapper<KatharsisMappableException> {

@Override
public ErrorResponse toErrorResponse(KatharsisMappableException exception) {
return new ErrorResponseBuilder()
.setStatus(500)
.setSingleErrorData(ErrorData.builder()
.setTitle("Custom Error")
.build())
.build();
}
}

}