-
Notifications
You must be signed in to change notification settings - Fork 653
Description
new Exception().StackTrace is an incorrect translation from Java. We should find all calls to Exception.StackTrace as well as the StackTrace class and convert them correctly. From ChatGPT:
In Java, the code
new Exception().getStackTrace()creates a new instance of theExceptionclass, and then calls thegetStackTrace()method on that instance to obtain an array ofStackTraceElementobjects. Each element in the array represents a stack trace element, providing information about the call stack at the point where the exception was created.Here's an example of how it might be used:
StackTraceElement[] trace = new Exception().getStackTrace(); for (StackTraceElement element : trace) { System.out.println(element.getClassName() + " - " + element.getMethodName() + " - " + element.getLineNumber()); }This code prints out the class name, method name, and line number for each element in the stack trace.
In C#, the equivalent code would use the
StackTraceclass from theSystem.Diagnosticsnamespace. Here's an example:using System; using System.Diagnostics; class Program { static void Main() { StackTrace trace = new StackTrace(); StackFrame[] frames = trace.GetFrames(); foreach (StackFrame frame in frames) { Console.WriteLine($"{frame.GetMethod().DeclaringType} - {frame.GetMethod().Name} - {frame.GetFileLineNumber()}"); } } }This C# code creates a new
StackTraceinstance and then uses theGetFrames()method to obtain an array ofStackFrameobjects. Similar to the Java example, it prints out the declaring type, method name, and line number for each frame in the stack trace.
We have a class called StackTraceHelper that we could add the support to convert it to a string. There are also several calls to .printStackTace() that should be reviewed. In .NET, Exception.StackTrace doesn't contain the exception type, so a lot of the tests use Exception.ToString() instead. But we would be better off with a centralized way of dealing with stack traces (in Support) - StackTraceHelper only applies to the test code, but there is code in production that is also not correctly translated. Of course, since it is something we own, moving it to Support is an option.
In short, we want to review all of the code that was using .getStackTrace() or .printStackTrace() in Lucene.
Originally posted by @NightOwl888 in #926 (comment)