Skip to content

Commit 36e603e

Browse files
committed
Issue #66 - Add methods for ordered ExceptionMapper
1 parent 2ef20cf commit 36e603e

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ To do so, create all the required implementation, for example :
170170
```java
171171
public class MyExceptionMapper implements ch.powerunit.extensions.exceptions.ExceptionMapper {
172172
public RuntimeException apply(Exception e) {
173-
//TODO
173+
//Add code here
174174
}
175175

176176
public Class<? extends Exception> targetException() {
177-
return //TODO;
177+
return //Add code here;
178178
}
179179

180180
// Optional, to define the order between the ExceptionMapper

src/main/java/ch/powerunit/extensions/exceptions/Constants.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import static ch.powerunit.extensions.exceptions.ExceptionMapper.forException;
2323
import static ch.powerunit.extensions.exceptions.SupplierWithException.ignored;
2424
import static java.util.Objects.requireNonNull;
25+
import static java.util.stream.Collectors.toList;
2526

2627
import java.sql.SQLException;
27-
import java.util.Comparator;
2828
import java.util.ServiceLoader;
2929
import java.util.ServiceLoader.Provider;
3030
import java.util.function.Function;
@@ -100,8 +100,8 @@ private static ExceptionMapper buildTransformerExceptionMapper() throws ClassNot
100100
}
101101

102102
private static Function<Exception, RuntimeException> computeDefaultMapper() {
103-
return ExceptionMapper.forExceptions(ServiceLoader.load(ExceptionMapper.class).stream().map(Provider::get)
104-
.sorted(Comparator.comparingInt(ExceptionMapper::order)).toArray(ExceptionMapper[]::new));
103+
return ExceptionMapper.forOrderedExceptions(
104+
ServiceLoader.load(ExceptionMapper.class).stream().map(Provider::get).collect(toList()));
105105
}
106106

107107
private Constants() {

src/main/java/ch/powerunit/extensions/exceptions/ExceptionMapper.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
import static java.util.Arrays.stream;
2323

24+
import java.util.Collection;
25+
import java.util.Comparator;
2426
import java.util.Optional;
27+
import java.util.ServiceLoader;
28+
import java.util.ServiceLoader.Provider;
2529
import java.util.function.Function;
2630

2731
/**
@@ -135,6 +139,26 @@ default int order() {
135139
* for the other exception just create a {@code WrappedException}.
136140
*/
137141
static <E extends Exception> ExceptionMapper forException(Class<E> clazz, Function<E, RuntimeException> mapper) {
142+
return forException(clazz, mapper, 0);
143+
}
144+
145+
/**
146+
* Helper method to create exception wrapper that check the exception class.
147+
*
148+
* @param clazz
149+
* the class of the exception to be wrapped.
150+
* @param mapper
151+
* the exception mapper.
152+
* @param order
153+
* the order
154+
* @param <E>
155+
* the type of the exception.
156+
* @return A new exception mapper, which use the one received as parameter or
157+
* for the other exception just create a {@code WrappedException}.
158+
* @since 2.2.0
159+
*/
160+
static <E extends Exception> ExceptionMapper forException(Class<E> clazz, Function<E, RuntimeException> mapper,
161+
int order) {
138162
return new ExceptionMapper() {
139163

140164
@Override
@@ -146,6 +170,11 @@ public RuntimeException apply(Exception t) {
146170
public Class<E> targetException() {
147171
return clazz;
148172
}
173+
174+
@Override
175+
public int order() {
176+
return order;
177+
}
149178
};
150179
}
151180

@@ -196,4 +225,21 @@ static Function<Exception, RuntimeException> forExceptions(ExceptionMapper... ma
196225
.orElseGet(() -> new WrappedException(e));
197226
}
198227

228+
/**
229+
* Helper method to create exception wrapper that use the first one that is
230+
* applicable, but having them sorted using the {@link #order()} method.
231+
*
232+
* @param mappers
233+
* the mapper to be tried
234+
* @return the mapping function.
235+
* @since 2.2.0
236+
*/
237+
static Function<Exception, RuntimeException> forOrderedExceptions(Collection<ExceptionMapper> mappers) {
238+
if (mappers.isEmpty()) {
239+
return WrappedException::new;
240+
}
241+
return ExceptionMapper.forExceptions(mappers.stream().sorted(Comparator.comparingInt(ExceptionMapper::order))
242+
.toArray(ExceptionMapper[]::new));
243+
}
244+
199245
}

src/test/java/ch/powerunit/extensions/exceptions/ExceptionMapperTest.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.io.IOException;
2323
import java.sql.SQLException;
24+
import java.util.Collections;
25+
import java.util.List;
2426

2527
import javax.xml.bind.JAXBException;
2628
import javax.xml.transform.TransformerException;
@@ -70,12 +72,6 @@ public void testForExceptions2MapperForOtherException() {
7072
new SQLException("test")).is(both(exceptionMessage("test")).and(instanceOf(WrappedException.class)));
7173
}
7274

73-
@Test
74-
public void testForExceptions3None() {
75-
assertThatFunction(ExceptionMapper.forExceptions(), new IOException("test"))
76-
.is(instanceOf(WrappedException.class));
77-
}
78-
7975
@Test
8076
public void testForExceptions3SameExceptionFirst() {
8177
assertThatFunction(
@@ -122,6 +118,12 @@ public void testForExceptions3MapperForOtherException() {
122118
new SQLException("test")).is(both(exceptionMessage("test")).and(instanceOf(WrappedException.class)));
123119
}
124120

121+
@Test
122+
public void testForExceptionsAnyNone() {
123+
assertThatFunction(ExceptionMapper.forExceptions(), new IOException("test"))
124+
.is(instanceOf(WrappedException.class));
125+
}
126+
125127
@Test
126128
public void testForExceptionsAnySameExceptionFirst() {
127129
assertThatFunction(ExceptionMapper.forExceptions(
@@ -175,6 +177,31 @@ public void testForExceptionsAnyMapperForOtherException() {
175177
new SQLException("test")).is(both(exceptionMessage("test")).and(instanceOf(WrappedException.class)));
176178
}
177179

180+
@Test
181+
public void testForOrderedExceptionsNone() {
182+
assertThatFunction(ExceptionMapper.forOrderedExceptions(Collections.emptyList()), new IOException("test"))
183+
.is(instanceOf(WrappedException.class));
184+
}
185+
186+
@Test
187+
public void testForOrderedOneElement() {
188+
assertThatFunction(
189+
ExceptionMapper.forOrderedExceptions(List.of(
190+
ExceptionMapper.forException(RuntimeException.class, e -> new WrappedException("testme1"), 2))),
191+
new IllegalArgumentException("test"))
192+
.is(both(exceptionMessage("testme1")).and(instanceOf(WrappedException.class)));
193+
}
194+
195+
@Test
196+
public void testForOrderedTwoElement() {
197+
assertThatFunction(ExceptionMapper.forOrderedExceptions(
198+
List.of(ExceptionMapper.forException(RuntimeException.class, e -> new WrappedException("testme1"), 2),
199+
ExceptionMapper.forException(IllegalArgumentException.class,
200+
e -> new WrappedException("testme2"), 1))),
201+
new IllegalArgumentException("test"))
202+
.is(both(exceptionMessage("testme2")).and(instanceOf(WrappedException.class)));
203+
}
204+
178205
@Test
179206
public void testJaxbException() {
180207
ExceptionMapper.jaxbExceptionMapper().apply(new JAXBException("msg"));

0 commit comments

Comments
 (0)