Skip to content

Commit

Permalink
katharsis-project#444 : Add default exception mapper only if customer…
Browse files Browse the repository at this point in the history
… mapper not found for KatharsisMappableException
  • Loading branch information
Karthik Chandraraj committed Aug 7, 2017
1 parent 73d1a87 commit fd150be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
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();
}
}

}

0 comments on commit fd150be

Please sign in to comment.