The idea behind Rollbar::Mode
is inspired from the Emacs major and minor
modes, which alter the behaviour of the editor in certain cases. This is
the equivalent of a "minor mode", which alters the way Rollbar integrates
with your code:
- In development it automatically re-raises exceptions logged with Rollbar
- In production it alters some default values for better integration with Heroku
In development (i.e. when the Rollbar token is not set), the reporter will not send errors to Rollbar but re-raise them, allowing the following code pattern to work well both in development and in production:
def find(id)
user = method_that_could_raise_exception(id)
rescue StandardError => exception
Rollbar.error(exception)
nil
end
As of v2.22 of the gem, Rollbar has a setting called raise_on_error
but will
only work if Rollbar believes it's enabled (the access token is enabled). See
the early return
in the code snippet below, taken from
Rollbar::Notifier#log
:
def log(level, *args)
return 'disabled' unless enabled?
message, exception, extra, context = extract_arguments(args)
use_exception_level_filters = use_exception_level_filters?(extra)
return 'ignored' if ignored?(exception, use_exception_level_filters)
begin
status = call_before_process(:level => level,
:exception => exception,
:message => message,
:extra => extra)
return 'ignored' if status == 'ignored'
rescue Rollbar::Ignore
return 'ignored'
end
level = lookup_exception_level(level, exception,
use_exception_level_filters)
ret = report_with_rescue(level, message, exception, extra, context)
raise(exception) if configuration.raise_on_error && exception
ret
end
If the Rollbar access token is not availble and the raise_on_errror
setting
is set to true
, this gem will monkeypatch the log
method described above
to side-steps the error delivery mechanism and automatically re-raise the
exception.
In order to make sure that the gem only affects your code in development, you
can enable it only in the Bundler development
group:
group :development do
gem 'rollbar-mode'
end
Rollbar has several configuration options that help with error reporting, like:
code_version
for linking stack traces to GitHub (SemVer, git SHA)environment
which defaults to "unspecified"populate_empty_backtraces
for manually initialized exceptionsuse_async
for reporting errors using eithergirl_friday
or threading
Rollbar::Mode
will alter the default configuration for Rollbar to these
values:
code_version
will be set to the value ofHEROKU_SLUG_COMMIT
if availableenvironment
will be set to the value ofHEROKU_APP_NAME
, if availablepopulate_empty_backtraces
will be set totrue
(normal default isfalse
)use_async
will be set totrue
(normal default isfalse
)
The HEROKU_*
environment variables are provided by the Dyno Metadata
feature and has to be enabled manually.
If you do not want these changes, make sure the gem is in the development group.
Bug reports and pull requests are welcome on GitHub at https://github.com/andreimaxim/rollbar-mode.