Skip to content

Commit

Permalink
added Exceptional.rescue for handling exceptions outside the request …
Browse files Browse the repository at this point in the history
…processing loop.
  • Loading branch information
darragh committed Oct 20, 2009
1 parent 5f72996 commit f2dbdca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib/exceptional.rb
Expand Up @@ -2,7 +2,7 @@

require 'exceptional/catcher'
require 'exceptional/startup'
require 'exceptional/logger'
require 'exceptional/log_factory'
require 'exceptional/config'
require 'exceptional/exception_data'
require 'exceptional/remote'
Expand All @@ -13,6 +13,15 @@ module Exceptional
CLIENT_NAME = 'getexceptional-rails-plugin'

def self.logger
::Exceptional::Logger.logger
::Exceptional::LogFactory.logger
end

def self.rescue(&block)
begin
block.call
rescue Exception => e
Exceptional::Catcher.handle(e)
raise(e)
end
end
end
2 changes: 1 addition & 1 deletion lib/exceptional/catcher.rb
@@ -1,7 +1,7 @@
module Exceptional
class Catcher
class << self
def handle(exception, controller, request)
def handle(exception, controller=nil, request=nil)
data = ExceptionData.new(exception, controller, request)
Remote.error(data.to_json)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/exceptional/logger.rb → lib/exceptional/log_factory.rb
@@ -1,7 +1,7 @@
require 'logger'

module Exceptional
class Logger
class LogFactory
def self.logger
@logger ||= create_logger_with_fallback
end
Expand All @@ -12,8 +12,8 @@ def self.create_logger_with_fallback
log_dir = File.join(Config.application_root, 'log')
Dir.mkdir(log_dir) unless File.directory?(log_dir)
log_path = File.join(log_dir, "/exceptional.log")
log = ::Logger.new(log_path)
log.level = ::Logger::INFO
log = Logger.new(log_path)
log.level = Logger::INFO
def log.format_message(severity, timestamp, progname, msg)
"[#{severity.upcase}] (#{[Kernel.caller[2].split('/').last]}) #{timestamp.utc.to_s} - #{msg2str(msg).gsub(/\n/, '').lstrip}\n"
end
Expand Down
17 changes: 17 additions & 0 deletions spec/exceptional_rescue_spec.rb
@@ -0,0 +1,17 @@
require File.dirname(__FILE__) + '/spec_helper'

context 'resuce errors from within a block' do
class FunkyException < StandardError; end
it "send them to catcher and reraise" do
to_raise = FunkyException.new
Exceptional::Catcher.should_receive(:handle).with(to_raise)
begin
Exceptional.rescue do
raise to_raise
end
fail "expected to raise"
rescue FunkyException => e
e.should == to_raise
end
end
end

0 comments on commit f2dbdca

Please sign in to comment.