Skip to content

Instantly share code, notes, and snippets.

@AlexandrBasan
Last active July 27, 2016 03:41
Show Gist options
  • Save AlexandrBasan/d7d6431e47ed84097eb03cfbe0a1b080 to your computer and use it in GitHub Desktop.
Save AlexandrBasan/d7d6431e47ed84097eb03cfbe0a1b080 to your computer and use it in GitHub Desktop.
ExceptionDatabaseSaver middleware for Rails
require_dependency "lib/exception_database_saver.rb"
config.middleware.use ExceptionDatabaseSaver
# Easy custom middleware to save all Exceptions raised by Rails to Audit table to DB
# Created by Alexandr Basan
class ExceptionDatabaseSaver
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
rescue Exception => exception
Audit.create!(
:auditable => 0,
:user => 0,
:action => "error",
:comment => "Exception was rised",
:audited_changes => "#{exception}. #{exception.backtrace}"
)
raise exception
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment