From ed5a0455b63d084371d8460ee3c87a870c934071 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 9 Apr 2013 16:05:04 +0200 Subject: [PATCH] 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 --- hy/compiler.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 76b266a..e109ca0 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -38,7 +38,21 @@ import sys 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 = {} @@ -111,12 +125,15 @@ class HyASTCompiler(object): for _type in _compile_table: if type(tree) == _type: 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: - err = HyCompileError(str(e)) - err.exception = e - err.start_line = getattr(e, "start_line", None) - err.start_column = getattr(e, "start_column", None) - raise err + raise HyCompileError(exception=e, + start_line=getattr(e, "start_line", 0), + start_column=getattr(e, "start_column", 0)) raise HyCompileError("Unknown type - `%s'" % (str(type(tree))))