| 1 | package pro.verron.officestamper.preset; | |
| 2 | ||
| 3 | import org.slf4j.Logger; | |
| 4 | import org.slf4j.LoggerFactory; | |
| 5 | import pro.verron.officestamper.api.ExceptionResolver; | |
| 6 | import pro.verron.officestamper.api.Insert; | |
| 7 | import pro.verron.officestamper.api.OfficeStamperException; | |
| 8 | ||
| 9 | import static pro.verron.officestamper.utils.wml.WmlFactory.newRun; | |
| 10 | ||
| 11 | ||
| 12 | /// The ExceptionResolvers class provides a set of static factory methods to create different types of ExceptionResolver | |
| 13 | /// implementations. These resolvers are designed to handle exceptions that occur during the processing of placeholders | |
| 14 | /// in text documents. This class is a utility class and cannot be instantiated. | |
| 15 | public class ExceptionResolvers { | |
| 16 | ||
| 17 | private static final Logger logger = LoggerFactory.getLogger(ExceptionResolvers.class); | |
| 18 | ||
| 19 | static { | |
| 20 | if (!logger.isTraceEnabled()) | |
| 21 | logger.info("Set TRACE log level, to add stacktrace info to resolution exceptions"); | |
| 22 | } | |
| 23 | ||
| 24 | private ExceptionResolvers() { | |
| 25 | throw new OfficeStamperException("Utility class"); | |
| 26 | } | |
| 27 | ||
| 28 | /// The passing resolver will handle exceptions by returning the placeholder expression. It logs the exception | |
| 29 | /// message and the stack trace if tracing is enabled. | |
| 30 | /// | |
| 31 | /// @return An instance of `ExceptionResolver` that returns the placeholder expression. | |
| 32 | public static ExceptionResolver passing() { | |
| 33 | return new PassingResolver(logger.isTraceEnabled(), "${%s}"); | |
| 34 | } | |
| 35 | ||
| 36 | /// The defaulting resolver class will handle exceptions by returning an empty string. It logs the exception message | |
| 37 | /// and the stack trace if tracing is enabled. | |
| 38 | /// | |
| 39 | /// @return An instance of `ExceptionResolver` that returns an empty string. | |
| 40 | public static ExceptionResolver defaulting() { | |
| 41 | return new DefaultingResolver("", logger.isTraceEnabled()); | |
| 42 | } | |
| 43 | ||
| 44 | /// The defaulting resolver class will handle exceptions by returning a default value. It logs the exception message | |
| 45 | /// and the stack trace if tracing is enabled. | |
| 46 | /// | |
| 47 | /// @param value The default value to be returned if an exception occurs. | |
| 48 | /// | |
| 49 | /// @return An instance of `ExceptionResolver` that returns a default value. | |
| 50 | public static ExceptionResolver defaulting(String value) { | |
| 51 | return new DefaultingResolver(value, logger.isTraceEnabled()); | |
| 52 | } | |
| 53 | ||
| 54 | /// The throwing resolver will handle exceptions by immediately throwing an OfficeStamperException. It is used to | |
| 55 | /// propagate errors encountered during the processing of placeholders in text documents. | |
| 56 | /// | |
| 57 | /// @return An instance of `ExceptionResolver` that throws an exception. | |
| 58 | public static ExceptionResolver throwing() { | |
| 59 | return new ThrowingResolver(logger.isTraceEnabled()); | |
| 60 | } | |
| 61 | ||
| 62 | private record DefaultingResolver(String value, boolean tracing) | |
| 63 | implements ExceptionResolver { | |
| 64 | ||
| 65 | private static final Logger logger = LoggerFactory.getLogger(DefaultingResolver.class); | |
| 66 | ||
| 67 | @Override | |
| 68 | public Insert resolve(String expression, String message, Exception cause) { | |
| 69 | if (tracing) logger.warn(message, cause); | |
| 70 | else logger.warn(message); | |
| 71 |
1
1. resolve : replaced return value with null for pro/verron/officestamper/preset/ExceptionResolvers$DefaultingResolver::resolve → TIMED_OUT |
return new Insert(newRun(value)); |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | private record PassingResolver(boolean tracing, String template) | |
| 76 | implements ExceptionResolver { | |
| 77 | ||
| 78 | @Override | |
| 79 | public Insert resolve(String expression, String message, Exception cause) { | |
| 80 | if (tracing) logger.warn(message, cause); | |
| 81 | else logger.warn(message); | |
| 82 |
1
1. resolve : replaced return value with null for pro/verron/officestamper/preset/ExceptionResolvers$PassingResolver::resolve → TIMED_OUT |
return new Insert(newRun(template.formatted(expression))); |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | private record ThrowingResolver(boolean tracing) | |
| 87 | implements ExceptionResolver { | |
| 88 | ||
| 89 | @Override | |
| 90 | public Insert resolve(String expression, String message, Exception cause) { | |
| 91 |
1
1. resolve : negated conditional → SURVIVED |
if (tracing) throw new OfficeStamperException(message, cause); |
| 92 | else throw new OfficeStamperException(message); | |
| 93 | } | |
| 94 | } | |
| 95 | } | |
Mutations | ||
| 71 |
1.1 |
|
| 82 |
1.1 |
|
| 91 |
1.1 |