Closed johnjohndoe closed 11 years ago
Hi @johnjohndoe thanks for your bug submission. I changed and commited a new version. The parse() method will now fail on invalid XML file and the code where your problem was manifesting is deleted. Also, I deleted the is_valid() method (it there is a exception on parse() -- no need to check is_valid() later).
Hope this is OK for you?
@tkrajina Looks good so far.
One thing I would rewrite is how you handle exceptions. I would not handle all errors as generic exceptions but add a specific XMLSyntaxError except
block before. This is the error you know of at the point in time.
except XMLSyntaxError as e:
mod_logging.debug('Error in:\n%s\n-----------\n' % self.xml)
mod_logging.exception(e)
raise mod_gpx.GPXException('Error parsing XML: %s' % str(e))
except Exception as e:
mod_logging.debug('Error in:\n%s\n-----------\n' % self.xml)
mod_logging.exception(e)
raise mod_gpx.GPXException('Unknown error: %s' % str(e))
If you can accept a crash on an unknown error you could even omit the generic except
block.
What about this: https://github.com/tkrajina/gpxpy/commit/8accbce26c06cd9df0197902bb6aa302830c63b7 ?
The idea is to have two kinds of gpxpy exceptions, one for invalid XML files and the other for invalid valid-XML-invalid-GPX files. You can choose if to detect all exceptions with GPXException or XML-specific with GPXXMLSyntaxException. If GPXXMLSyntaxException -- you can retrieve the original (which can be lxml or minidom) exception with e.original_exception.
@tkrajina I disagree. I find it more complicated to handle multiple GPX*Exception
s as a user of the library. What would be the benefit of yet another wrapper for an exception caused by an internal module not part of the library? All the user needs is the original error message which you wrap into a GPXException
. This is good enough imho.
@tkrajina Please consider this comment by abarnert with regards to exception handling.
When I use the parser with a broken GPX file an exception is raised. Here is the code I use. My first assumption was to catch a
GPXException
.However the main reason is somewhere else.
Please refer to this Stackoverflow post which already suggests a solution to the problem.
Here is a sample file that produces the syntax error.