Skip to content

Commit 7926887

Browse files
committed
[fix] handle IllegalStateException in file:directory-list#2
fixes #5028 The Java code of DirectoryList can throw an IllegalStateException when a non-existent directory is supplied as its first argument. This exception class is now handled in file:list#2. Additionally, the raised error now has a proper code `file:DIRECTORY_NOT_FOUND`. This will allow us in the future to add proper error codes to more errors raised by this module.
1 parent 4a94b68 commit 7926887

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

extensions/modules/file/src/main/java/org/exist/xquery/modules/file/DirectoryList.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.IOException;
2525
import java.nio.file.Files;
2626
import java.nio.file.Path;
27-
import java.nio.file.Paths;
2827
import java.util.Date;
2928

3029
import org.apache.logging.log4j.LogManager;
@@ -34,11 +33,7 @@
3433
import org.exist.dom.QName;
3534
import org.exist.dom.memtree.MemTreeBuilder;
3635
import org.exist.util.FileUtils;
37-
import org.exist.xquery.BasicFunction;
38-
import org.exist.xquery.Cardinality;
39-
import org.exist.xquery.FunctionSignature;
40-
import org.exist.xquery.XPathException;
41-
import org.exist.xquery.XQueryContext;
36+
import org.exist.xquery.*;
4237
import org.exist.xquery.value.DateTimeValue;
4338
import org.exist.xquery.value.FunctionParameterSequenceType;
4439
import org.exist.xquery.value.FunctionReturnSequenceType;
@@ -47,9 +42,11 @@
4742
import org.exist.xquery.value.SequenceType;
4843
import org.exist.xquery.value.Type;
4944

45+
import static org.exist.xquery.modules.file.FileErrorCode.DIRECTORY_NOT_FOUND;
46+
5047
/**
5148
* eXist File Module Extension DirectoryList
52-
*
49+
* <p>
5350
* Enumerate a list of files, including their size and modification time, found
5451
* in a specified directory, using a pattern
5552
*
@@ -121,7 +118,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
121118
builder.addAttribute(new QName("directory", null, null), baseDir.toString());
122119
try {
123120
final int patternsLen = patterns.getItemCount();
124-
final String includes[] = new String[patternsLen];
121+
final String[] includes = new String[patternsLen];
125122
for (int i = 0; i < patternsLen; i++) {
126123
includes[i] = patterns.itemAt(0).getStringValue();
127124
}
@@ -172,8 +169,8 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
172169
builder.endElement();
173170

174171
return (NodeValue) builder.getDocument().getDocumentElement();
175-
} catch (final IOException e) {
176-
throw new XPathException(this, e.getMessage());
172+
} catch (final IOException | IllegalStateException e) {
173+
throw new XPathException(this, DIRECTORY_NOT_FOUND, e.getMessage());
177174
} finally {
178175
context.popDocumentContext();
179176
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
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; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
package org.exist.xquery.modules.file;
23+
24+
import org.exist.dom.QName;
25+
import org.exist.xquery.ErrorCodes;
26+
27+
class FileErrorCode extends ErrorCodes.ErrorCode {
28+
public static final ErrorCodes.ErrorCode DIRECTORY_NOT_FOUND = new FileErrorCode("DIRECTORY_NOT_FOUND", "The directory could not be found.");
29+
30+
FileErrorCode(String code, String description) {
31+
super(new QName(code, FileModule.NAMESPACE_URI, FileModule.PREFIX), description);
32+
}
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(:
2+
: eXist-db Open Source Native XML Database
3+
: Copyright (C) 2001 The eXist-db Authors
4+
:
5+
6+
: http://www.exist-db.org
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; either
11+
: version 2.1 of the License, or (at your option) any later version.
12+
:
13+
: This library is distributed in the hope that it will be useful,
14+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
: Lesser General Public License for more details.
17+
:
18+
: You should have received a copy of the GNU Lesser General Public
19+
: License along with this library; if not, write to the Free Software
20+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
:)
22+
xquery version "3.1";
23+
24+
module namespace dirlist="http://exist-db.org/testsuite/modules/file/dirlist";
25+
26+
27+
import module namespace file="http://exist-db.org/xquery/file";
28+
29+
30+
declare namespace test="http://exist-db.org/xquery/xqsuite";
31+
32+
33+
declare
34+
%test:assertError("file:DIRECTORY_NOT_FOUND")
35+
function dirlist:non-existent-error-code() {
36+
file:directory-list("/non/existent", ())
37+
};
38+
39+
declare
40+
%test:assertXPath("contains($result,'basedir /non/existent does not exist.') or contains($result,'basedir \non\existent does not exist.')")
41+
function dirlist:non-existent-error-description() {
42+
try {
43+
file:directory-list("/non/existent", ())
44+
} catch file:DIRECTORY_NOT_FOUND {
45+
$err:description
46+
}
47+
};

0 commit comments

Comments
 (0)