Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active Oct 10, 2020
Embed
What would you like to do?
Failsafe: degrade user experience and notify when something goes wrong.

Failsafe

When something goes wrong I want to degrade the user experience (instead of returning a 500 - Server Error) And I want to be notified about the failure

Usage

Wrap non mission critical code:

Failsafe.failsafe do
  UnreliableApi.send_event "Signup"
end

Wrap complex partials:

<%- Failsafe.failsafe do %>
  <%= render "notifications" %>
<% end %>

Prevent side effects:

Failsafe.failsafe { log_event }
Failsafe.failsafe { notify_recipient }
Failsafe.failsafe { send_event_to_optimizely }
Failsafe.failsafe { send_event_to_kinesis }
module Failsafe
def self.failsafe
yield
rescue StandardError => error
if Rails.env.production?
Rollbar.error error
nil
else
raise error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment