Skip to content

Commit ed74267

Browse files
committed
Changed SafeFunction default ex handler
Default behavior of SafeFunction is now to wraps only checked exceptions. runtime exception are just re thrown.
1 parent 8359491 commit ed74267

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

fj-core/src/docs/SafeFunction.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ With safe functions it look likes :
3232
}
3333
```
3434

35-
The default behavior is that any thrown Exception will be wrapped by a ConfigRuntimeException.
35+
The default behavior when an exception is thrown :
36+
- Wraps checked Exception around a *ConfigRuntimeException*
37+
- *RuntimeException* are just re-thrown
38+
3639
It is possible to provide custom exception handlers.
3740

3841
Read the [SafeFunction javadoc](https://javadoc.io/doc/org.fugerit.java/fj-core/latest/org/fugerit/java/core/function/SafeFunction.html) too.

fj-core/src/main/java/org/fugerit/java/core/function/SafeFunction.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,38 @@ public class SafeFunction {
4444

4545
private SafeFunction() {}
4646

47+
/**
48+
* <p>This Exception consumer just log the exception at warn log level. (no Exception is re-thrown).</p>
49+
*/
4750
public static final Consumer<Exception> EX_CONSUMER_LOG_WARN = e -> log.warn( "Exception suppressed : {}", e.toString() );
4851

52+
/**
53+
* <p>This Exception consumer log and print the stack trace of the exception at warn log level. (no Exception is re-thrown).</p>
54+
*/
4955
public static final Consumer<Exception> EX_CONSUMER_TRACE_WARN = e -> log.warn( "Exception suppressed : "+e, e );
5056

57+
/**
58+
* <p>This Exception consumer wraps any Exception around a {@link ConfigRuntimeException}.</p>
59+
*/
5160
public static final Consumer<Exception> EX_CONSUMER_THROW_CONFIG_RUNTIME = e -> { throw new ConfigRuntimeException( e ); };
5261

53-
private static final Consumer<Exception> DEFAULT_EX_CONSUMER = EX_CONSUMER_THROW_CONFIG_RUNTIME;
62+
/**
63+
* <p>This Exception consumer wraps checked Exception around a {@link ConfigRuntimeException}.</p>
64+
*
65+
* <p>RuntimeException are just re-thrown.</p>
66+
*/
67+
public static final Consumer<Exception> EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE = e -> {
68+
if ( e instanceof RuntimeException ) {
69+
throw (RuntimeException)e;
70+
} else {
71+
throw new ConfigRuntimeException( "Convert exception to ConfigRuntimeException : "+e, e );
72+
}
73+
};
74+
75+
/**
76+
* Default behavior is {@link EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE}
77+
*/
78+
private static final Consumer<Exception> DEFAULT_EX_CONSUMER = EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE;
5479

5580
/**
5681
* <p>Get a value returned by an UnsafeSupplier, and convert any raised Exception</p>
@@ -141,6 +166,13 @@ public static void apply( UnsafeVoid<Exception> fun, Consumer<Exception> exHandl
141166
}
142167
}
143168

169+
/**
170+
* <p>This method will apply a function only if a condition is met.</p>
171+
*
172+
* @param condition the condition to check
173+
* @param fun the function to apply if the condition returned <code>true</code>
174+
* @return the value returned by the condition
175+
*/
144176
public static boolean applyOnCondition( UnsafeSupplier<Boolean, Exception> condition, UnsafeVoid<Exception> fun ) {
145177
boolean cond = get( condition );
146178
if ( cond ) {
@@ -149,10 +181,26 @@ public static boolean applyOnCondition( UnsafeSupplier<Boolean, Exception> condi
149181
return cond;
150182
}
151183

184+
/**
185+
* <p>This method will apply a function only if a object is not null.</p>
186+
*
187+
* @param <T> the type of the object to check
188+
* @param v the object to check for <code>null</code>
189+
* @param fun the function to apply
190+
* @return <code>true</code> if the object is not null.
191+
*/
152192
public static <T> boolean applyIfNotNull( T v, UnsafeVoid<Exception> fun ) {
153193
return applyOnCondition( () -> v != null , fun);
154194
}
155195

196+
/**
197+
* <p>This method return a value, provided by a supplier function, only if a condition is met.</p>
198+
*
199+
* @param <R> the return type
200+
* @param condition the condition to check
201+
* @param supplier the supplier function
202+
* @return the value returned by the supplier if the condition returns <code>true</code> or <code>null</code> otherwise.
203+
*/
156204
public static <R> R getOnCondition( UnsafeSupplier<Boolean, Exception> condition, UnsafeSupplier<R, Exception> supplier ) {
157205
return get( () -> {
158206
R res = null;
@@ -165,6 +213,15 @@ public static <R> R getOnCondition( UnsafeSupplier<Boolean, Exception> condition
165213

166214
}
167215

216+
/**
217+
* <p>This method return a value, provided by a supplier function, only if an object is not <code>null</code>.</p>
218+
*
219+
* @param <T> the type of the object to check for <code>null</code>
220+
* @param <R> the return type
221+
* @param v the object to check for <code>null</code>
222+
* @param supplier the supplier function
223+
* @return the value returned by the supplier if the object is not <code>null</code> or <code>null</code> otherwise.
224+
*/
168225
public static <T, R> R getIfNotNull( T v, UnsafeSupplier<R, Exception> supplier ) {
169226
return getOnCondition( () -> v != null , supplier );
170227
}

fj-core/src/test/java/test/org/fugerit/java/core/function/TestSafeFunction.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.function.Consumer;
99

1010
import org.fugerit.java.core.cfg.ConfigRuntimeException;
11+
import org.fugerit.java.core.db.dao.DAOException;
12+
import org.fugerit.java.core.db.dao.DAORuntimeException;
1113
import org.fugerit.java.core.function.SafeFunction;
1214
import org.fugerit.java.core.xml.XMLException;
1315
import org.junit.Assert;
@@ -19,6 +21,10 @@
1921
@Slf4j
2022
public class TestSafeFunction {
2123

24+
private static final DAOException TEST_CHECKED_EX = new DAOException( "test checked" );
25+
26+
private static final DAORuntimeException TEST_RUNTIME_EX = new DAORuntimeException( "test runtime" );
27+
2228
@Test
2329
public void testFailGet() {
2430
Assert.assertThrows(ConfigRuntimeException.class, () -> {
@@ -107,6 +113,22 @@ public void testExHandler() {
107113
Assert.assertTrue( this.testExHandlerWorker( SafeFunction.EX_CONSUMER_THROW_CONFIG_RUNTIME, true ) );
108114
}
109115

116+
@Test
117+
public void testExHandlerThrowConfigRuntime() {
118+
Assert.assertThrows( ConfigRuntimeException.class ,
119+
() -> SafeFunction.EX_CONSUMER_THROW_CONFIG_RUNTIME.accept( TEST_RUNTIME_EX ) );
120+
Assert.assertThrows( ConfigRuntimeException.class ,
121+
() -> SafeFunction.EX_CONSUMER_THROW_CONFIG_RUNTIME.accept( TEST_CHECKED_EX ) );
122+
}
123+
124+
@Test
125+
public void testExHandlerThrowConfigRuntimeRethrowRTE() {
126+
Assert.assertThrows( TEST_RUNTIME_EX.getClass() ,
127+
() -> SafeFunction.EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE.accept( TEST_RUNTIME_EX ) );
128+
Assert.assertThrows( ConfigRuntimeException.class ,
129+
() -> SafeFunction.EX_CONSUMER_RETHROW_RTE_OR_CONVERT_CHECKED_TO_CRE.accept( TEST_CHECKED_EX ) );
130+
}
131+
110132
public String testExampleToOneLineClassic() {
111133
StringBuilder builder = new StringBuilder();
112134
try ( BufferedReader reader = new BufferedReader( new StringReader( "test" ) ) ) {

0 commit comments

Comments
 (0)