SPARQL-Anything / PySPARQL-Anything

The SPARQL Anything Python library
Apache License 2.0
10 stars 0 forks source link

Uninformative error message when query not found #9

Open jmmcd opened 9 months ago

jmmcd commented 9 months ago

Version: sparql-anything-v0.8.2.jar

This issue is not an important one. It could be a small cosmetic improvement.

When I tried to sa.run() a query as below, it failed because I mistyped the filename, with the error as below. I think the error message could be more informative, eg FileNotFoundError.

In [4]: sa.run(q='query1.sparql')
[main] INFO com.github.sparqlanything.cli.SPARQLAnything - SPARQL anything
---------------------------------------------------------------------------
JavaException                             Traceback (most recent call last)
Cell In[4], line 1
----> 1 sa.run(q='query1.sparql')

File ~\miniconda3\Lib\site-packages\pysparql_anything\sparql_anything.py:30, in SparqlAnything.run(self, **kwargs)
     23 """
     24 The run method replaces the regular command line execution.
     25 @param **kwargs The keyword arguments are the same as the regular
     26     flags for the Sparql Anything CLI, minus the hyphen.
     27 See the User Guide for an example.
     28 """
     29 command = cmd.RunCommand(kwargs, self.receiver)
---> 30 command.execute()

File ~\miniconda3\Lib\site-packages\pysparql_anything\command.py:159, in RunCommand.execute(self)
    157 """ instructions for a run request """
    158 args = super()._combine()
--> 159 self.receiver.main(args)

File ~\miniconda3\Lib\site-packages\pysparql_anything\engine.py:39, in Engine.main(self, args)
     37 def main(self, args):
     38     """ Wrapper for the SPARQL Anything main method. """
---> 39     self.reflection.main(args)

File jnius\\jnius_export_class.pxi:876, in jnius.JavaMethod.__call__()

File jnius\\jnius_export_class.pxi:1059, in jnius.JavaMethod.call_staticmethod()

File jnius\\jnius_utils.pxi:79, in jnius.check_exception()

JavaException: JVM exception occurred: Lexical error at line 1, column 14.  Encountered: <EOF> after prefix "query1.sparql" org.apache.jena.query.QueryParseException
MarcoR1791 commented 8 months ago

Hi James,

I could implement a preliminary check on whether the file exists that returns a FileNotFoundError.

The JavaException however comes from SPARQL Anything and is simply thrown onto the terminal by PySPARQL.

@enridaga Thoughts? Should I go ahead and implement what I suggested above?

enridaga commented 8 months ago

Actually, the error is not a file not found but a syntax error on the string interpreted as a query. The value for -q is either a file or, if a file does not exist, a valid SPARQL query. In practice, what happens is that the file is not found, therefore the system attempts to execute the string as a query itself, and fails (see the last line Lexical error at line 1, column 14. Encountered: <EOF>). Maybe, a better way would be to catch the error on the SA code and throw a better message (nor file or valid query string provided...).

jmmcd commented 8 months ago

Thank you, it makes sense.

Off-topic - I have PySA running in my code base now instead of running SA.jar from the command-line. It looks like it is going to cut my runtime from about 5 hours to 3 minutes. So I think you have saved a lot of CO2 emissions with this package!