Skip to content

Commit d0f43e3

Browse files
committed
[bugfix] Avoid NPE under rare circumstances in translating a Java Exception to an XQuery Error Code.
This is a regression that was introduced in 91ab58f. Closes #7
1 parent bcfec79 commit d0f43e3

File tree

7 files changed

+108
-19
lines changed

7 files changed

+108
-19
lines changed

exist-core/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@
924924
<include>src/main/java/org/exist/xquery/ErrorCodes.java</include>
925925
<include>src/main/java/org/exist/xquery/FunctionFactory.java</include>
926926
<include>src/main/java/org/exist/xquery/Optimizer.java</include>
927+
<include>src/main/java/org/exist/xquery/TryCatchExpression.java</include>
927928
<include>src/main/java/org/exist/xquery/UserDefinedFunction.java</include>
928929
<include>src/main/java/org/exist/xquery/XPathUtil.java</include>
929930
<include>src/main/java/org/exist/xquery/XQueryContext.java</include>
@@ -1233,6 +1234,7 @@
12331234
<exclude>src/main/java/org/exist/xquery/Materializable.java</exclude>
12341235
<exclude>src/main/java/org/exist/xquery/NameTest.java</exclude>
12351236
<exclude>src/main/java/org/exist/xquery/Optimizer.java</exclude>
1237+
<exclude>src/main/java/org/exist/xquery/TryCatchExpression.java</exclude>
12361238
<exclude>src/main/java/org/exist/xquery/UserDefinedFunction.java</exclude>
12371239
<exclude>src/test/java/org/exist/xquery/WatchdogTest.java</exclude>
12381240
<exclude>src/main/java/org/exist/xquery/XPathUtil.java</exclude>

exist-core/src/main/java/org/exist/xquery/ErrorCodes.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import org.exist.Namespaces;
4949
import org.exist.dom.QName;
5050

51+
import javax.annotation.Nullable;
52+
5153
/**
5254
*
5355
* @author aretter
@@ -295,14 +297,14 @@ public class ErrorCodes {
295297
public static class ErrorCode {
296298

297299
private final QName errorQName;
298-
private final String description;
300+
private @Nullable final String description;
299301

300-
public ErrorCode(String code, String description) {
302+
public ErrorCode(final String code, @Nullable final String description) {
301303
this.errorQName = new QName(code, Namespaces.EXIST_XQUERY_XPATH_ERROR_NS, Namespaces.EXIST_XQUERY_XPATH_ERROR_PREFIX);
302304
this.description = description;
303305
}
304306

305-
public ErrorCode(QName errorQName, String description) {
307+
public ErrorCode(final QName errorQName, final String description) {
306308
this.errorQName = errorQName;
307309
this.description = description;
308310
}
@@ -313,10 +315,10 @@ public QName getErrorQName() {
313315

314316
@Override
315317
public String toString() {
316-
return "(" + errorQName.getNamespaceURI() + "#" + errorQName.getLocalPart() + "):" + description;
318+
return "(" + errorQName.toString() + "): " + description;
317319
}
318320

319-
public String getDescription(){
321+
public @Nullable String getDescription(){
320322
return description;
321323
}
322324
}
@@ -337,12 +339,21 @@ private EXistErrorCode(String code, String description) {
337339

338340
public static class JavaErrorCode extends ErrorCode {
339341

340-
public JavaErrorCode(final Throwable throwable) {
341-
super(new QName(
342-
throwable.getClass().getName(),
343-
Namespaces.EXIST_JAVA_BINDING_NS,
344-
Namespaces.EXIST_JAVA_BINDING_NS_PREFIX),
345-
throwable.getMessage() != null ? throwable.getMessage() : throwable.getCause().getMessage());
342+
private JavaErrorCode(final QName errorQName, @Nullable final String description) {
343+
super(errorQName, description);
344+
}
345+
346+
public static JavaErrorCode fromThrowable(final Throwable throwable) {
347+
final QName errorQName = new QName(throwable.getClass().getName(), Namespaces.EXIST_JAVA_BINDING_NS, Namespaces.EXIST_JAVA_BINDING_NS_PREFIX);
348+
@Nullable final String description;
349+
if (throwable.getMessage() != null) {
350+
description = throwable.getMessage();
351+
} else if (throwable.getCause() != null) {
352+
description = throwable.getCause().getMessage();
353+
} else {
354+
description = null;
355+
}
356+
return new JavaErrorCode(errorQName, description);
346357
}
347358
}
348359
}

exist-core/src/main/java/org/exist/xquery/JavaBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
558558
return XPathUtil.javaObjectToXPath(javaResult, context, true, false, false, this);
559559

560560
} catch (final IllegalAccessException | InvocationTargetException | InstantiationException | IllegalArgumentException e) {
561-
throw new XPathException(this, new ErrorCodes.JavaErrorCode(e), e.getMessage());
561+
throw new XPathException(this, ErrorCodes.JavaErrorCode.fromThrowable(e), e.getMessage());
562562
}
563563
}
564564

exist-core/src/main/java/org/exist/xquery/TryCatchExpression.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -166,7 +190,7 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem) thr
166190
}
167191
} else {
168192
// Get errorcode from all other errors and exceptions
169-
errorCode = new JavaErrorCode(throwable);
193+
errorCode = JavaErrorCode.fromThrowable(throwable);
170194
}
171195

172196
// We need the qname in the end
@@ -414,7 +438,7 @@ private ErrorCode extractErrorCode(final XPathException xpe) {
414438
}
415439

416440
// Fallback, create java error
417-
return new ErrorCodes.JavaErrorCode(retVal);
441+
return JavaErrorCode.fromThrowable(retVal);
418442
}
419443

420444
@Override

extensions/modules/exi/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@
139139
-->
140140
<header>${project.parent.relativePath}/../../elemental-parent/elemental-LGPL-21-ONLY-license.template.txt</header>
141141
<excludes>
142+
<exclude>exi-functions-demo.xql</exclude>
142143
<exclude>pom.xml</exclude>
143144
<exclude>src/**</exclude>
144-
<exclude>exi-functions-demo.xql</exclude>
145145
</excludes>
146146
</licenseSet>
147147

@@ -160,6 +160,8 @@
160160
</multi>
161161
<includes>
162162
<include>pom.xml</include>
163+
<include>src/main/java/org/exist/xquery/modules/exi/DecodeExiFunction.java</include>
164+
<include>src/main/java/org/exist/xquery/modules/exi/EncodeExiFunction.java</include>
163165
</includes>
164166
</licenseSet>
165167

@@ -170,6 +172,8 @@
170172
<header>${project.parent.relativePath}/../../exist-parent/existdb-LGPL-21-license.template.txt</header>
171173
<excludes>
172174
<exclude>pom.xml</exclude>
175+
<exclude>src/main/java/org/exist/xquery/modules/exi/DecodeExiFunction.java</exclude>
176+
<exclude>src/main/java/org/exist/xquery/modules/exi/EncodeExiFunction.java</exclude>
173177
</excludes>
174178
</licenseSet>
175179

extensions/modules/exi/src/main/java/org/exist/xquery/modules/exi/DecodeExiFunction.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -125,8 +149,8 @@ public Sequence eval(Sequence[] args, Sequence contextSequence)
125149
context.popDocumentContext();
126150
}
127151
}
128-
catch(EXIException | SAXException | IOException exie) {
129-
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
152+
catch(final EXIException | SAXException | IOException exie) {
153+
throw new XPathException(this, JavaErrorCode.fromThrowable(exie.getCause()), exie.getMessage());
130154
}
131155
}
132156

extensions/modules/exi/src/main/java/org/exist/xquery/modules/exi/EncodeExiFunction.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -114,8 +138,8 @@ public Sequence eval(Sequence[] args, Sequence contextSequence)
114138
// TODO - test!
115139
throw new XPathException(this, ErrorCodes.FODC0002, ioex.getMessage());
116140
}
117-
catch(EXIException | SAXException exie) {
118-
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
141+
catch(final EXIException | SAXException exie) {
142+
throw new XPathException(this, JavaErrorCode.fromThrowable(exie.getCause()), exie.getMessage());
119143
}
120144
}
121145

0 commit comments

Comments
 (0)