Skip to content

Commit

Permalink
Refactor Base
Browse files Browse the repository at this point in the history
  • Loading branch information
nateberkopec committed Dec 24, 2014
1 parent 43bb3c6 commit fb94dca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 52 deletions.
76 changes: 26 additions & 50 deletions lib/raven/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,64 +78,31 @@ def send(evt)
# Raven.capture do
# MyApp.run
# end
def capture(options = {}, &block)
if block
begin
block.call
rescue Error
raise # Don't capture Raven errors
rescue Exception => e
capture_exception(e, options)
raise
end
else
# Install at_exit hook
at_exit do
if $ERROR_INFO
logger.debug "Caught a post-mortem exception: #{$ERROR_INFO.inspect}"
capture_exception($ERROR_INFO, options)
end
end
def capture(options = {})
install_at_exit_hook unless block_given?
begin
yield
rescue Error
raise # Don't capture Raven errors
rescue Exception => e
capture_exception(e, options)
raise
end
end

def capture_exception(exception, options = {})
send_or_skip(exception) do
if (evt = Event.from_exception(exception, options))
yield evt if block_given?
if configuration.async?
configuration.async.call(evt)
else
send(evt)
end
end
return unless configuration.should_send?(exception)
if (evt = Event.from_exception(exception, options))
yield evt if block_given?
send(evt)
end
end

def capture_message(message, options = {})
send_or_skip(message) do
if (evt = Event.from_message(message, options))
yield evt if block_given?
if configuration.async?
configuration.async.call(evt)
else
send(evt)
end
end
end
end

def send_or_skip(exc)
should_send = if configuration.should_send
configuration.should_send.call(*[exc])
else
true
end

if configuration.send_in_current_environment? && should_send
yield if block_given?
else
configuration.log_excluded_environment_message
return unless configuration.should_send?(message)
if (evt = Event.from_message(message, options))
yield evt if block_given?
send(evt)
end
end

Expand Down Expand Up @@ -226,5 +193,14 @@ def inject
alias :captureMessage :capture_message
alias :annotateException :annotate_exception
alias :annotate :annotate_exception

private

def install_at_exit_hook
at_exit do
logger.debug "Caught a post-mortem exception: #{$ERROR_INFO.inspect}"
capture_exception($ERROR_INFO, options)
end
end
end
end
5 changes: 5 additions & 0 deletions lib/raven/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def send(event)
return
end

if configuration.async
configuration.async.call(event)
return
end

# Set the project ID correctly
event.project = self.configuration.project_id

Expand Down
10 changes: 10 additions & 0 deletions lib/raven/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ def async=(value)

alias_method :async?, :async

def should_send?(exc)
callback_result = should_send ? should_send.call(*[exc]) : true
if callback_result && send_in_current_environment?
true
else
log_excluded_environment_message
false
end
end

# Allows config options to be read like a hash
#
# @param [Symbol] option Key for a given attribute
Expand Down
2 changes: 0 additions & 2 deletions spec/raven/raven_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

it 'sends the result of Event.capture_message' do
expect(Raven::Event).to receive(:from_message).with(message, options)
expect(Raven).not_to receive(:send).with(event)

prior_async = Raven.configuration.async
Raven.configuration.async = lambda { :ok }
Expand Down Expand Up @@ -60,7 +59,6 @@

it 'sends the result of Event.capture_exception' do
expect(Raven::Event).to receive(:from_exception).with(exception, options)
expect(Raven).not_to receive(:send).with(event)

prior_async = Raven.configuration.async
Raven.configuration.async = lambda { :ok }
Expand Down

0 comments on commit fb94dca

Please sign in to comment.