Enhance error reporting

We're now able to make the difference between a compiler bug and a user
trying to compile wrong thing, and report this correctly on the console.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-09 16:05:04 +02:00
parent 4e61ae59fd
commit ed5a0455b6

View File

@ -38,7 +38,21 @@ import sys
class HyCompileError(HyError): class HyCompileError(HyError):
pass def __init__(self, exception,
start_line=0, start_column=0):
self.exception = exception
self.start_line = start_line
self.start_column = start_column
def __str__(self):
if self.start_line == 0:
return("Internal Compiler Bug\n%s: %s"
% (self.exception.__class__.__name__,
self.exception))
return ("Compilation error at line %d, column %d\n%s: %s"
% (self.start_line, self.start_column,
self.exception.__class__.__name__,
self.exception))
_compile_table = {} _compile_table = {}
@ -111,12 +125,15 @@ class HyASTCompiler(object):
for _type in _compile_table: for _type in _compile_table:
if type(tree) == _type: if type(tree) == _type:
return _compile_table[_type](self, tree) return _compile_table[_type](self, tree)
except HyCompileError:
# compile calls compile, so we're going to have multiple raise
# nested; so let's re-raise this exception, let's not wrap it in
# another HyCompileError!
raise
except Exception as e: except Exception as e:
err = HyCompileError(str(e)) raise HyCompileError(exception=e,
err.exception = e start_line=getattr(e, "start_line", 0),
err.start_line = getattr(e, "start_line", None) start_column=getattr(e, "start_column", 0))
err.start_column = getattr(e, "start_column", None)
raise err
raise HyCompileError("Unknown type - `%s'" % (str(type(tree)))) raise HyCompileError("Unknown type - `%s'" % (str(type(tree))))