Peel me a Grape :: We make things work

We Make Things Work :: Blog

The default error pages with a rails app are quite ugly.

It’s nice to have your error pages use the same layout as your app. It’s a good idea to have them as static files – in case your app is catastrophically hosed. But it’s also nice to use your app layouts, so that you’re not manually editing your error pages.

I pretty much followed this guys approach using rake to pre-generate the pages. I made small changes so that the task works without work without having to change routes or add a controller to my app.

task :generate_static_pages => :environment do
  class PreGenerateController < ApplicationController
    def show
      render :action => params[:id]
    end
  end
  require 'action_controller/integration'
  ActionController::Routing::Routes.add_route 'pre_generate/:id', 
                                 :controller => 'pre_generate', :action => 'show'
  app = ActionController::Integration::Session.new
  ['404', '500'].each do |action|
    app.get "/pre_generate/#{action}" 
    File.open("public/#{action}.html", "w") { |f| f.write app.response.body }
  end
end

and I added this to my cap deploy.rb

namespace :deploy do
  task :generate_static_pages do
    invoke_command "cd #{release_path} && rake generate_static_pages RAILS_ENV=#{rails_env}" 
  end
end

after "deploy:finalize_update", "deploy:generate_static_pages" 

Using rails integration testing library does seem like the most convenient, in this way I can generate the page without starting http server.

blog comments powered by Disqus