Permalink
Browse files

switched to exception notification via rescuing in middleware, handli…

…ng in notification and re-raising said exception (thanks to workmad3); migration and generators updated for extra attributes.
  • Loading branch information...
1 parent e3afc97 commit fd784349d06b896352b0ea8818737b71cfa505e2 @bsodmike committed Sep 21, 2011
View
5 app/views/epic_fail/failures/index.html.erb
@@ -1,7 +1,10 @@
<h1>Failures</h1>
<ul>
<% @failures.each do |f| %>
- <li>#<%= f.id %>: <%= f.message %></li>
+ <li>
+ <h3>(#<%= f.id %>) <%= f.exception %>: <%= f.message %></h3>
+ <p><%= f.created_at.strftime('%H:%M:%S %Z @ %m/%d/%Y') %></p>
+ </li>
<% end %>
</ul>
View
14 config/initializers/exception_handler.rb
@@ -1,7 +1,7 @@
-ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
- Rails.logger.debug "EXCEPTION: #{payload[:exception]}" if Rails.env.development? && !payload[:exception].blank?
- if payload[:exception]
- name, message = *payload[:exception]
- EpicFail::Failure.create!(:message => message)
- end
-end
+# ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
+# Rails.logger.debug "EXCEPTION: #{payload[:exception]}" if Rails.env.development? && !payload[:exception].blank?
+# if payload[:exception]
+# name, message = *payload[:exception]
+# EpicFail::Failure.create!(:message => message)
+# end
+# end
View
2 db/migrate/20110920230244_create_epic_fail_failures.rb
@@ -1,7 +1,9 @@
class CreateEpicFailFailures < ActiveRecord::Migration
def change
create_table :epic_fail_failures do |t|
+ t.text :exception
t.text :message
+ t.text :backtrace
t.timestamps
end
View
6 lib/epic_fail/engine.rb
@@ -1,5 +1,9 @@
+$:.unshift(File.join(File.dirname(__FILE__), "..", "epic_fail", "exceptions"))
+
require "epic_fail"
require "rails"
+require "epic_fail/exceptions/rack"
+require "pry" if Rails.env.development?
# https://gist.github.com/1228809
module ActionDispatch::Routing
@@ -17,5 +21,7 @@ module EpicFail
class Engine < Rails::Engine
isolate_namespace EpicFail
engine_name :epic_fail
+
+ config.app_middleware.use("EpicFail::Exceptions::Rack")
end
end
View
24 lib/epic_fail/exceptions/rack.rb
@@ -0,0 +1,24 @@
+module EpicFail
+ module Exceptions
+ class Rack
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ # downward logic
+ @app.call(env)
+ # upward logic
+ rescue Exception => e
+ notify_exception(e, env)
+ raise e
+ #@app.call(env) # allow call processing to continue
+ end
+
+ def notify_exception(e, env)
+ # Exception: backtrace exception message
+ EpicFail::Failure.create!(:exception => "#{e.class}", :message => "#{e.message}", :backtrace => "#{e.backtrace}")
+ end
+ end
+ end
+end
View
2 lib/generators/epic_fail/install/templates/migration.rb
@@ -1,7 +1,9 @@
class CreateEpicFailFailures < ActiveRecord::Migration
def change
create_table :epic_fail_failures do |t|
+ t.text :exception
t.text :message
+ t.text :backtrace
t.timestamps
end
View
2 spec/dummy/db/schema.rb
@@ -14,7 +14,9 @@
ActiveRecord::Schema.define(:version => 20110920230244) do
create_table "epic_fail_failures", :force => true do |t|
+ t.text "exception"
t.text "message"
+ t.text "backtrace"
t.datetime "created_at"
t.datetime "updated_at"
end

0 comments on commit fd78434

Please sign in to comment.