From 64c61f0f8cdfc30b5be20a3595702486834096ca Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 4 Mar 2026 23:38:01 +0100 Subject: [PATCH] Improve stack trace error handling. --- .../Runtime/RuntimeInformation.cs | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/YesNt.Interpreter/Runtime/RuntimeInformation.cs b/YesNt.Interpreter/Runtime/RuntimeInformation.cs index 7ad1ca1..3216be3 100644 --- a/YesNt.Interpreter/Runtime/RuntimeInformation.cs +++ b/YesNt.Interpreter/Runtime/RuntimeInformation.cs @@ -123,13 +123,29 @@ internal sealed class RuntimeInformation { if (!Stop) { - Line line = Lines[Math.Min(LineNumber, Lines.Count - 1)]; - WriteLine($"{Environment.NewLine}[{(IsTask ? $"Task {TaskId}" : "The process")} was terminated at line {line.LineNumber + 1} in the file \"{line.FileName}\" with the message: {message}]", true); + if (Lines.Count == 0) + { + WriteLine($"{Environment.NewLine}[{(IsTask ? $"Task {TaskId}" : "The process")} was terminated with the message: {message}]", true); + } + else + { + Line line = Lines[Math.Min(LineNumber, Lines.Count - 1)]; + WriteLine($"{Environment.NewLine}[{(IsTask ? $"Task {TaskId}" : "The process")} was terminated at line {line.LineNumber + 1} in the file \"{line.FileName}\" with the message: {message}]", true); + } + while (FunctionCallStack.Count > 0) { int stackLineNumber = FunctionCallStack.Pop().CallerLine; - Line stackLine = (ParentRuntimeInformation?.Lines ?? Lines)[stackLineNumber]; - WriteLine($" at line {stackLine.LineNumber + 1} in the file \"{stackLine.FileName}\"", true); + List targetLines = ParentRuntimeInformation?.Lines ?? Lines; + if (stackLineNumber >= 0 && stackLineNumber < targetLines.Count) + { + Line stackLine = targetLines[stackLineNumber]; + WriteLine($" at line {stackLine.LineNumber + 1} in the file \"{stackLine.FileName}\"", true); + } + else + { + WriteLine(" at unknown location (source not available)", true); + } } Stop = true;