Skip to content

Fix calls to Exception.StackTrace #932

Closed
@paulirwin

Description

@paulirwin

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 the Exception class, and then calls the getStackTrace() method on that instance to obtain an array of StackTraceElement objects. 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 StackTrace class from the System.Diagnostics namespace. 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 StackTrace instance and then uses the GetFrames() method to obtain an array of StackFrame objects. 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)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions