private string ExtractLuaSource(int linenumber) { var lines = RawSource.Split('\n').ToList(); var lua = $"## {lines[linenumber - 2]}\n## --->{lines[linenumber - 1]}\n"; if (linenumber < lines.Count) { lua = $"{lua}## {lines[linenumber]}"; } return(lua); }
private string HumanizeException(Exception ex) { var lines = RawSource.Split('\n').ToList(); var summary = string.Empty; if (ex is InterpreterException ie) { // Get information from decorated message. CallStack is only available for // certain lua exceptions; decorated message / innerexception always has what // is needed. var e = ParseException(ie.DecoratedMessage); if (!string.IsNullOrEmpty(e.Filename)) { summary = $"Line {e.LineNumber}: {e.Error}\n{ExtractLuaSource(e.LineNumber)}"; } else { summary = $"Could not be parsed, raw message follows {ie.DecoratedMessage}"; } if (ie is SyntaxErrorException) { return($"\nSyntax error: {summary}"); } if (ie is ScriptRuntimeException) { return($"\nScripting runtime error: {summary}"); } if (ie is DynamicExpressionException) { return($"\nLua type error: {summary}"); } else { return($"\nInternal error from Lua code: STACK: {summary}\nERR: {ie.DecoratedMessage}"); } } else { var retstr = string.Empty; retstr = $"\nC# exception, perhaps caused by Lua script code: STACK: {ex.StackTrace}\n ERR:{ex.Message}"; if (ex.InnerException != null) { retstr = $"{retstr}\nINNER STACK TRACE: {ex.InnerException.StackTrace}\n INNER ERR: {ex.InnerException.Message}"; } return(retstr); } }