ballerina-spec icon indicating copy to clipboard operation
ballerina-spec copied to clipboard

Readability of the Stack Trace

Open nirmal070125 opened this issue 9 months ago • 4 comments

Currently, on an error Ballerina stack trace gets printed like following;

Stack trace: [callableName: validatePerson fileName: error_reporting.bal lineNumber: 30,callableName: validatePeople fileName: error_reporting.bal lineNumber: 16,callableName: main fileName: error_reporting.bal lineNumber: 42]

Code - https://ballerina.io/learn/by-example/error-reporting/

IMO, it is very hard to read this and understand. Should we use multi-line approach similar to Java?

nirmal070125 avatar Feb 26 '25 03:02 nirmal070125

I agree. The current approach,io:println(err.stackTrace());, just prints the internal StrackFrame fields.

What you are expecting is something like the following. As Waruna mentioned, we use this format when a panic happens.

error: Validation failed for a person {"person":{"name":"Bob","age":-1}}
        at wso2.helloworld.0:validatePeople(main.bal:20)
           wso2.helloworld.0:main(main.bal:42)
cause: Age cannot be negative
        at wso2.helloworld.0:validatePerson(main.bal:30)
           wso2.helloworld.0:validatePeople(main.bal:16)
           ... 1 more

We should introduce a way to print stack traces in this format.

sameerajayasoma avatar Feb 26 '25 21:02 sameerajayasoma

@warunalakshitha, Where do we print the above panic output? Is that part of the runtime code?

sameerajayasoma avatar Feb 26 '25 21:02 sameerajayasoma

Yes. This is part of the runtime code. Current Jballerina runtime represents error (ErrorValue) as a combination of Ballerina value and Runtime exception. Above is the overridden Java method implementation of Ballerina ErrorValue (RuntimeException) printstacktrace . But this is only part of JBallerina.

Ballerina error lang library does not have printStackTrace function.

io:println(err.stackTrace()); Note: This line prints array.toString() for StackFrame[] array. IMO we cannot change this behavior since it is a generic array toString representation [1]. array:toString() prints the toString representation of StackFrame which cannot contain any newline characters as per the spec [2]

One suggestion would be to use error:printStackTrace library function for this requirement.

Initial discussions for the implementation of stackTrace can be found in [3]

[1] https://ballerina.io/spec/lang/master/#ToString [2] https://github.com/ballerina-platform/ballerina-spec/blob/master/lang/lib/error.bal#L57 [3] https://github.com/ballerina-platform/ballerina-spec/issues/675#issuecomment-743691193

warunalakshitha avatar Mar 04 '25 05:03 warunalakshitha

@warunalakshitha, could you come up with a spec proposal for this?

sameerajayasoma avatar Apr 22 '25 20:04 sameerajayasoma