Skip to content

Commit

Permalink
Use Exception#detailed_message to show backtrace (#1952)
Browse files Browse the repository at this point in the history
Before:

```
Use Ctrl-C to stop
2023-10-13 18:49:17 - NoMethodError - undefined method `time' for 1:Integer:
        test.rb:4:in `block in <main>'
        /home/mame/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sinatra-3.1.0/lib/sinatra/base.rb:1775:in `call'
```

After:

```
Use Ctrl-C to stop
2023-10-13 18:49:17 - NoMethodError - undefined method `time' for 1:Integer:

          1.time {}
           ^^^^^
        Did you mean?  times
        test.rb:4:in `block in <main>'
        /home/mame/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/sinatra-3.1.0/lib/sinatra/base.rb:1775:in `call'
```

Co-authored-by: Patrik Ragnarsson <patrik@starkast.net>
  • Loading branch information
mame and dentarg committed Oct 18, 2023
1 parent 6ddd6b6 commit d011cc4
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,19 @@ def error_block!(key, *block_params)
end

def dump_errors!(boom)
msg = ["#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - #{boom.class} - #{boom.message}:", *boom.backtrace].join("\n\t")
if boom.respond_to?(:detailed_message)
msg = boom.detailed_message(highlight: false)
if msg =~ /\A(.*?)(?: \(#{ Regexp.quote(boom.class.to_s) }\))?\n/
msg = $1
additional_msg = $'.lines(chomp: true)
else
additional_msg = []
end
else
msg = boom.message
additional_msg = []
end
msg = ["#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - #{boom.class} - #{msg}:", *additional_msg, *boom.backtrace].join("\n\t")
@env['rack.errors'].puts(msg)
end

Expand Down

0 comments on commit d011cc4

Please sign in to comment.