Skip to content

Commit

Permalink
Better exception messaging when message is "None"
Browse files Browse the repository at this point in the history
e.g. SystemExit exception
  • Loading branch information
eirannejad committed Oct 13, 2023
1 parent 341f300 commit 7599ba7
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/runtime/McNeel.PythonEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ public class PyException : Exception
public int LineNumber { get; } = -1;

public PyException(PythonException pyEx)
: base(pyEx.Message)
: base(ParseMessage(pyEx))
{
string traceback = pyEx.Traceback is null ? string.Empty : PythonException.TracebackToString(pyEx.Traceback);

_pyStackTrace = pyEx.Traceback is null ? string.Empty : string.Join(
Environment.NewLine,
"Traceback (most recent call last):",
traceback,
$"{pyEx.Type.Name}: {pyEx.Message}"
$"{pyEx.Type.Name}: {Message}"
);

Match m = s_msgParser.Matches(traceback)
Expand All @@ -314,6 +314,14 @@ public PyException(PythonException pyEx)
}

public override string ToString() => _pyStackTrace;

static string ParseMessage(PythonException pyEx)
{
if (pyEx.Message.Equals("None", StringComparison.InvariantCultureIgnoreCase))
return $"{pyEx.Type.Name} exception";

return pyEx.Message;
}
}

public int GetLineNumber(object frame) => Runtime.PyFrame_GetLineNumber(((PyObject)frame).Handle);
Expand Down

0 comments on commit 7599ba7

Please sign in to comment.