How to display error type in ruby? How to display error type in ruby? ruby ruby

How to display error type in ruby?


begin  raise ArgumentError, "I'm a description"rescue => e  puts "An error of type #{e.class} happened, message is #{e.message}"end

Prints: An error of type ArgumentError happened, message is I'm a description


If you want to show the original backtrace and highlighting, you can take advantage of Exception#full_message:

full_message(highlight: bool, order: [:top or :bottom]) → string

Returns formatted string of exception. The returned string is formatted using the same format that Ruby uses when printing an uncaught exceptions to stderr.

If highlight is true the default error handler will send the messages to a tty.

order must be either of :top or :bottom, and places the error message and the innermost backtrace come at the top or the bottom.

The default values of these options depend on $stderr and its tty? at the timing of a call.

begin  raise ArgumentError, "I'm a description"rescue StandardError => e  puts e.full_message(highlight: true, order: :top)end


My version of printing errors with type, message and trace:

begin    some_statementrescue => e    puts "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace:  \n #{e.backtrace.join("\n")}"    Rails.logger.error "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace:  \n #{e.backtrace.join("\n")}"end