Skip to content

Commit 85f8e58

Browse files
authored
Merge pull request #3 from lfir/feat/post-reqs
Add utility method to log and throw XMLParseException
2 parents e4e4af5 + e8600ee commit 85f8e58

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

src/main/java/cf/maybelambda/httpvalidator/springboot/persistence/XMLErrorHandler.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,57 @@
55
import org.xml.sax.ErrorHandler;
66
import org.xml.sax.SAXParseException;
77

8+
import javax.management.modelmbean.XMLParseException;
9+
810
/**
9-
* Custom error handler for XML schema validation errors.
11+
* Custom error handler for XML schema validation errors. Also contains utility methods related to error handling.
1012
*/
1113
public class XMLErrorHandler implements ErrorHandler {
1214
private static Logger logger = LoggerFactory.getLogger(XMLErrorHandler.class);
1315
private static final String logmsg = "Warning during Schema Validation of the datafile";
1416

17+
/**
18+
* Functional interface representing a function that accepts one argument and produces a result,
19+
* potentially throwing an exception during the process.
20+
*
21+
* <p>This interface is similar to {@link java.util.function.Function}, but it allows for checked
22+
* exceptions to be thrown during the function application.
23+
*
24+
* @param <T> the type of the input to the function
25+
* @param <R> the type of the result of the function
26+
*/
27+
@FunctionalInterface
28+
interface ThrowingFunction<T, R> {
29+
R apply(T arg) throws Exception;
30+
}
31+
32+
/**
33+
* A utility method to apply a function that may throw an exception, with additional exception handling.
34+
*
35+
* <p>This method attempts to apply the provided function to the given argument. If an exception occurs,
36+
* it logs the error message along with the exception and then throws an {@code XMLParseException} with
37+
* the same data.
38+
*
39+
* @param <T> the type of the input to the function
40+
* @param <R> the type of the result of the function
41+
* @param f the function to be applied, which may throw an exception
42+
* @param arg the argument to be passed to the function
43+
* @param logger the logger to be used
44+
* @param msg the error message to be logged and included in the thrown {@code XMLParseException}
45+
* @return the result of the function application
46+
* @throws XMLParseException if the function throws any exception during its application
47+
*
48+
* @see ThrowingFunction
49+
*/
50+
static <T, R> R parseInputOrThrow(ThrowingFunction<T, R> f, T arg, Logger logger, String msg) throws XMLParseException {
51+
try {
52+
return f.apply(arg);
53+
} catch (Exception e) {
54+
logger.error(msg, e);
55+
throw new XMLParseException(e, msg + "\n");
56+
}
57+
}
58+
1559
/**
1660
* Handles warnings encountered during XML schema validation.
1761
* The exception is logged but not thrown again.

src/main/java/cf/maybelambda/httpvalidator/springboot/persistence/XMLValidationTaskDao.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cf.maybelambda.httpvalidator.springboot.persistence;
22

33
import cf.maybelambda.httpvalidator.springboot.model.ValidationTask;
4-
import com.fasterxml.jackson.core.JsonProcessingException;
54
import com.fasterxml.jackson.databind.JsonNode;
65
import com.fasterxml.jackson.databind.ObjectMapper;
76
import org.slf4j.Logger;
@@ -35,6 +34,7 @@
3534
import java.util.ArrayList;
3635
import java.util.List;
3736

37+
import static cf.maybelambda.httpvalidator.springboot.persistence.XMLErrorHandler.parseInputOrThrow;
3838
import static java.util.Objects.requireNonNull;
3939

4040
/**
@@ -104,13 +104,7 @@ public XMLValidationTaskDao() throws ParserConfigurationException, SAXException,
104104
* @throws XMLParseException if parsing fails.
105105
*/
106106
Document parseXMLInput(InputStream inputStream) throws XMLParseException {
107-
try {
108-
return this.xmlParser.parse(inputStream);
109-
} catch (Exception e) {
110-
String errmsg = "Failed to parse target XML content";
111-
logger.error(errmsg, e);
112-
throw new XMLParseException(e, errmsg + "\n");
113-
}
107+
return parseInputOrThrow(this.xmlParser::parse, inputStream, logger, "Failed to parse target XML content");
114108
}
115109

116110
/**
@@ -151,7 +145,7 @@ synchronized Document getDocData() throws XMLParseException, FileNotFoundExcepti
151145
* @return The new validation task.
152146
* @throws XMLParseException if JSON content in the reqbody element cannot be parsed.
153147
*/
154-
private ValidationTask createVTaskFromNodes(NodeList validation) throws XMLParseException {
148+
ValidationTask createVTaskFromNodes(NodeList validation) throws XMLParseException {
155149
MethodType method = null;
156150
String url = null;
157151
List<String> headers = new ArrayList<>();
@@ -174,13 +168,7 @@ private ValidationTask createVTaskFromNodes(NodeList validation) throws XMLParse
174168
headers.add(content);
175169
}
176170
if (REQ_BODY_TAG.equals(name)) {
177-
try {
178-
reqBody = this.mapper.readTree(content);
179-
} catch (JsonProcessingException e) {
180-
String errmsg = "Invalid JSON content encountered in the data file";
181-
logger.error(errmsg, e);
182-
throw new XMLParseException(e, errmsg + "\n");
183-
}
171+
reqBody = parseInputOrThrow(this.mapper::readTree, content, logger, "Invalid JSON encountered in data file");
184172
}
185173
if (RES_TAG.equals(name)) {
186174
resStatusCode = Integer.parseInt(attrs.getNamedItem(RES_SC_ATTR).getTextContent());

0 commit comments

Comments
 (0)