-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor exception resolvers for flexibility
Consolidate `DefaultingResolver`, `PassingResolver`, and `ThrowingResolver` into a single `ExceptionResolvers` utility class with static factory methods. Updated tests and configurations to use this new utility class, improving overall exception handling flexibility and code maintainability. Added deprecation warnings for legacy configuration methods.
- Loading branch information
1 parent
0a26ba6
commit 580081a
Showing
11 changed files
with
166 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
engine/src/main/java/pro/verron/officestamper/preset/DefaultingResolver.java
This file was deleted.
Oops, something went wrong.
104 changes: 104 additions & 0 deletions
104
engine/src/main/java/pro/verron/officestamper/preset/ExceptionResolvers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package pro.verron.officestamper.preset; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import pro.verron.officestamper.api.ExceptionResolver; | ||
import pro.verron.officestamper.api.OfficeStamperException; | ||
import pro.verron.officestamper.api.Placeholder; | ||
|
||
|
||
/** | ||
* The ExceptionResolvers class provides a set of static factory methods to create different types of ExceptionResolver | ||
* implementations. | ||
* These resolvers are designed to handle exceptions that occur during the processing of placeholders in text | ||
* documents. | ||
* This class is a utility class and cannot be instantiated. | ||
*/ | ||
public class ExceptionResolvers { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ExceptionResolvers.class); | ||
|
||
static { | ||
if (!logger.isTraceEnabled()) | ||
logger.info("Set TRACE log level, to add stacktrace info to resolution exceptions"); | ||
} | ||
|
||
private ExceptionResolvers() { | ||
throw new OfficeStamperException("Utility class"); | ||
} | ||
|
||
/** | ||
* The passing resolver will handle exceptions by returning the placeholder expression. | ||
* It logs the exception message, and the stack trace if tracing is enabled. | ||
*/ | ||
public static ExceptionResolver passing() { | ||
return new PassingResolver(logger.isTraceEnabled()); | ||
} | ||
|
||
/** | ||
* The defaulting resolver class will handle exceptions by returning an empty string. | ||
* It logs the exception message, and the stack trace if tracing is enabled. | ||
*/ | ||
public static ExceptionResolver defaulting() { | ||
return new DefaultingResolver("", logger.isTraceEnabled()); | ||
} | ||
|
||
/** | ||
* The defaulting resolver class will handle exceptions by returning a default value. | ||
* It logs the exception message, and the stack trace if tracing is enabled. | ||
*/ | ||
public static ExceptionResolver defaulting(String value) { | ||
return new DefaultingResolver(value, logger.isTraceEnabled()); | ||
} | ||
|
||
/** | ||
* The throwing resolver will handle exceptions by immediately throwing an OfficeStamperException. | ||
* It is used to propagate errors encountered during the processing of placeholders in text documents. | ||
*/ | ||
public static ExceptionResolver throwing() { | ||
return new ThrowingResolver(logger.isTraceEnabled()); | ||
} | ||
|
||
public static ExceptionResolver legacyBehavior( | ||
boolean shouldFail, | ||
boolean emptyOnError, | ||
boolean shouldReplace, | ||
String replacementValue | ||
) { | ||
if (shouldFail) return new ThrowingResolver(logger.isTraceEnabled()); | ||
if (emptyOnError) return new DefaultingResolver("", logger.isTraceEnabled()); | ||
if (shouldReplace) return new DefaultingResolver(replacementValue, logger.isTraceEnabled()); | ||
return new PassingResolver(logger.isTraceEnabled()); | ||
} | ||
|
||
private record DefaultingResolver(String value, boolean tracing) | ||
implements ExceptionResolver { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultingResolver.class); | ||
|
||
@Override public String resolve(Placeholder placeholder, String message, Exception cause) { | ||
if (tracing) logger.warn(message, cause); | ||
else logger.warn(message); | ||
return value; | ||
} | ||
} | ||
|
||
private record PassingResolver(boolean tracing) | ||
implements ExceptionResolver { | ||
|
||
@Override public String resolve(Placeholder placeholder, String message, Exception cause) { | ||
if (tracing) logger.warn(message, cause); | ||
else logger.warn(message); | ||
return placeholder.expression(); | ||
} | ||
} | ||
|
||
private record ThrowingResolver(boolean tracing) | ||
implements ExceptionResolver { | ||
|
||
@Override public String resolve(Placeholder placeholder, String message, Exception cause) { | ||
if (tracing) throw new OfficeStamperException(message, cause); | ||
else throw new OfficeStamperException(message); | ||
} | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
engine/src/main/java/pro/verron/officestamper/preset/PassingResolver.java
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
engine/src/main/java/pro/verron/officestamper/preset/ThrowingResolver.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.