Skip to content

Commit a4979fb

Browse files
committed
[refactor] Cleanup Error Code classes
1 parent 0cc7fdf commit a4979fb

File tree

40 files changed

+2087
-459
lines changed

40 files changed

+2087
-459
lines changed

exist-core/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@
12231223
<include>src/main/java/org/exist/xquery/functions/fn/transform/Transform.java</include>
12241224
<include>src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java</include>
12251225
<include>src/main/java/org/exist/xquery/functions/integer/WordPicture.java</include>
1226+
<include>src/main/java/org/exist/xquery/functions/map/MapExpr.java</include>
12261227
<include>src/test/java/org/exist/xquery/functions/request/GetHeaderTest.java</include>
12271228
<include>src/test/java/org/exist/xquery/functions/securitymanager/GetPermissionsTest.java</include>
12281229
<include>src/test/java/org/exist/xquery/functions/securitymanager/GroupManagementFunctionRemoveGroupTest.java</include>
@@ -1863,6 +1864,7 @@
18631864
<exclude>src/main/java/org/exist/xquery/functions/fn/transform/Transform.java</exclude>
18641865
<exclude>src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java</exclude>
18651866
<exclude>src/main/java/org/exist/xquery/functions/integer/WordPicture.java</exclude>
1867+
<exclude>src/main/java/org/exist/xquery/functions/map/MapExpr.java</exclude>
18661868
<exclude>src/main/java/org/exist/xquery/functions/map/MapType.java</exclude>
18671869
<exclude>src/test/java/org/exist/xquery/functions/request/GetHeaderTest.java</exclude>
18681870
<exclude>src/test/java/org/exist/xquery/functions/securitymanager/AccountMetadataFunctionsTest.java</exclude>

exist-core/src/main/java/org/exist/test/runner/ExtTestErrorFunction.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
package org.exist.test.runner;
4747

48+
import org.exist.dom.QName;
4849
import org.exist.xquery.ErrorCodes;
4950
import org.exist.xquery.XPathException;
5051
import org.exist.xquery.XQueryContext;
@@ -118,9 +119,14 @@ private XPathException errorMapAsXPathException(final MapType errorMap) {
118119
final String description = safeGetMapStringValue(DESCRIPTION_MAP_KEY, errorMap, "");
119120

120121
final Sequence seqErrorCode = errorMap.get(CODE_MAP_KEY);
121-
final ErrorCodes.ErrorCode errorCode;
122-
if(seqErrorCode != null && !seqErrorCode.isEmpty()) {
123-
errorCode = new ErrorCodes.ErrorCode(((QNameValue)seqErrorCode.itemAt(0)).getQName(), description);
122+
ErrorCodes.ErrorCode errorCode;
123+
if (seqErrorCode != null && !seqErrorCode.isEmpty()) {
124+
final QName errorQName = ((QNameValue)seqErrorCode.itemAt(0)).getQName();
125+
try {
126+
errorCode = ErrorCodes.fromQName(errorQName);
127+
} catch (final IllegalArgumentException e) {
128+
errorCode = new ErrorCodes.DynamicErrorCode(errorQName, description);
129+
}
124130
} else {
125131
errorCode = ErrorCodes.ERROR;
126132
}

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

Lines changed: 1488 additions & 260 deletions
Large diffs are not rendered by default.

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import java.io.PrintWriter;
5050
import java.util.ArrayList;
5151
import java.util.List;
52-
import java.util.Optional;
5352

5453
import org.apache.commons.io.output.StringBuilderWriter;
5554
import org.apache.logging.log4j.LogManager;
@@ -60,9 +59,12 @@
6059

6160
import org.exist.xquery.ErrorCodes.ErrorCode;
6261
import org.exist.xquery.ErrorCodes.JavaErrorCode;
62+
import org.exist.xquery.functions.fn.FunError;
6363
import org.exist.xquery.util.ExpressionDumper;
6464
import org.exist.xquery.value.*;
6565

66+
import javax.annotation.Nullable;
67+
6668
/**
6769
* XQuery 3.0 try {...} catch{...} expression.
6870
*
@@ -369,16 +371,22 @@ private void addErrValue(final Throwable t) throws XPathException {
369371
// description is available (for example, if the error function
370372
// was called with one argument).
371373
private void addErrDescription(final Throwable t, final ErrorCode errorCode) throws XPathException {
372-
final Optional<String> errorDesc = Optional.ofNullable(errorCode.getDescription());
373-
final Optional<String> throwableDesc = Optional.ofNullable(t instanceof XPathException ? ((XPathException) t).getDetailMessage() : t.getMessage());
374-
final Expression expression = this;
375-
final Sequence description = errorDesc
376-
.<Sequence>map(
377-
d -> new StringValue(expression, throwableDesc.filter(td -> !td.equals(d)).map(td -> d + (d.endsWith(".") ? " " : ". ") + td).orElse(d))
378-
).orElse(
379-
errorDesc.<Sequence>map(d -> new StringValue(expression, "")).orElse(Sequence.EMPTY_SEQUENCE)
380-
);
374+
@Nullable String errorDesc = null;
375+
if (t instanceof FunError.FnErrorXPathException) {
376+
errorDesc = ((FunError.FnErrorXPathException) t).getDetailMessage();
377+
}
378+
379+
if (errorDesc == null) {
380+
errorDesc = errorCode.getDescription();
381+
if (errorDesc != null) {
382+
@Nullable final String throwableDesc = t instanceof XPathException ? ((XPathException) t).getDetailMessage() : t.getMessage();
383+
if (!errorDesc.equals(throwableDesc)) {
384+
errorDesc = errorDesc + (errorDesc.endsWith(".") ? " " : ". ") + throwableDesc;
385+
}
386+
}
387+
}
381388

389+
final Sequence description = errorDesc != null ? new StringValue(this, errorDesc) : Sequence.EMPTY_SEQUENCE;
382390
final LocalVariable err_description = new LocalVariable(QN_DESCRIPTION);
383391
err_description.setSequenceType(new SequenceType(Type.QNAME, Cardinality.ZERO_OR_ONE));
384392
err_description.setValue(description);
@@ -411,7 +419,7 @@ public void dump(final ExpressionDumper dumper) {
411419
}
412420

413421
/**
414-
* Extract and construct errorcode from error text.
422+
* Extract and construct error code from error text.
415423
*/
416424
private ErrorCode extractErrorCode(final XPathException xpe) {
417425

@@ -422,10 +430,26 @@ private ErrorCode extractErrorCode(final XPathException xpe) {
422430
if (':' == message.charAt(8)) {
423431

424432
final String[] data = extractLocalName(xpe.getMessage());
425-
final ErrorCode errorCode = new ErrorCode(data[0], data[1]);
433+
434+
435+
QName errorQName = null;
436+
ErrorCode errorCode = null;
437+
try {
438+
errorQName = QName.parse(context, data[0]);
439+
} catch (final QName.IllegalQNameException e) {
440+
errorCode = ErrorCodes.EXistErrorCode.ERROR;
441+
}
442+
443+
if (errorCode == null) {
444+
try {
445+
errorCode = ErrorCodes.fromQName(errorQName);
446+
} catch (final IllegalArgumentException e) {
447+
errorCode = new ErrorCodes.DynamicErrorCode(errorQName, data[1]);
448+
}
449+
}
450+
426451
LOG.debug("Parsed string '{}' for Errorcode. Qname='{}' message='{}'", xpe.getMessage(), data[0], data[1]);
427452
return errorCode;
428-
429453
}
430454

431455
// Convert xpe to Throwable

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,6 @@ public class FnModule extends AbstractInternalModule {
306306
Arrays.sort(functions, new FunctionComparator());
307307
}
308308

309-
public final static ErrorCodes.ErrorCode SENR0001 = new ErrorCodes.ErrorCode("SENR0001", "serialization error in fn:serialize");
310-
public final static ErrorCodes.ErrorCode SEPM0019 = new ErrorCodes.ErrorCode("SEPM0019", "It is an error if an instance of the data model " +
311-
"used to specify the settings of serialization parameters specifies the value of the same parameter more than once.");
312-
313309
public FnModule(Map<String, List<?>> parameters) {
314310
super(functions, parameters, true);
315311
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import org.exist.xquery.Cardinality;
5555
import org.exist.xquery.ErrorCodes;
5656
import org.exist.xquery.ErrorCodes.ErrorCode;
57-
import org.exist.xquery.Function;
57+
import org.exist.xquery.Expression;
5858
import org.exist.xquery.FunctionSignature;
5959
import org.exist.xquery.XPathException;
6060
import org.exist.xquery.XQueryContext;
@@ -148,12 +148,17 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
148148
if (!args[0].isEmpty()) {
149149
QName errorQName = ((QNameValue) args[0].itemAt(0)).getQName();
150150
String prefix = errorQName.getPrefix();
151-
if (prefix==null){
151+
if (prefix == null){
152152
final String ns = errorQName.getNamespaceURI();
153153
prefix = getContext().getPrefixForURI(ns);
154154
errorQName = new QName(errorQName.getLocalPart(), errorQName.getNamespaceURI(), prefix);
155155
}
156-
errorCode = new ErrorCode(errorQName, errorDesc);
156+
157+
try {
158+
errorCode = ErrorCodes.fromQName(errorQName);
159+
} catch (final IllegalArgumentException e) {
160+
errorCode = new ErrorCodes.DynamicErrorCode(errorQName, errorDesc);
161+
}
157162
}
158163
// If there is a third argument, use it.
159164
if (args.length == 3) {
@@ -165,6 +170,16 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
165170
logger.trace("{}: {}", errorDesc, errorCode.toString());
166171
}
167172

168-
throw new XPathException(this, errorCode, errorDesc, errorVal);
173+
throw new FnErrorXPathException(this, errorCode, errorDesc, errorVal);
174+
}
175+
176+
/**
177+
* Indicates that the XPathException was raised
178+
* explicitly through fn:error.
179+
*/
180+
public static class FnErrorXPathException extends XPathException {
181+
FnErrorXPathException(final Expression expr, final ErrorCode errorCode, final String errorDesc, final Sequence errorVal) {
182+
super(expr, errorCode, errorDesc, errorVal);
183+
}
169184
}
170185
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
118118

119119
return new StringValue(this, writer.toString());
120120
} catch (final SAXException e) {
121-
throw new XPathException(this, FnModule.SENR0001, e.getMessage());
121+
throw new XPathException(this, ErrorCodes.W3CErrorCode.SENR0001, e.getMessage());
122122
}
123123
}
124124

@@ -192,7 +192,7 @@ public static Sequence normalize(final Expression callingExpr, final XQueryConte
192192
final int itemType = next.getType();
193193
if (Type.subTypeOf(itemType, Type.NODE)) {
194194
if (itemType == Type.ATTRIBUTE || itemType == Type.NAMESPACE || itemType == Type.FUNCTION) {
195-
throw new XPathException(callingExpr, FnModule.SENR0001,
195+
throw new XPathException(callingExpr, ErrorCodes.W3CErrorCode.SENR0001,
196196
"It is an error if an item in the sequence to serialize is an attribute node or a namespace node.");
197197
}
198198
step2.add(next);
@@ -225,7 +225,7 @@ public static Sequence normalize(final Expression callingExpr, final XQueryConte
225225
}
226226
return (DocumentImpl)receiver.getDocument();
227227
} catch (final SAXException e) {
228-
throw new XPathException(callingExpr, FnModule.SENR0001, e.getMessage());
228+
throw new XPathException(callingExpr, ErrorCodes.W3CErrorCode.SENR0001, e.getMessage());
229229
} finally {
230230
context.popDocumentContext();
231231
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,12 @@ private XPathException originalXPathException(final String prefix, @Nonnull fina
300300
final StructuredQName from = xPathException.getErrorCodeQName();
301301
if (from != null) {
302302
final QName errorCodeQName = new QName(from.getLocalPart(), from.getURI(), from.getPrefix());
303-
final ErrorCodes.ErrorCode errorCode = new ErrorCodes.ErrorCode(errorCodeQName, cause.getMessage());
303+
ErrorCodes.ErrorCode errorCode = null;
304+
try {
305+
errorCode = ErrorCodes.fromQName(errorCodeQName);
306+
} catch (final IllegalArgumentException ee) {
307+
errorCode = new ErrorCodes.DynamicErrorCode(errorCodeQName, cause.getMessage());
308+
}
304309
return new XPathException(fnTransform, errorCode, prefix + cause.getMessage());
305310
} else {
306311
return new XPathException(fnTransform, defaultErrorCode, prefix + cause.getMessage());

exist-core/src/main/java/org/exist/xquery/functions/map/MapErrorCode.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

exist-core/src/main/java/org/exist/xquery/functions/map/MapExpr.java

Lines changed: 25 additions & 1 deletion
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
*
@@ -76,7 +100,7 @@ public Sequence eval(Sequence contextSequence, final Item contextItem) throws XP
76100
for (final Mapping mapping : this.mappings) {
77101
final Sequence key = mapping.key.eval(contextSequence, null);
78102
if (key.getItemCount() != 1) {
79-
throw new XPathException(this, MapErrorCode.EXMPDY001, "Expected single value for key, got " + key.getItemCount());
103+
throw new XPathException(this, ErrorCodes.EXistErrorCode.EXMPDY001, "Expected single value for key, got " + key.getItemCount());
80104
}
81105
final AtomicValue atomic = key.itemAt(0).atomize();
82106
final Sequence value = mapping.value.eval(contextSequence, null);

0 commit comments

Comments
 (0)