Ruby 2.6 added options to Exception#full_message

by Atul Bhosale,

Ruby 2.6.0-preview2 was recently released.

Ruby 2.5.0

The Exception#full_message method returns a formatted string of the exception.

>> e = StandardError.new('foo')
=> #<StandardError: foo>
>> e.full_message
=> "\e[1mTraceback\e[m (most recent call last):\n(irb):14:in `full_message': \e[1mfoo (\e[1;4mStandardError\e[m\e[1m)\e[m\n"

The string contains escape sequences too.

It was proposed that escape sequences should be excluded from the error message.

Nobuyoshi Nakada said that since Exception#full_message is expected to return the message printed to stderr, escape sequences are intentional.

Benoit Daloze suggested that we can provide an option to disable escape sequences and it was approved.

Ruby 2.6.0

Ruby 2.6.0 provides highlight option to the Exception#full_message method to exclude escape sequences.

>> e = StandardError.new('foo')
=> #<StandardError: foo>
>> e.full_message
=> "\e[1mTraceback\e[m (most recent call last):\n(irb):11:in `full_message': \e[1mfoo (\e[1;4mStandardError\e[m\e[1m)\e[m\n"
>> e.full_message(highlight: false)
=> "Traceback (most recent call last):\n(irb):12:in `full_message': foo (StandardError)\n"

The order argument provides options to place the error message and the innermost backtrace come at the top or the bottom of the result returned by Exception#full_message. The order value must be either :top or :bottom.

>> e = StandardError.new('foo')
=> #<StandardError: foo>
>> e.full_message
=> "\e[1mTraceback\e[m (most recent call last):\n(irb):2:in `full_message': \e[1mfoo (\e[1;4mStandardError\e[m\e[1m)\e[m\n"
>> e.full_message(highlight: false)
=> "Traceback (most recent call last):\n(irb):3:in `full_message': foo (StandardError)\n"
>> e.full_message(highlight: false, order: :top)
=> "(irb):4:in `full_message': foo (StandardError)\n"
>> e.full_message(highlight: false, order: :bottom)
=> "Traceback (most recent call last):\n(irb):5:in `full_message': foo (StandardError)\n"

Let's set a backtrace for an error object and try order option for Exception#full_message method.

>> e = StandardError.new('foo')
=> #<StandardError: foo>
>> e.set_backtrace(["a.rb:1", "b.rb:2"])
=> ["a.rb:1", "b.rb:2"]
>> e.full_message(highlight: false, order: :top)
=> "a.rb:1: foo (StandardError)\n\tfrom b.rb:2\n"
>> e.full_message(highlight: false, order: :bottom)
=> "Traceback (most recent call last):\n\t1: from b.rb:2\na.rb:1: foo (StandardError)\n"

Hope you use these options with Exception#full_message to debug ruby applications.

Here is the relevant commit and discussion.

More articles

Convert Figma design to code

Learn how to use the create-component package to instantly convert Figma designs into static UI components for React, Next.js, Vue, Angular, and more—styled with Tailwind CSS. This guide covers setup, design rules, conversion from design to code, and best practices for rapid, consistent UI development.

Read more

Keycloak with Google Login

This guide explains how to configure Google as an identity provider in Keycloak and allow users to log in only with Google..

Read more

Ready to Build Something Amazing?

Codemancers can bring your vision to life and help you achieve your goals