Skip to content

Commit

Permalink
Improving error handling and reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
pachanga committed Nov 22, 2024
1 parent b0165db commit 8d2ed9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/compile/antlr_proc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,13 @@ static void WrapError(ANTLR_Processor proc, Action action)
{
action();
}
catch(SymbolError err)
catch(Exception e)
{
proc.errors.Add(err);
if(e is ICompileError ce)
proc.errors.Add(ce);
else
//NOTE: let's turn other exceptions into BuildErrors
proc.errors.Add(new BuildError(proc.module.file_path, e));
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/compile/error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public SyntaxError(string file, SourceRange range, string msg)

public class BuildError : Exception, ICompileError
{
private const int MAX_EXCEPTION_STR_LEN = 1000;

public string text { get; }
public string stack_trace { get { return StackTrace; } }
public SourceRange range { get { return new SourceRange(); } }
Expand All @@ -87,15 +89,13 @@ public BuildError(string file, string msg)
this.file = file;
}

private const int MAX_EXCEPTION_STACK_LEN = 700;

public BuildError(string file, Exception inner)
: base(ErrorUtils.MakeMessage(file, new SourceRange(), inner.Message), inner)
{
var stack = inner.StackTrace;
if(stack.Length > MAX_EXCEPTION_STACK_LEN)
stack = stack.Substring(0, MAX_EXCEPTION_STACK_LEN) + "...";
this.text = inner.Message + stack;
var msg = inner.ToString(); //NOTE: this will include all inner exceptions stack traces as well
if(msg.Length > MAX_EXCEPTION_STR_LEN)
msg = msg.Substring(0, MAX_EXCEPTION_STR_LEN) + "...";
this.text = msg;
this.file = file;
}
}
Expand Down

0 comments on commit 8d2ed9b

Please sign in to comment.