Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calls to Exception.StackTrace #932

Open
paulirwin opened this issue Mar 13, 2024 · 0 comments
Open

Fix calls to Exception.StackTrace #932

paulirwin opened this issue Mar 13, 2024 · 0 comments

Comments

@paulirwin
Copy link
Contributor

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)

@paulirwin paulirwin mentioned this issue Mar 13, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant