0.01
Repository is archived
No release in over 3 years
Low commit activity in last 3 years
Rails error handler that integrates with Sentry and generates 4xx error responses
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.2, >= 5.2.2.1
~> 2.2, >= 2.2.3
~> 0.16
~> 1.10, >= 1.10.4
~> 2.0, >= 2.0.6
~> 1.0, >= 1.0.4
~> 12.0
~> 3.4
~> 0.49
~> 0.11, >= 0.11.1
~> 1.8

Runtime

 Project Readme

Error Handler

This library wraps invocation of Sentry and exception notification for convenience.

You still need to configure Sentry and ExceptionNotification gems separately. This gem is for invocation, not configuration.

Installation

source "https://na.artifactory.swg-devops.com/artifactory/api/gems/apset-ruby" do
  # Report errors to Sentry
  gem "safety_goggles", "~> 2.0"
end

Sample Usage

In a controller such as application_controller.rb:

rescue_from Exception, with: :render_error

def render_error(error)
  require "safety_goggles"
  code = SafetyGoggles::Handler.handle_error(error)

  render status: code, json: { code: code, message: error.to_s }
end

If you'd like to configure HTTP Basic Auth to throw exceptions and trigger the above code path, you can do this in the controller:

include ActionController::HttpAuthentication::Basic::ControllerMethods

MY_USERNAME = ENV.fetch("MY_USERNAME")
MY_PASSWORD = ENV.fetch("MY_PASSWORD")

before_action do
  success = authenticate_with_http_basic do |name, password|
    # This comparison uses & so that it doesn't short circuit and
    # uses `variable_size_secure_compare` so that length information
    # isn't leaked.
    ActiveSupport::SecurityUtils.variable_size_secure_compare(name, MY_USERNAME) &
      ActiveSupport::SecurityUtils.variable_size_secure_compare(password, MY_PASSWORD)
  end

  require "safety_goggles/unauthorized_error"
  raise SafetyGoggles::UnauthorizedError, "HTTP Basic: Access denied." unless success

  success
end

Build as a Gem

  • Travis CI will build and publish a new version of the gem whenever you push a new tag:

    git tag -a 2.0.0 -m v2.0.0 && git push --tags
    
  • Should the need arise, you can install a local version as follows:

    gem build *.gemspec
    gem install *.gem --ignore-dependencies