Skip to content

Commit

Permalink
Refactor exception resolvers for flexibility
Browse files Browse the repository at this point in the history
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
caring-coder committed Sep 22, 2024
1 parent 0a26ba6 commit 580081a
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public interface OfficeStamperConfiguration {
* Checks if the failOnUnresolvedExpression flag is set to true or false.
*
* @return true if failOnUnresolvedExpression is set to true, false otherwise.
*
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true)
boolean isFailOnUnresolvedExpression();
Expand All @@ -26,6 +32,11 @@ public interface OfficeStamperConfiguration {
* @param failOnUnresolvedExpression flag indicating whether to fail on unresolved expressions
*
* @return the updated OfficeStamperConfiguration object
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnresolvedExpression);
Expand All @@ -34,20 +45,35 @@ public interface OfficeStamperConfiguration {
* Determines whether to leave empty on expression error.
*
* @return true if expression errors are left empty, false otherwise
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true) boolean isLeaveEmptyOnExpressionError();

/**
* Determines whether unresolved expressions in the OfficeStamper configuration should be replaced.
*
* @return true if unresolved expressions should be replaced, false otherwise.
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true) boolean isReplaceUnresolvedExpressions();

/**
* Retrieves the default value for unresolved expressions.
*
* @return the default value for unresolved expressions
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true) String getUnresolvedExpressionsDefaultValue();

Expand All @@ -57,6 +83,11 @@ public interface OfficeStamperConfiguration {
* @param unresolvedExpressionsDefaultValue the default value for unresolved expressions
*
* @return the updated OfficeStamperConfiguration object
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration unresolvedExpressionsDefaultValue(String unresolvedExpressionsDefaultValue);
Expand All @@ -67,6 +98,11 @@ public interface OfficeStamperConfiguration {
* @param replaceUnresolvedExpressions flag indicating whether to replace unresolved expressions
*
* @return the updated OfficeStamperConfiguration object
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration replaceUnresolvedExpressions(
Expand All @@ -79,6 +115,11 @@ OfficeStamperConfiguration replaceUnresolvedExpressions(
* @param leaveEmpty boolean value indicating whether to leave empty on expression error
*
* @return the updated OfficeStamperConfiguration object
* @deprecated This method is deprecated because it offers limited functionality by just checking a flag.
* It is replaced by {@link #setExceptionResolver(ExceptionResolver)} , which provides
* complete customization over the behavior during resolution failures. The new method
* allows you to define how unresolved expressions are handled in a more flexible and
* comprehensive manner.
*/
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration leaveEmptyOnExpressionError(boolean leaveEmpty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public DocxStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnre
}

private ExceptionResolver computeExceptionResolver(boolean tracing) {
if (failOnUnresolvedExpression) return new ThrowingResolver(tracing);
if (replaceWithDefaultOnError()) return new DefaultingResolver(replacementDefault(), tracing);
return new PassingResolver(tracing);
if (failOnUnresolvedExpression) return new ExceptionResolvers.ThrowingResolver(tracing);
if (replaceWithDefaultOnError()) return new ExceptionResolvers.DefaultingResolver(replacementDefault(), tracing);
return new ExceptionResolvers.PassingResolver(tracing);
}

private boolean replaceWithDefaultOnError() {
Expand Down

This file was deleted.

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);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ private static Arguments expressionReplacementInGlobalParagraphsTest()
""";
OfficeStamperConfiguration config = OfficeStamperConfigurations
.standard()
.setExceptionResolver(new PassingResolver());
.setExceptionResolver(ExceptionResolvers.passing());
return arguments("expressionReplacementInGlobalParagraphsTest", config, context, template, expected);
}

Expand Down Expand Up @@ -1181,7 +1181,7 @@ private static Arguments expressionReplacementInTablesTest() {
""";
var config = OfficeStamperConfigurations
.standard()
.setExceptionResolver(new PassingResolver());
.setExceptionResolver(ExceptionResolvers.passing());
return arguments("expressionReplacementInTablesTest", config, context, template, expected);
}

Expand Down Expand Up @@ -1248,7 +1248,7 @@ private static Arguments expressionReplacementWithCommentTest() {
""";
var config = OfficeStamperConfigurations
.standard()
.setExceptionResolver(new PassingResolver());
.setExceptionResolver(ExceptionResolvers.passing());
return arguments("expressionReplacementWithCommentsTest", config, context, template, expected);
}

Expand Down Expand Up @@ -1296,7 +1296,7 @@ private static Arguments leaveEmptyOnExpressionErrorTest() {
""";
var config = OfficeStamperConfigurations
.standard()
.setExceptionResolver(new DefaultingResolver());
.setExceptionResolver(ExceptionResolvers.defaulting());
return arguments("leaveEmptyOnExpressionErrorTest", config, context, template, expected);
}

Expand Down Expand Up @@ -1342,10 +1342,11 @@ private static Arguments mapAccessorAndReflectivePropertyAccessorTest_shouldReso
""";

var defaultValue = "N/C";
var config = OfficeStamperConfigurations.standard()
.setLineBreakPlaceholder("\n")
.addResolver(Resolvers.nullToDefault("N/C"))
.setExceptionResolver(new DefaultingResolver("N/C"))
.addResolver(Resolvers.nullToDefault(defaultValue))
.setExceptionResolver(ExceptionResolvers.defaulting(defaultValue))
.setEvaluationContextConfigurer(ctx -> ctx.addPropertyAccessor(new MapAccessor()));

return arguments("mapAccessorAndReflectivePropertyAccessorTest_shouldResolveMapAndPropertyPlaceholders",
Expand Down Expand Up @@ -1375,7 +1376,7 @@ private static Arguments nullPointerResolutionTest_testWithDefaultSpel() {

var config = OfficeStamperConfigurations
.standard()
.setExceptionResolver(new PassingResolver());
.setExceptionResolver(ExceptionResolvers.passing());

return arguments("nullPointerResolutionTest_testWithDefaultSpel", config, context, template, expected);
}
Expand Down
Loading

0 comments on commit 580081a

Please sign in to comment.