Skip to content

Commit

Permalink
Merge pull request #540 from the-qa-company/GH-384-send-400-bad-queries
Browse files Browse the repository at this point in the history
GH-384 send 400 bad queries
  • Loading branch information
ate47 authored Dec 4, 2024
2 parents 881bed7 + 54469e5 commit edd147d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit edd147d

Please sign in to comment.