Skip to content

Commit

Permalink
1182 setup bugsnag (#1184)
Browse files Browse the repository at this point in the history
* Add bugsnag gem

* Add bugsnag api key to credentials

* Add bugsnag initialize

* Reorder rescue in AppController

* Log handled exceptions

* Exclude authz errors from logging

* Avoid re-notifying an exception

* Update app/controllers/application_controller.rb

Co-authored-by: Justin <[email protected]>

---------

Co-authored-by: Justin <[email protected]>
  • Loading branch information
mononoken and jmilljr24 authored Dec 4, 2024
1 parent f24f961 commit 9d3c4b3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ gem "dry-initializer", "~> 3.1"
# Catch unsafe migrations
gem "strong_migrations", "~> 2.1"

# Track errors in prod
gem "bugsnag", "~> 6.27"

group :development, :test, :staging do
gem "faker"
end
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ GEM
activemodel (>= 6.1)
brakeman (6.2.2)
racc
bugsnag (6.27.1)
concurrent-ruby (~> 1.0)
builder (3.3.0)
capybara (3.40.0)
addressable
Expand Down Expand Up @@ -554,6 +556,7 @@ DEPENDENCIES
bootstrap
bootstrap_form (~> 5.4)
brakeman
bugsnag (~> 6.27)
capybara
city-state (~> 1.1.0)
cuprite
Expand Down
19 changes: 14 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
verify_authorized unless: :devise_controller?

before_action :set_current_user
around_action :switch_locale

KNOWN_ERRORS = [ActionPolicy::Unauthorized]
rescue_from StandardError, with: :log_and_reraise
rescue_from ActionPolicy::Unauthorized do |ex|
flash[:alert] = t("errors.authorization_error")

redirect_back_or_to root_path
end

def set_current_user
Current.user = current_user
end
Expand All @@ -14,9 +21,11 @@ def switch_locale(&action)
I18n.with_locale(locale, &action)
end

rescue_from ActionPolicy::Unauthorized do |ex|
flash[:alert] = t("errors.authorization_error")

redirect_back fallback_location: root_path
def log_and_reraise(error)
unless KNOWN_ERRORS.include?(error.class)
Bugsnag.notify(error)
error.instance_eval { def skip_bugsnag = true }
end
raise
end
end
2 changes: 1 addition & 1 deletion config/credentials/production.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
B8stKWMaJ/RVHL44hfyXskbZj+ioVbNZ0V4Jx8u6JSIhY0rbL7QDNqZGnguv1jR/OUN1DNeR6MvNP3nPAfXuSFW9Iq3T5wZgqOga5k4cB/wupr66FyBx+EkuE8Xp6ghDrCZHoxkAyMkly7WfXzpp1linj4Dt5YyU2V7cK7b6JA4JEa/1/8faHeNC2h4PwSHbQqoVyGk24yvu9vSn8/0q0Wp8gSwh8/1gPWva5Xa4S65CaMHgl9OqavtwZ7EpV9infVYoQd0cbhT9nK/EutMl9/n6hraxAir/XsiNJZvC7teDGfpWMw0gR3U8AbL9rFVSqXOfeDymjPM2GCf/mMe8eeAO9TKXQJcvcQ37fVOwqMgzmMGQA6+KcZJDY81JI9Dwz+u9X2+gh88f0rP7BkxTtTl7EbSTLqWhNCvPNE0lm252vJ2EVWcK6rNaYwr8gmFNmsF8jC7WbGnrZyM5TB68rgkoIgLfAZxo0W5Xhsahdv8ULF4QtkH+jbDD1EesR+bLp2GlWgl0G/EGRwYt0fUml8Vzp8hd2XqGLLndkA2NjSrnWgtJdjZ5/Qe1LDUrNLtXo0q1hIrBjlpNZZjYqA6wsEPiqSca92Lbpv9ib56FSWfzjgBJQtxpJYP3YUGak5TmJUomu+RRB2cHfCMTIKhrV4PmsOd8vROq0Rm8X6FotQ9eSWZ//ej1yZagW0vGpcSv4ubn9zr5tF+sABr1cCga98HYkgU+uSLtyV5eDfMvglT4FxQ4uIbAJjhe5jn+sXHHR179YrH+BEU3EnuSkquHJe/kXnRJHO4pUyragaWvlMEYPJjkhnlAyYkfcpRUI4Zu8g1AoOcjyzBzTui6qhC6hcNSxqf+vE5saNHA1ySIM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bjwk+mwWsMsnlDIF38Fbrkt8YyMfJ9CBeFaAzi1BJw54NJ784UluEo3QUc5T1WRan3H6Nb5dgBSq9VhWGV0QKSJEtc2IvmDrfE3qn8IaR3E7NZbV6UoeczwEwnTI9HuvrZ0WkNdNugx8E1Dk/c8ZVjLmtcsB2XlQtW4mtzWGj+k9ZUuRCXe032xQwJbgHtBHbtVLi4SbRvYyiU5m4JfORuaMMu/V9EZXPjLvAyWdRlVKDptu+CbPgbbG0GPMSOpx/XfV0QgolZ0kgUTemawSTeBDVnkvufwIaOfImfEt5U/QnMUYJyR0ZBpjAXNqaE+bb+qVe+yBOJIWY6SETlPfIhWDfVfCDm+RY--BTHcPE2kQRjxri/C--sSAa87XeJ0VAblR+MOmoBg==
7 changes: 7 additions & 0 deletions config/initializers/bugsnag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if Rails.application.credentials.bugsnag_api_key.present?
Bugsnag.configure do |config|
config.api_key = Rails.application.credentials.bugsnag_api_key
end
else
Bugsnag.configuration.logger = ::Logger.new("/dev/null")
end

0 comments on commit 9d3c4b3

Please sign in to comment.