Skip to content

Commit b3e23e0

Browse files
committed
refactor: use switch expression for exception handling in ExceptionHandler
- Replace if-else chain with Java switch expression in handleException method - Normalize engine type configuration to handle case and whitespace - Improves readability and maintainability of centralized exception handling logic
1 parent 585b0e4 commit b3e23e0

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/main/java/org/mifos/workflow/core/engine/WorkflowEngineFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public WorkflowEngineFactory(WorkflowConfig properties, FlowableWorkflowEngine f
3232

3333
@PostConstruct
3434
private void init() {
35-
String engineType = properties.getEngine().getType().toLowerCase();
35+
String engineType = properties.getEngine().getType().trim().toLowerCase();
3636
logger.info("Initializing workflow engine of type: {}", engineType);
3737
switch (engineType) {
3838
case "flowable":

src/main/java/org/mifos/workflow/util/ExceptionHandler.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,40 @@
1313
@Slf4j
1414
public 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

Comments
 (0)