DebugMate
Exception handling for Ruby on Rails
Installation
Add this line to your application's Gemfile:
gem 'debugmate', '~> 0.1.2'And then execute:
bundle installOr install it yourself as:
gem install debugmateUsage
Create config/debugmate.yml in your rails application and provide the following values:
data:
domain: your-domain
enabled: true
token: your-tokenCreate config/initializers/debugmate.rb with the following line:
DEBUGMATE_CONFIG = YAML.load_file("#{Rails.root}/config/debugmate.yml")Get the last executed query with the exception
To be able to get the query, you must add the following boilerplate code to the debugmate.rb initializer.
This is the only way to get Rails to intercept the call for the moment:
module ActiveRecord
class LogSubscriber < ActiveSupport::LogSubscriber
# this allows us to call the original sql method. Ruby does copy the original method on the fly
alias_method :original_sql, :sql
def sql(event)
Debugmate::ExceptionHandler.last_executed_query = event.payload[:sql] if DEBUGMATE_CONFIG['data']['enabled'] === true
Debugmate::ExceptionHandler.last_executed_binds = event.payload[:binds] if DEBUGMATE_CONFIG['data']['enabled'] === true
original_sql(event)
end
end
endEdit config/application.rb to use Debugmate.
Add the middleware inside the Application class, ideally as the last line, following the example:
module Blog
class Application < Rails::Application
# --- Here is the exiting code ---
config.middleware.use Debugmate::ExceptionHandler
end
endTo verify that your Debugmate setup is correct, follow these steps:
- Create the Rake Task File Add the following content to a new file called lib/tasks/debugmate_task.rake in your project
namespace :debugmate do
desc "Send fake data to webhook"
task test: :environment do
Debugmate::ExceptionHandler.send_test
end
end- Run the Test Command After adding the Rake task, run the following command in your terminal to test the setup:
rails debugmate:testThis command will send fake data to your configured webhook, allowing you to verify that Debugmate is working correctly.
Get the current User
As there are a lot of ways an user can be retrieved and maybe even the application has no user at all, if you want
to know the current user that caused the exception, you must provide a public method called current_user in your
ApplicationController.
This method must return a Hash with the id, name and email.
You can follow the example. Debugmate only needs the Hash:
class ApplicationController < ActionController::Base
def current_user
# --- some logic that gets the user for your application ---
user = {
id: 99,
name: "User from Rails",
email: "user@fromrails.com"
}
end
endSending exceptions manually to Debugmate
If you want to catch an exception manually, you can do so using the Publish class. Just pass the exception and call
execute
def index
@articles = Article.test
rescue => my_error
begin
Debugmate::Publish.new(my_error).execute
end
endSending information about the request
If you want Debugmate to register more details about the exception, just pass the request along:
def index
@articles = Article.test
rescue => my_error
begin
Debugmate::Publish.new(my_error, request).execute
end
endSending extra info to Debugmate
If you want to send some extra info about the exception, you can make use of the extra_data parameter. Debugmate will
display it in its Context tab.
def index
@articles = Article.test
rescue => my_error
begin
extra = {hello: 'world'}
Debugmate::Publish.new(my_error, request, extra).execute
end
endRunning tests
Go to the gem folder and run bundler to install the gem dependencies locally
bundleTo run the available tests, go to the gem folder and execute
bundle exec rspecYou can get the list of passing tests with the doc format:
bundle exec rspec --format docContributing
Contribution directions go here.
License
The gem is available as open source under the terms of the MIT License.