Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active October 10, 2020 18:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pcreux/0442bd7572b8fed6245931c24c74ccd7 to your computer and use it in GitHub Desktop.
Save pcreux/0442bd7572b8fed6245931c24c74ccd7 to your computer and use it in GitHub Desktop.
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