5 ms·
Show HN: Decorate Java stack traces with source code snippets
- treebeard5440 3y agoGreat work - I like this a lot
- rickette 3y agoThis look pretty useful, thanks.
- chii 3y agoVery cool work! Some unsolicited opinion: One potential code pet peeve i always have is that the overloads being used in java serves no purpose, but makes the code super confusing to read without using an IDE. For example, https://github.com/laech/java-stacksrc/blob/main/core/src/main/java/nz/lae/stacksrc/StackTraceDecorator.java#L82 https://github.com/laech/java-stacksrc/blob/main/core/src/ma... , which is code to decorate the stacktrace element, calls the function `decorate`. However, there's several overloads, all named decorate! It is certainly a better naming convention to call it `decorateElement` imho.
- gunnarmorling 3y agoLoving it! Building a Maven Surefire plug-in to do this was on my list of potential side projects for quite a while, so cool that someone finally did this. Even better as a JUnit extension. Congrats on the release, can't wait to give it a try!.
- nunobrito 3y agoThanks for publishing. From the title was expecting that we'd get the code snippet related to the code itself, rather than the junit where the code failed. But I guess for such feature to be possible you'd have to pack the source code inside the jar itself.
- tomverelst 3y agoJust tested it, and it does print code snippets of the related code of the source code of my project, and not just of the test itself.
- CrimsonChapulin 3y agoFYI: for the gradle dependencies section on the read me, both junit4 and junit5 say junit5.
- laech 3y agoThanks for pointing that out, it's now fixed.
- zmmmmm 3y agoReminds me of power assertions in Groovy - one of my favorite features!
- PhilipRoman 3y agoYep they are amazing. Never been a fan of alternative JVM languages, but wrote most tests in Groovy just for this feature.
- mike_hearn 3y agoThere is a great library to add power assertions to Kotlin apps: https://github.com/bnorm/kotlin-power-assert/ https://github.com/bnorm/kotlin-power-assert/ You just add the Gradle plugin and you're done.
- sakopov 3y agoThis is pretty awesome! This got me curious if I can do this in C# so I took a quick stab at it. It's not actually updating the original stack trace but it's kind of cool. Might be worth exploring further :) static void Main() { try { throw new DivideByZeroException("Boom!"); } catch (Exception ex) { var trace = new StackTrace(ex, true); var frame = trace.GetFrame(0); var targetFilename = frame.GetFileName(); var targetLineNumber = frame.GetFileLineNumber(); Console.WriteLine($"Exception occurred at {targetFilename}, line {targetLineNumber}"); if (File.Exists(targetFilename)) { var source = File.ReadAllLines(targetFilename); var startIndex = Math.Max(0, targetLineNumber - 3); var endIndex = Math.Min(source.Length - 1, targetLineNumber + 1); for (var i = startIndex; i <= endIndex; i++) { var lineNumber = i + 1; Console.WriteLine($"{(lineNumber == targetLineNumber ? $">> {lineNumber}" : $" {lineNumber}")}{source[i]}"); } } } }
- theanonymousone 3y agoOne big "off-label" use of stack traces for me is to determine the caller method of the current method. I use it in a lightly customised log function to also log the name of the method that has emitted the log.
- javanonymous 3y agoIf I understand your use case, most loggers (like Logback) have the possiblity to automatically include the name of the method that called the log-statement. Logback even supports including the call chain in the output, so you could include the parent method, grand parent method etc. This probably uses reflection, so it could have a negative impact for applications that need a high performance. https://logback.qos.ch/manual/layouts.html https://logback.qos.ch/manual/layouts.html
- theanonymousone 3y agoThanks. Will take a look. I don't use logging frameworks in smaller projects.
- papercrane 3y agoIf you're using Java 9+ there is an official API to do that. It's the StackWaker class. https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/lang/StackWalker.html https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/l...
- theanonymousone 3y agoMany thanks. Will look at it.
- surajcm 3y agoAwesome !! Good to see this
- junon 3y agoI did this a while back for Python: https://GitHub.com/qix-/better-exceptions https://GitHub.com/qix-/better-exceptions