Skip to content

Commit

Permalink
Refactor exception handling approach for configuration
Browse files Browse the repository at this point in the history
Replaced `setFailOnUnresolvedExpression` with `setExceptionResolver` throughout the test files and configuration classes. Deprecated `isFailOnUnresolvedExpression`, `replaceUnresolvedExpressions`, and similar methods, and introduced a computed `ExceptionResolver` approach to centralize and simplify error handling logic.
  • Loading branch information
caring-coder committed Sep 20, 2024
1 parent c7184dc commit ad9b865
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,53 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.spel.SpelParserConfiguration;
import pro.verron.officestamper.preset.DefaultingResolver;
import pro.verron.officestamper.preset.PassingResolver;
import pro.verron.officestamper.preset.ThrowingResolver;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

public interface OfficeStamperConfiguration {

Logger logger = LoggerFactory.getLogger(OfficeStamperConfiguration.class);

default ExceptionResolver getExceptionResolver(boolean tracing) {
if (isFailOnUnresolvedExpression())
return new ThrowingResolver(tracing);
if (replaceWithDefaultOnError())
return new DefaultingResolver(replacementDefault(), tracing);
return new PassingResolver(tracing);

}

/**
* Checks if the failOnUnresolvedExpression flag is set to true or false.
*
* @return true if failOnUnresolvedExpression is set to true, false otherwise.
*/
@Deprecated(since = "2.5", forRemoval = true)
boolean isFailOnUnresolvedExpression();

default boolean replaceWithDefaultOnError() {
return isLeaveEmptyOnExpressionError() || isReplaceUnresolvedExpressions();
}

default String replacementDefault() {
return isLeaveEmptyOnExpressionError()
? ""
: getUnresolvedExpressionsDefaultValue();
}
/**
* Sets the failOnUnresolvedExpression flag to determine whether unresolved expressions should
* cause an exception to be thrown.
*
* @param failOnUnresolvedExpression flag indicating whether to fail on unresolved expressions
*
* @return the updated OfficeStamperConfiguration object
*/
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnresolvedExpression);

/**
* Determines whether to leave empty on expression error.
*
* @return true if expression errors are left empty, false otherwise
*/
boolean isLeaveEmptyOnExpressionError();
@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.
*/
boolean isReplaceUnresolvedExpressions();
@Deprecated(since = "2.5", forRemoval = true) boolean isReplaceUnresolvedExpressions();

/**
* Retrieves the default value for unresolved expressions.
*
* @return the default value for unresolved expressions
*/
String getUnresolvedExpressionsDefaultValue();

/**
* Sets the failOnUnresolvedExpression flag to determine whether unresolved expressions should
* cause an exception to be thrown.
*
* @param failOnUnresolvedExpression flag indicating whether to fail on unresolved expressions
*
* @return the updated OfficeStamperConfiguration object
*/
OfficeStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnresolvedExpression);
@Deprecated(since = "2.5", forRemoval = true) String getUnresolvedExpressionsDefaultValue();

/**
* Sets the default value for unresolved expressions in the OfficeStamperConfiguration object.
Expand All @@ -79,6 +58,7 @@ default String replacementDefault() {
*
* @return the updated OfficeStamperConfiguration object
*/
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration unresolvedExpressionsDefaultValue(String unresolvedExpressionsDefaultValue);

/**
Expand All @@ -88,6 +68,7 @@ default String replacementDefault() {
*
* @return the updated OfficeStamperConfiguration object
*/
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration replaceUnresolvedExpressions(
boolean replaceUnresolvedExpressions
);
Expand All @@ -99,9 +80,8 @@ OfficeStamperConfiguration replaceUnresolvedExpressions(
*
* @return the updated OfficeStamperConfiguration object
*/
OfficeStamperConfiguration leaveEmptyOnExpressionError(
boolean leaveEmpty
);
@Deprecated(since = "2.5", forRemoval = true)
OfficeStamperConfiguration leaveEmptyOnExpressionError(boolean leaveEmpty);

/**
* Exposes an interface to the expression language.
Expand All @@ -126,8 +106,7 @@ OfficeStamperConfiguration exposeInterfaceToExpressionLanguage(
* @return the updated OfficeStamperConfiguration object
*/
OfficeStamperConfiguration addCommentProcessor(
Class<?> interfaceClass,
Function<ParagraphPlaceholderReplacer, CommentProcessor> commentProcessorFactory
Class<?> interfaceClass, Function<ParagraphPlaceholderReplacer, CommentProcessor> commentProcessorFactory
);

/**
Expand All @@ -152,9 +131,7 @@ OfficeStamperConfiguration addCommentProcessor(
*
* @return the updated OfficeStamperConfiguration object
*/
OfficeStamperConfiguration setLineBreakPlaceholder(
String lineBreakPlaceholder
);
OfficeStamperConfiguration setLineBreakPlaceholder(String lineBreakPlaceholder);

/**
* Retrieves the EvaluationContextConfigurer for configuring the Spring Expression Language (SPEL) EvaluationContext
Expand Down Expand Up @@ -230,9 +207,7 @@ OfficeStamperConfiguration setSpelParserConfiguration(
*
* @return the updated OfficeStamperConfiguration instance
*/
OfficeStamperConfiguration setResolvers(
List<ObjectResolver> resolvers
);
OfficeStamperConfiguration setResolvers(List<ObjectResolver> resolvers);

/**
* Adds an ObjectResolver to the OfficeStamperConfiguration.
Expand All @@ -241,8 +216,9 @@ OfficeStamperConfiguration setResolvers(
*
* @return The updated OfficeStamperConfiguration.
*/
OfficeStamperConfiguration addResolver(
ObjectResolver resolver
);
OfficeStamperConfiguration addResolver(ObjectResolver resolver);

ExceptionResolver getExceptionResolver();

OfficeStamperConfiguration setExceptionResolver(ExceptionResolver exceptionResolver);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public DocxStamper(OfficeStamperConfiguration configuration) {
configuration.getCommentProcessors(),
configuration.getPreprocessors(),
configuration.getSpelParserConfiguration(),
configuration.getExceptionResolver(logger.isTraceEnabled()));
configuration.getExceptionResolver());
}

private DocxStamper(
Expand Down
Loading

0 comments on commit ad9b865

Please sign in to comment.