Skip to content

Commit 422e0d5

Browse files
[MOD] Parsers: Handling of embedded query exceptions
1 parent 0793ee1 commit 422e0d5

5 files changed

Lines changed: 32 additions & 15 deletions

File tree

basex-core/src/main/java/org/basex/build/html/HtmlParser.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@ private static IO toXml(final IO io, final Parser parser, final HtmlOptions hopt
7777
final StringWriter sw = new StringWriter();
7878
final XMLReader reader = parser.reader(hopts, sw);
7979

80-
// define input
80+
// check encoding, define input
81+
final String encoding = Strings.normEncoding(hopts.get(ENCODING), false);
82+
if(encoding != null) {
83+
final String error = Strings.checkEncoding(encoding);
84+
if(error != null) throw INVALIDOPTION_X.getIO(error);
85+
}
8186
try(InputStream in = io.inputStream()) {
8287
final InputSource is = new InputSource(in);
83-
final String enc = io.encoding() != null ? io.encoding() : hopts.get(ENCODING);
84-
if(enc != null) {
85-
final String error = Strings.checkEncoding(enc);
86-
if(error != null) throw INVALIDOPTION_X.getIO("Unknown encoding: " + error + '.');
87-
is.setEncoding(Strings.normEncoding(enc, false));
88-
}
88+
final String enc = io.encoding() != null ? io.encoding() : encoding;
89+
if(enc != null) is.setEncoding(enc);
8990
reader.parse(is);
9091
}
9192
return new IOContent(token(sw.toString()), io.name());

basex-core/src/main/java/org/basex/query/func/fn/FnUnparsedBinary.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ Str parse(final TextInput ti, final Options options, final QueryContext qc) {
2727
throw Util.notExpected();
2828
}
2929

30+
@Override
31+
public QueryError error() {
32+
return QueryError.RESINPUT_X;
33+
}
34+
3035
@Override
3136
protected Options options(final QueryContext qc) {
3237
return new Options();

basex-core/src/main/java/org/basex/query/func/fn/ParseFn.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,24 @@ boolean nl() {
3737
}
3838

3939
/**
40-
* Returns a format-specific error code for invalid input.
41-
* @return error code
40+
* Returns a parser-specific exception.
41+
* @param ex original exception
42+
* @return embedded or new exception
4243
*/
43-
QueryError error() {
44-
return null;
44+
final QueryException exception(final IOException ex) {
45+
if(ex instanceof final QueryIOException qio) {
46+
final QueryException qe = qio.getCause();
47+
if(qe.error() == QueryError.INVALIDOPTION_X) return qe;
48+
}
49+
return error().get(info, ex);
4550
}
4651

52+
/**
53+
* Returns a parser-specific error.
54+
* @return error code
55+
*/
56+
abstract QueryError error();
57+
4758
/**
4859
* Returns parse options.
4960
* @param qc query context
@@ -72,7 +83,7 @@ protected Value parse(final QueryContext qc) throws QueryException {
7283
} catch(final DecodingException ex) {
7384
throw RECDECODING_X.get(info, ex);
7485
} catch(final IOException ex) {
75-
throw error().get(info, ex);
86+
throw exception(ex);
7687
}
7788
}
7889

@@ -111,7 +122,7 @@ protected Value doc(final QueryContext qc) throws QueryException {
111122
} catch(final DecodingException ex) {
112123
throw RECDECODING_X.get(info, ex);
113124
} catch(final InputException ex) {
114-
throw error().get(info, ex);
125+
throw exception(ex);
115126
} catch(final IOException ex) {
116127
Util.debug(ex);
117128
throw RESWHICH_X.get(info, io);

basex-core/src/main/java/org/basex/query/func/fn/ParseHtml.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private DBNode parse(final IO io, final QueryContext qc) throws QueryException {
5757
try {
5858
return new DBNode(new HtmlParser(io, prsr, new MainOptions(), options));
5959
} catch(final IOException ex) {
60-
throw error().get(info, ex);
60+
throw exception(ex);
6161
}
6262
}
6363

basex-core/src/main/java/org/basex/util/Strings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private static String hash(final String string, final String algo) {
217217

218218
/**
219219
* Returns a unified representation of the specified encoding.
220-
* @param encoding input encoding
220+
* @param encoding input encoding (can be {@code null})
221221
* @param dflt return default values if supplied encoding is {@code null} or ambiguous
222222
* @return encoding or {@code null}
223223
*/

0 commit comments

Comments
 (0)