/
rake_handler.rb
71 lines (63 loc) · 2.04 KB
/
rake_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Patch Rake::Application to handle errors with Airbrake
module Airbrake::RakeHandler
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_airbrake, :display_error_message
alias_method :display_error_message, :display_error_message_with_airbrake
end
end
def display_error_message_with_airbrake(ex)
if Airbrake.sender && Airbrake.configuration &&
(Airbrake.configuration.rescue_rake_exceptions ||
(Airbrake.configuration.rescue_rake_exceptions===nil && !self.tty_output?))
Airbrake.notify_or_ignore(ex, :component => reconstruct_command_line, :cgi_data => environment_info)
end
display_error_message_without_airbrake(ex)
end
def reconstruct_command_line
"rake #{ARGV.join( ' ' )}"
end
def environment_info
ENV.reject do |k|
Airbrake.configuration.rake_environment_filters.include? k
end
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
class << self
include Airbrake::RakeHandler
end
end