# Better runtime backtraces

**URL:** <https://forum.crystal-lang.org/t/better-runtime-backtraces/1394>\
**Category:** Community\
**Created:** [November 21, 2019, 7:11am UTC](https://forum.crystal-lang.org/t/better-runtime-backtraces/1394 "2019-11-21T07:11:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [November 21, 2019, 7:11am UTC](https://forum.crystal-lang.org/t/better-runtime-backtraces/1394/1 "2019-11-21T07:11:08Z")

</div>

crystal has the ability to generate very descriptive backtraces at compile time:

> $ cat go2.cr  
> def go1  
> go3  
> end
> 
> go1  
> $ crystal build --error-trace go2.cr  
> In go2.cr:5:1
> 
> 5 | go1  
> ^–  
> Error: instantiating ‘go1()’
> 
> In go2.cr:2:3
> 
> 2 | go3  
> ^–  
> Error: undefined local variable or method ‘go3’ for top-level
> 
> Did you mean ‘go1’?

I had a thought that maybe runtime exception traces, like this one:

> $ cat go.cr  
> raise “BOOM”
> 
> $ crystal go.cr  
> Unhandled exception: BOOM (Exception)  
> from go.cr:1:1 in ‘\_\_crystal\_main’  
> from /usr/share/crystal/src/crystal/main.cr:97:5 in ‘main\_user\_code’  
> from /usr/share/crystal/src/crystal/main.cr:86:7 in ‘main’  
> from /usr/share/crystal/src/crystal/main.cr:106:3 in ‘main’  
> from \_\_libc\_start\_main  
> from \_start  
> from ???

could possibly be aumented so that they show more information, like looking up the source code on the lines of the backtrace, at runtime. Thoughts?  
Thanks!

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 21, 2019, 12:39pm UTC](https://forum.crystal-lang.org/t/better-runtime-backtraces/1394/2 "2019-11-21T12:39:33Z")

</div>

This might help debugging during development, but it wouldn’t be useful in production. The additional verbosity would fill up logs and provide no benefit. Often source code wouldn’t be available anyway in the runtime environment. And these enhancements for runtime errors would add a bunch of additional code to every Crystal program, again with often no real gain.

It would be possible though to have enhanced backtraces for development. This should be pretty easy to implement, and could even go entirely into user code. Essentially, you just need to override `Callstack.print_frame` and enhance the frame output.

An alternative solution would be to pipe stderr through a program that enhances the backtraces. That might actually be easier to implement and it’s more flexible because it doesn’t need to be activated at compile time and you can even enhance backtraces from logs.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 21, 2019, 12:44pm UTC](https://forum.crystal-lang.org/t/better-runtime-backtraces/1394/3 "2019-11-21T12:44:44Z")

</div>

Raven.cr’s crash handler would be an example for parsing the error output from a Crystal program and enhancing it: [https://github.com/Sija/raven.cr/blob/master/src/crash\_handler.cr](https://github.com/Sija/raven.cr/blob/master/src/crash_handler.cr)

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [December 8, 2019, 1:13am UTC](https://forum.crystal-lang.org/t/better-runtime-backtraces/1394/4 "2019-12-08T01:13:40Z")

</div>

Thanks. I’ll try making one a shard and if I really like it propose it for the stdlib (in non release compiles only perhaps? :)
