@Override public Expr inline(final QueryContext qc, final VarScope scp, final Var var, final Expr ex) throws QueryException { boolean change = false; try { final Expr sub = expr.inline(qc, scp, var, ex); if (sub != null) { if (sub.isValue()) return optPre(sub, qc); expr = sub; change = true; } } catch (final QueryException qe) { if (!qe.isCatchable()) throw qe; for (final Catch c : catches) { if (c.matches(qe)) { // found a matching clause, inline variable and error message final Catch nw = c.inline(qc, scp, var, ex); return optPre((nw == null ? c : nw).asExpr(qe, qc, scp), qc); } } throw qe; } for (final Catch c : catches) change |= c.inline(qc, scp, var, ex) != null; return change ? this : null; }
/** * Processes the HTTP request. Parses new modules and discards obsolete ones. * * @param http HTTP context * @throws Exception exception */ void process(final HTTPContext http) throws Exception { try { module.process(http, this); } catch (final QueryException ex) { if (ex.file() == null) ex.info(function.info); throw ex; } }
@Override public Value value(final QueryContext qc) throws QueryException { // don't catch errors from error handlers try { return qc.value(expr); } catch (final QueryException ex) { if (!ex.isCatchable()) throw ex; for (final Catch c : catches) if (c.matches(ex)) return c.value(qc, ex); throw ex; } }
@Override public Expr compile(final QueryContext qc, final VarScope scp) throws QueryException { try { super.compile(qc, scp); if (expr.isValue()) return optPre(expr, qc); } catch (final QueryException ex) { if (!ex.isCatchable()) throw ex; for (final Catch c : catches) { if (c.matches(ex)) { // found a matching clause, compile and inline error message return optPre(c.compile(qc, scp).asExpr(ex, qc, scp), qc); } } throw ex; } for (final Catch c : catches) c.compile(qc, scp); return optimize(qc, scp); }
/** * Refreshes the view after a file has been saved. * * @param root root directory * @param ctx database context * @throws InterruptedException interruption */ void parse(final IOFile root, final Context ctx) throws InterruptedException { final long id = ++parseId; final HashSet<String> parsed = new HashSet<>(); final TreeMap<String, InputInfo> errs = new TreeMap<>(); // collect files to be parsed final ProjectCache pc = cache(root); final StringList mods = new StringList(), lmods = new StringList(); for (final String path : pc) { final IOFile file = new IOFile(path); if (file.hasSuffix(IO.XQSUFFIXES)) (file.hasSuffix(IO.XQMSUFFIX) ? lmods : mods).add(path); } mods.add(lmods); // parse modules for (final String path : mods) { if (id != parseId) throw new InterruptedException(); if (parsed.contains(path)) continue; final IOFile file = new IOFile(path); try (final TextInput ti = new TextInput(file)) { // parse query try (final QueryContext qc = new QueryContext(ctx)) { final String input = ti.cache().toString(); final boolean lib = QueryProcessor.isLibrary(input); qc.parse(input, lib, path, null); // parsing was successful: remember path parsed.add(path); for (final byte[] mod : qc.modParsed) parsed.add(Token.string(mod)); } catch (final QueryException ex) { // parsing failed: remember path final InputInfo ii = ex.info(); errs.put(path, ii); parsed.add(ii.path()); } } catch (final IOException ex) { // file may not be accessible Util.debug(ex); } } errors = errs; }
/** * Tests if errors are thrown when some mandatory attributes are missing in a <http:request/>, * <http:body/> or <http:multipart/>. * * @throws IOException I/O Exception */ @Test public void errors() throws IOException { // Incorrect requests final List<byte[]> falseReqs = new ArrayList<>(); // Request without method final byte[] falseReq1 = token( "<http:request " + "xmlns:http='http://expath.org/ns/http-client' " + "href='" + REST_ROOT + "'/>"); falseReqs.add(falseReq1); // Request with send-authorization and no credentials final byte[] falseReq2 = token( "<http:request " + "xmlns:http='http://expath.org/ns/http-client' " + "method='GET' href='" + REST_ROOT + "' " + "send-authorization='true'/>"); falseReqs.add(falseReq2); // Request with send-authorization and only username final byte[] falseReq3 = token( "<http:request " + "xmlns:http='http://expath.org/ns/http-client' " + "method='GET' href='" + REST_ROOT + "' " + "send-authorization='true' username='******'/>"); falseReqs.add(falseReq3); // Request with body that has no media-type final byte[] falseReq4 = token( "<http:request " + "xmlns:http='http://expath.org/ns/http-client' " + "method='POST' href='" + REST_ROOT + "'>" + "<http:body>" + "</http:body>" + "</http:request>"); falseReqs.add(falseReq4); // Request with multipart that has no media-type final byte[] falseReq5 = token( "<http:request method='POST' " + "xmlns:http='http://expath.org/ns/http-client' " + "href='" + REST_ROOT + "'>" + "<http:multipart boundary='xxx'>" + "</http:multipart>" + "</http:request>"); falseReqs.add(falseReq5); // Request with multipart with part that has a body without media-type final byte[] falseReq6 = token( "<http:request method='POST' " + "xmlns:http='http://expath.org/ns/http-client' " + "href='" + REST_ROOT + "'>" + "<http:multipart boundary='xxx'>" + "<http:header name='hdr1' value-='val1'/>" + "<http:body media-type='text/plain'>" + "Part1" + "</http:body>" + "<http:header name='hdr1' value-='val1'/>" + "<http:body>" + "Part1" + "</http:body>" + "</http:multipart>" + "</http:request>"); falseReqs.add(falseReq6); // Request with schema different from http final byte[] falseReq7 = token( "<http:request " + "xmlns:http='http://expath.org/ns/http-client' " + "href='ftp://basex.org'/>"); falseReqs.add(falseReq7); // Request with content and method which must be empty final byte[] falseReq8 = token( "<http:request " + "xmlns:http='http://expath.org/ns/http-client' " + "method='DELETE' href='" + REST_ROOT + "'>" + "<http:body media-type='text/plain'>" + "</http:body>" + "</http:request>"); falseReqs.add(falseReq8); for (final byte[] falseReq : falseReqs) { final DBNode dbNode = new DBNode(new IOContent(falseReq)); try { final HttpRequestParser rp = new HttpRequestParser(null); rp.parse(dbNode.children().next(), null); fail("Exception not thrown"); } catch (final QueryException ex) { assertTrue(ex.getMessage().contains(ErrType.HC.toString())); } } }