Skip to content

Commit

Permalink
Merge pull request #1309 from BigG1947/send-sentry-when-csv-download-…
Browse files Browse the repository at this point in the history
…failed-with-exception

send sentry when csv download failed with exception
  • Loading branch information
senid231 authored Jul 3, 2023
2 parents 5eabc45 + 24a8395 commit b671bd8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/initializers/active_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@

config.authorization_adapter = PolicyAdapter
config.pundit_default_policy = 'DefaultApplicationPolicy'

# Enables CSV streaming in development and test modes.
# Useful to test streaming bugs locally.
config.disable_streaming_in = []
end

Dir[Rails.root.join('lib/active_admin/**/*.rb')].each { |s| require s }
Expand Down Expand Up @@ -224,4 +228,6 @@

ActiveAdmin::ResourceDSL.include Rails.application.routes.url_helpers
ActiveAdmin::ResourceDSL.include ApplicationHelper

ActiveAdmin::ResourceController.prepend ActiveAdmin::CsvStreamWithErrorHandler
end
27 changes: 27 additions & 0 deletions lib/active_admin/csv_stream_with_error_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module ActiveAdmin
module CsvStreamWithErrorHandler
protected

def stream_resource(&)
block_with_error_handler = wrap_stream_error_handling(&)
super(&block_with_error_handler)
end

private

# When we stream response block is executed in Puma after rails done with request.
# All exceptions are rescued and suppressed.
def wrap_stream_error_handling
error_context = capture_context
proc do |csv|
yield(csv)
rescue StandardError => e
CaptureError.log_error(e)
CaptureError.capture(e, error_context)
raise e
end
end
end
end

0 comments on commit b671bd8

Please sign in to comment.