Reducing Developer Friction With Actionable Errors

Juraj Kostolanský September 23, 2022

Rails allows us to add a custom button right to the default error pages to perform our custom action. This ActiveSupport::ActionableError feature is supported since Rails 6.

As an example, we can be using the db/seeds.rb file to populate our database and create the initial user. To reduce some friction, we can check in our application if there are any users in the database and if not, we can populate it right from the error page.

Actionable errors require a custom error object, so let’s create one:

class MissingSeedsError < StandardError
  include ActiveSupport::ActionableError

  def message
    'Missing seeds in the database'
  end

  action 'Run db:seeds' do
    Rails.application.load_seed
  end
end

We can add a before_action to our ApplicationController to check if there are any users:

ApplicationController < ActionController::Base
  before_action :check_db_seeds, if: -> { Rails.env.development? }

  # ...

  private
  
  def check_db_seeds
    raise MissingSeedsError if User.none?
  end
end

And here is the result:

ActionableError Example

Let's stay in touch

Do you like what you read? Subscribe to get my content on web development, programming, system administration, side projects and more. No spam, unsubscribe at any time.