No commit activity in last 3 years
No release in over 3 years
Simple formatter for ruby exception messages.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
>= 0
 Project Readme

Build Status

ExceptionFormatter

Note: There is also a gem named exception-formatter (dash not underscore) and maybe you're looking for that.

Purpose

I was sick of writing "#{exception.class.name}: #{exception.message}\n#{exception.backtrace.join("\n")}" and alike.

Simple formatter for ruby exception messages.

Features:

  • Custom format pattern
  • Nested exception support (see gems like nested_exceptions, nestegg, nesty)
  • ActiveRecord:::RecordInvalid exception support

Installation

Add this line to your application's Gemfile:

gem 'exception_formatter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install exception_formatter

Examples

Using default pattern

If no pattern is given the default pattern is used (see ExceptionFormatter::DEFAULT_PATTERN):

begin
  ... code ... code ... code
rescue => ex
  logger.error ExceptionFormatter.format(ex)
end

Will produce the string:

  StandardError: the exception message
  file:31
  file:76

Using custom pattern

begin
  ... code ... code ... code
rescue => ex
  logger.error ExceptionFormatter.format(ex, pattern: "[%k] %m\nStacktrace:\n%b")
end

Will produce the string:

  [StandardError] the exception message
  Stacktrace:
  file:31
  file:76

Alternatively use instance method:

FORMATTER = ExceptionFormatter.new(pattern: "[%k] %m\n%b")

begin
  ... code ... code ... code
rescue => ex
  logger.error FORMATTER.format(ex)
end

Available pattern

%b         = exception backtrace, lines joined with "\n"
%c         = cause, the nested exception message (by calling exception.cause.message)
%c{method} = cause, the nested exception message (by calling exception.method.message)
             (useful if nested exception does not respond to #cause but to #reason or what ever)
%k         = exception class name
%m         = exception message
%r         = record errors, the record error messages in case of ActiveRecord:::RecordInvalid exception
             (by calling exception.record.errors.full_messages)
%x{method} = message return by calling exception.send(method)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request