From 54469e5618f4dedb555663a617ab4cdf0f73597b Mon Sep 17 00:00:00 2001 From: qaate47 Date: Wed, 4 Dec 2024 15:01:56 +0100 Subject: [PATCH] GH-384 Send 400 error with bad parsing --- .../qendpoint/controller/EndpointController.java | 3 ++- .../compiler/CantParseQueryException.java | 16 ++++++++++++++++ .../qendpoint/compiler/SparqlRepository.java | 16 ++++++++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/CantParseQueryException.java diff --git a/qendpoint-backend/src/main/java/com/the_qa_company/qendpoint/controller/EndpointController.java b/qendpoint-backend/src/main/java/com/the_qa_company/qendpoint/controller/EndpointController.java index e36497c1..5cf86bca 100644 --- a/qendpoint-backend/src/main/java/com/the_qa_company/qendpoint/controller/EndpointController.java +++ b/qendpoint-backend/src/main/java/com/the_qa_company/qendpoint/controller/EndpointController.java @@ -1,5 +1,6 @@ package com.the_qa_company.qendpoint.controller; +import com.the_qa_company.qendpoint.compiler.CantParseQueryException; import com.the_qa_company.qendpoint.store.EndpointStoreUtils; import com.the_qa_company.qendpoint.store.exception.EndpointStoreInputException; import jakarta.servlet.http.HttpServletRequest; @@ -66,7 +67,7 @@ public void sparqlEndpoint(@RequestParam(value = "query", required = false) fina } else { throw new ServerWebInputException("Query not specified"); } - } catch (EndpointStoreInputException e) { + } catch (EndpointStoreInputException | CantParseQueryException e) { throw new ServerWebInputException(e.getMessage()); } } diff --git a/qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/CantParseQueryException.java b/qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/CantParseQueryException.java new file mode 100644 index 00000000..1f978c3e --- /dev/null +++ b/qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/CantParseQueryException.java @@ -0,0 +1,16 @@ +package com.the_qa_company.qendpoint.compiler; + +public class CantParseQueryException extends RuntimeException { + + public CantParseQueryException(String message) { + super(message); + } + + public CantParseQueryException(String message, Throwable cause) { + super(message, cause); + } + + public CantParseQueryException(Throwable cause) { + super(cause); + } +} diff --git a/qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/SparqlRepository.java b/qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/SparqlRepository.java index f173aa3f..22de7d6d 100644 --- a/qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/SparqlRepository.java +++ b/qendpoint-store/src/main/java/com/the_qa_company/qendpoint/compiler/SparqlRepository.java @@ -600,7 +600,13 @@ private ClosableResult execute0(RepositoryConnection customConnection, String logger.info("Running given sparql query: {}", sparqlQuery); - ParsedQuery parsedQuery = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, sparqlQuery, null); + ParsedQuery parsedQuery; + + try { + parsedQuery = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, sparqlQuery, null); + } catch (RuntimeException re) { + throw new CantParseQueryException(re); + } if (compiledSail.getOptions().isDebugShowPlans()) { System.out.println(parsedQuery); @@ -797,7 +803,13 @@ public void executeUpdate(String sparqlQuery, int timeout, OutputStream out, try (connectionCloseable) { connection.setParserConfig(new ParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false)); - Update preparedUpdate = connection.prepareUpdate(QueryLanguage.SPARQL, sparqlQuery); + Update preparedUpdate; + + try { + preparedUpdate = connection.prepareUpdate(QueryLanguage.SPARQL, sparqlQuery); + } catch (RuntimeException e) { + throw new CantParseQueryException(e); + } if (timeout < 0) { preparedUpdate.setMaxExecutionTime(getOptions().getTimeoutQuery());