1313@ Slf4j
1414public class ExceptionHandler {
1515
16-
1716 public static <T > T executeWithExceptionHandling (String operation , String param , Supplier <T > operationSupplier ) {
1817 try {
1918 return operationSupplier .get ();
20- } catch (IllegalArgumentException e ) {
21- log .error ("Invalid arguments provided for {}: {}" , operation , param , e );
22- throw new IllegalArgumentException ("Invalid arguments for " + operation + ": " + param , e );
23- } catch (IllegalStateException e ) {
24- log .error ("Invalid state during {}: {}" , operation , param , e );
25- throw new IllegalStateException ("Invalid state during " + operation + ": " + param , e );
26- } catch (RuntimeException e ) {
27- log .error ("Runtime error during {}: {}" , operation , param , e );
28- throw new RuntimeException ("Runtime error during " + operation + ": " + param , e );
19+ } catch (Exception e ) {
20+ throw handleException (e , operation , param );
2921 }
3022 }
3123
32-
3324 public static void executeWithExceptionHandling (String operation , String param , Runnable operationRunnable ) {
3425 try {
3526 operationRunnable .run ();
36- } catch (IllegalArgumentException e ) {
37- log .error ("Invalid arguments provided for {}: {}" , operation , param , e );
38- throw new IllegalArgumentException ("Invalid arguments for " + operation + ": " + param , e );
39- } catch (IllegalStateException e ) {
40- log .error ("Invalid state during {}: {}" , operation , param , e );
41- throw new IllegalStateException ("Invalid state during " + operation + ": " + param , e );
42- } catch (RuntimeException e ) {
43- log .error ("Runtime error during {}: {}" , operation , param , e );
44- throw new RuntimeException ("Runtime error during " + operation + ": " + param , e );
27+ } catch (Exception e ) {
28+ throw handleException (e , operation , param );
29+ }
30+ }
31+
32+ private static RuntimeException handleException (Exception e , String operation , String param ) {
33+ switch (e ) {
34+ case IllegalArgumentException illegalArgumentException -> {
35+ log .error ("Invalid arguments provided for {}: {}" , operation , param , e );
36+ return new IllegalArgumentException ("Invalid arguments for " + operation + ": " + param , e );
37+ }
38+ case IllegalStateException illegalStateException -> {
39+ log .error ("Invalid state during {}: {}" , operation , param , e );
40+ return new IllegalStateException ("Invalid state during " + operation + ": " + param , e );
41+ }
42+ case RuntimeException runtimeException -> {
43+ log .error ("Runtime error during {}: {}" , operation , param , e );
44+ return new RuntimeException ("Runtime error during " + operation + ": " + param , e );
45+ }
46+ case null , default -> {
47+ log .error ("Unexpected error during {}: {}" , operation , param , e );
48+ return new RuntimeException ("Unexpected error during " + operation + ": " + param , e );
49+ }
4550 }
4651 }
4752}
0 commit comments