Skip to content

Commit

Permalink
[#231] Logs now output incorrect line whet QueryParseException happens
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Grigorev committed Jul 10, 2024
1 parent d3b60a6 commit 90b3408
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions s-pipes-core/src/main/java/cz/cvut/spipes/util/QueryUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.cvut.spipes.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
Expand All @@ -12,6 +13,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class QueryUtils {
Expand Down Expand Up @@ -182,11 +184,19 @@ public static Query createQuery(org.topbraid.spin.model.Query spinQuery) {
try {
return ARQFactory.get().createQuery(spinQuery);
} catch (QueryParseException e) {
String query = ARQFactory.get().createCommandString(spinQuery);query = query.substring(query.lastIndexOf("PREFIX"));
query = query.substring(query.indexOf('\n'));
LOG.error("Parse error [1] occurred in query [2] in method [3] of class [4].\n[1] ERROR:\n{}\n[2] QUERY:\n{}\n[3] METHOD:\n{}\n[4] CLASS:\n{}",
e.getMessage(), query, Thread.currentThread().getStackTrace()[2].getMethodName(),
Thread.currentThread().getStackTrace()[2].getClassName());
String query = ARQFactory.get().createCommandString(spinQuery);
String msg = e.getMessage();
Pattern pattern = Pattern.compile("line\\s+(\\d+)");
Matcher matcher = pattern.matcher(msg);
assert(matcher.find());
String numberAfterLine = matcher.group(1);
int wrongLineNumber = Integer.valueOf(numberAfterLine); //e.g
// etLine() returns wrong number for some reason
int beginningOfTheWrongLine = StringUtils.ordinalIndexOf(query, "\n", wrongLineNumber - 1);
int endOfTheWrongLine = StringUtils.ordinalIndexOf(query, "\n", wrongLineNumber);
String wrongLine = query.substring(beginningOfTheWrongLine, endOfTheWrongLine);
LOG.error("Parse error [1] occurred in lines [2].\n[1] ERROR:\n{}\n[2] LINE:\n{}",
msg, wrongLine);
throw e;
}
}
Expand Down

0 comments on commit 90b3408

Please sign in to comment.