Skip to content

Commit 2e24729

Browse files
committed
Enhanced the MX parser log verbosity when parsing malformed content
1 parent ec56748 commit 2e24729

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Prowide ISO 20022 - CHANGELOG
22

3+
#### 9.5.7 - SNAPSHOT
4+
* Enhanced the MX parser log verbosity when parsing malformed content
5+
36
#### 9.5.6 - January 2025
47
* Changed the `MxParseUtils` findByTags and findByPath methods to return the element values instead of the XML stream object
58

iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxReadImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public static AbstractMX parse(
9393

9494
return mx.orElse(null);
9595

96+
} catch (AssertionError e) {
97+
// depending on the jaxb implementation used, the assertion error can be thrown when the XML is not valid
98+
log.log(Level.SEVERE, "Error parsing XML", e);
99+
return null;
96100
} catch (final Exception e) {
97101
MxParseUtils.handleParseException(e);
98102
return null;

iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/NamespaceAndElementFilter.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
* @since 9.2.1
4646
*/
4747
public class NamespaceAndElementFilter extends XMLFilterImpl {
48-
private static final transient Logger log = Logger.getLogger(NamespaceAndElementFilter.class.getName());
48+
private static final Logger log = Logger.getLogger(NamespaceAndElementFilter.class.getName());
4949

5050
private String mainNamespace;
5151
private boolean inElementToPropagate = false;
52-
private String localNameToPropagate;
52+
private final String localNameToPropagate;
5353
private boolean inInnerElementToSkip = false;
5454
private String localNameToSkip;
55-
private boolean unbindNamespace;
55+
private final boolean unbindNamespace;
5656

5757
/**
5858
* @param localName the XML's element to propagate
@@ -91,7 +91,10 @@ public void startElement(String namespace, String localName, String prefix, Attr
9191
try {
9292
super.startElement(namespaceToPropagate, localName, prefix, attributes);
9393
} catch (Exception e) {
94-
log.log(Level.WARNING, "Error parsing " + localName + " [" + namespace + "] element", e);
94+
log.warning("Error parsing " + localName + " [" + namespace + "] element: " + exceptionMessage(e));
95+
if (log.isLoggable(Level.FINEST)) {
96+
log.log(Level.FINEST, "Error parsing " + localName + " [" + namespace + "] element", e);
97+
}
9598
}
9699
} else {
97100
// we have found an element within the structure to propagate with a not recognized namespace
@@ -150,7 +153,10 @@ public void endElement(String namespace, String localName, String prefix) throws
150153
try {
151154
super.endElement(namespaceToPropagate, localName, prefix);
152155
} catch (Exception e) {
153-
log.log(Level.WARNING, "Error parsing " + localName + " [" + namespace + "] element", e);
156+
log.warning("Error parsing " + localName + " [" + namespace + "] element: " + exceptionMessage(e));
157+
if (log.isLoggable(Level.FINEST)) {
158+
log.log(Level.FINEST, "Error parsing " + localName + " [" + namespace + "] element", e);
159+
}
154160
}
155161
}
156162
}
@@ -169,9 +175,23 @@ public void startPrefixMapping(String prefix, String url) throws SAXException {
169175
try {
170176
super.startPrefixMapping(prefix, url);
171177
} catch (Exception e) {
172-
log.log(Level.WARNING, "Error parsing " + prefix + " [" + url + "] prefix mapping", e);
178+
log.warning("Error parsing " + prefix + " [" + url + "] prefix mapping: " + exceptionMessage(e));
179+
if (log.isLoggable(Level.FINEST)) {
180+
log.log(Level.FINEST, "Error parsing " + prefix + " [" + url + "] prefix mapping", e);
181+
}
173182
}
174183
}
175184
}
176185
}
186+
187+
private static String exceptionMessage(Exception e) {
188+
String message = e.getMessage();
189+
if (message == null && e.getCause() != null) {
190+
message = e.getCause().getMessage();
191+
}
192+
if (message == null) {
193+
message = e.getClass().getSimpleName();
194+
}
195+
return message;
196+
}
177197
}

0 commit comments

Comments
 (0)