Skip to content

Commit

Permalink
Updated Rake handler to cooperate nicely with Rake 0.9.2, preserving …
Browse files Browse the repository at this point in the history
…compatibility with Rake 0.8.7
  • Loading branch information
leonid-shevtsov committed Jun 12, 2011
1 parent 34521f9 commit 95571c4
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions lib/hoptoad_notifier/rake_handler.rb
@@ -1,42 +1,60 @@
# Patch Rake::Application to handle errors with Hoptoad
# Code taken from Rake source
module HoptoadNotifier::RakeHandler
def standard_exception_handling
begin
yield
rescue SystemExit => ex
# Exit silently with current status
raise
rescue OptionParser::InvalidOption => ex
# Exit silently
exit(false)
rescue Exception => ex
if HoptoadNotifier.configuration.rescue_rake_exceptions ||
(HoptoadNotifier.configuration.rescue_rake_exceptions===nil && !self.tty_output?)

HoptoadNotifier.notify(ex, :component => reconstruct_command_line, :cgi_data => ENV)
end

handle_exception_without_hoptoad(ex)
exit(false)
def self.included(klass)
klass.class_eval do
include Rake087Methods unless defined?(Rake::VERSION) && Rake::VERSION >= '0.9.0'
alias_method :display_error_message_without_hoptoad, :display_error_message
alias_method :display_error_message, :display_error_message_with_hoptoad
end
end

def handle_exception_without_hoptoad(ex)
# Exit with error message
$stderr.puts "#{name} aborted!"
$stderr.puts ex.message
if options.trace
$stderr.puts ex.backtrace.join("\n")
else
$stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
$stderr.puts "(See full trace by running task with --trace)"
def display_error_message_with_hoptoad(ex)
if HoptoadNotifier.configuration.rescue_rake_exceptions ||
(HoptoadNotifier.configuration.rescue_rake_exceptions===nil && !self.tty_output?)

HoptoadNotifier.notify(ex, :component => reconstruct_command_line, :cgi_data => ENV)
end

display_error_message_without_hoptoad(ex)
end

def reconstruct_command_line
"rake #{ARGV.join( ' ' )}"
end

# This module brings Rake 0.8.7 error handling to 0.9.0 standards
module Rake087Methods
# Method taken from Rake 0.9.0 source
#
# Provide standard exception handling for the given block.
def standard_exception_handling
begin
yield
rescue SystemExit => ex
# Exit silently with current status
raise
rescue OptionParser::InvalidOption => ex
$stderr.puts ex.message
exit(false)
rescue Exception => ex
# Exit with error message
display_error_message(ex)
exit(false)
end
end

# Method extracted from Rake 0.8.7 source
def display_error_message(ex)
$stderr.puts "#{name} aborted!"
$stderr.puts ex.message
if options.trace
$stderr.puts ex.backtrace.join("\n")
else
$stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
$stderr.puts "(See full trace by running task with --trace)"
end
end
end
end

Rake.application.instance_eval do
Expand Down

0 comments on commit 95571c4

Please sign in to comment.