Rescue and report exceptions in non-critical code
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
lib Version bump to 0.2.1 Feb 25, 2018
test
.gitignore
.travis.yml Added Travis Feb 25, 2018
CHANGELOG.md
Gemfile
LICENSE.txt
README.md Moved installation section higher [skip ci] Jan 4, 2019
Rakefile Updated style Mar 11, 2015
safely_block.gemspec

README.md

Safely

safely do
  # keep going if this code fails
end

Exceptions are rescued and automatically reported to your favorite reporting service.

In development and test environments, exceptions are raised so you can fix them.

Read more

Build Status

Installation

Add this line to your application’s Gemfile:

gem 'safely_block'

Use It Everywhere

“Oh no, analytics brought down search”

safely { track_search(params) }

“Recommendations stopped updating because of one bad user”

users.each do |user|
  safely { update_recommendations(user) }
end

Also aliased as yolo.

Features

Specify a default value to return on exceptions

show_banner = safely(default: true) { show_banner_logic }

Raise specific exceptions

safely except: ActiveRecord::RecordNotUnique do
  # all other exceptions will be rescued
end

Pass an array for multiple exception classes.

Rescue only specific exceptions

safely only: ActiveRecord::RecordNotUnique do
  # all other exceptions will be raised
end

Silence exceptions

safely silence: ActiveRecord::RecordNotUnique do
  # code
end

Throttle reporting with:

safely throttle: {limit: 10, period: 1.minute} do
  # reports only first 10 exceptions each minute
end

Note: The throttle limit is approximate and per process.

Reporting

Reports exceptions to a variety of services out of the box thanks to Errbase.

Customize reporting with:

Safely.report_exception_method = -> (e) { Rollbar.error(e) }

By default, exception messages are prefixed with [safely]. This makes it easier to spot rescued exceptions. Turn this off with:

Safely.tag = false

To report exceptions manually:

Safely.report_exception(e)

Data Protection

To protect the privacy of your users, do not send personal data to exception services. Filter sensitive form fields, use ids (not email addresses) to identify users, and mask IP addresses.

With Rollbar, you can do:

Rollbar.configure do |config|
  config.person_id_method = "id" # default
  config.scrub_fields |= [:birthday]
  config.anonymize_user_ip = true
end

While working on exceptions, be on the lookout for personal data and correct as needed.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help: