Skip to content

Commit c2d9f54

Browse files
committed
[bugfix] fn:transform - clean up error handling
When the cause chain for an error contains an XPathException in native eXist, or the Saxon type for an XPathException, work harder to get the correct error code out of that cause, and reflect it in the XPathException ultimately thrown out of FnTransform. This has an effect on the error thrown by XQTS test transform-68, which becomes consistent with the proposed test suite fix here w3c/qt3tests#44
1 parent 9543567 commit c2d9f54

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FnTransform.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,8 @@ private XsltExecutable compileExecutable(final Options options) throws XPathExce
178178
FnTransform.ERROR_LISTENER.clear();
179179
return xsltCompiler.compile(options.xsltSource._2); // .compilePackage //TODO(AR) need to implement support for xslt-packages
180180
} catch (final SaxonApiException e) {
181-
//Generated by us, possibly in URI resolution, above - throw it directly
182-
throwOriginalXPathException(e, ErrorCodes.FOXT0003);
183-
final Optional<TransformerException> compilerException = FnTransform.ERROR_LISTENER.getWorst();
184-
if (compilerException.isPresent()) {
185-
throwOriginalXPathException(compilerException.get(), ErrorCodes.FOXT0003);
186-
throw new XPathException(this, ErrorCodes.FOXT0003, compilerException.get());
187-
}
188-
//Wrap any other error
189-
throw new XPathException(this, ErrorCodes.FOXT0003, e.getMessage());
181+
final Optional<Exception> compilerException = FnTransform.ERROR_LISTENER.getWorst().map(e1 -> e1);
182+
throw originalXPathException(compilerException.orElse(e), ErrorCodes.FOXT0003);
190183
}
191184
}
192185

@@ -199,11 +192,11 @@ private XsltExecutable compileExecutable(final Options options) throws XPathExce
199192
* @param defaultErrorCode use this code and its description to fill in blanks in what we finally throw
200193
* @throws XPathException the eventual eXist exception we may throw
201194
*/
202-
private void throwOriginalXPathException(final Throwable e, final ErrorCodes.ErrorCode defaultErrorCode) throws XPathException {
195+
private XPathException originalXPathException(final Throwable e, final ErrorCodes.ErrorCode defaultErrorCode) {
203196
Throwable cause = e;
204197
while (cause != null) {
205198
if (cause instanceof XPathException) {
206-
throw (XPathException) cause;
199+
return (XPathException) cause;
207200
}
208201
cause = cause.getCause();
209202
}
@@ -215,14 +208,16 @@ private void throwOriginalXPathException(final Throwable e, final ErrorCodes.Err
215208
final StructuredQName from = xPathException.getErrorCodeQName();
216209
if (from != null) {
217210
final QName errorCodeQName = new QName(from.getLocalPart(), from.getURI(), from.getPrefix());
218-
final ErrorCodes.ErrorCode errorCode = new ErrorCodes.ErrorCode(errorCodeQName, defaultErrorCode.getDescription());
219-
throw new XPathException(errorCode, cause.getMessage());
211+
final ErrorCodes.ErrorCode errorCode = new ErrorCodes.ErrorCode(errorCodeQName, cause.getMessage());
212+
return new XPathException(errorCode, cause.getMessage());
220213
} else {
221-
throw new XPathException(this, defaultErrorCode, cause.getMessage());
214+
return new XPathException(this, defaultErrorCode, cause.getMessage());
222215
}
223216
}
224217
cause = cause.getCause();
225218
}
219+
220+
return new XPathException(this, defaultErrorCode, e.getMessage());
226221
}
227222

228223
/**
@@ -339,16 +334,8 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
339334
options, sourceNode, delivery, xslt30Transformer, resultDocuments);
340335
return invocation.invoke();
341336

342-
} catch (final SaxonApiException e) {
343-
if (e.getErrorCode() != null) {
344-
final QName errorQn = QName.fromJavaQName(e.getErrorCode().getStructuredQName().toJaxpQName());
345-
throw new XPathException(this, new ErrorCodes.ErrorCode(errorQn, e.getMessage()), e.getMessage());
346-
} else {
347-
throwOriginalXPathException(e, ErrorCodes.FOXT0003);
348-
throw new XPathException(this, ErrorCodes.FOXT0003, e.getMessage());
349-
}
350-
} catch (final UncheckedXPathException e) {
351-
throw new XPathException(this, ErrorCodes.FOXT0003, e.getMessage());
337+
} catch (final SaxonApiException | UncheckedXPathException e) {
338+
throw originalXPathException(e, ErrorCodes.FOXT0003);
352339
}
353340

354341
} else {

0 commit comments

Comments
 (0)