ballerina-spec
ballerina-spec copied to clipboard
Readability of the Stack Trace
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?
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.
@warunalakshitha, Where do we print the above panic output? Is that part of the runtime code?
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, could you come up with a spec proposal for this?