Skip to content

Commit 1a4d16b

Browse files
committed
Patch Rake::Task#execute instead of Rake::Application#display_error_message
This is based on how https://github.com/airbrake/airbrake/blob/master/lib/airbrake/rake.rb does it Fixes #26
1 parent a5b24ec commit 1a4d16b

3 files changed

Lines changed: 68 additions & 14 deletions

File tree

lib/exception_notifier/rake.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'exception_notifier/rake/rails' if defined?(Rails)
12
require 'exception_notifier/rake/rake'
23
require 'exception_notifier/rake/rake_patch'
34
require 'exception_notifier/rake/version'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Based on/adapted from https://github.com/airbrake/airbrake/blob/master/lib/airbrake/rails.rb
2+
3+
module ExceptionNotifier
4+
class Rake
5+
class Railtie < ::Rails::Railtie
6+
rake_tasks do
7+
# Report exceptions occurring in Rake tasks.
8+
require 'exception_notifier/rake/rake_patch'
9+
# Work around https://github.com/nikhaldi/exception_notification-rake/issues/26
10+
# Rake::TaskManager won't have been defined when rake_patch.rb was first loaded.
11+
if Rails.env.development?
12+
load 'exception_notifier/rake/rake_patch.rb'
13+
end
14+
end
15+
end
16+
end
17+
end
Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,61 @@
1-
# Monkey patching patterns lifted from
2-
# https://github.com/thoughtbot/airbrake/blob/master/lib/airbrake/rake_handler.rb
1+
# Copied/adapted from https://github.com/airbrake/airbrake/blob/master/lib/airbrake/rake.rb
2+
3+
if Rake.const_defined?(:TaskManager)
4+
Rake::TaskManager.record_task_metadata = true
5+
end
6+
37
module ExceptionNotifier
4-
module RakePatch
5-
def display_error_message(ex)
6-
super(ex)
7-
ExceptionNotifier::Rake.maybe_deliver_notification(ex,
8-
:rake_command_line => reconstruct_command_line)
8+
module RakeTaskPatch
9+
# A wrapper around the original +#execute+, that catches all errors and
10+
# passes them on to ExceptionNotifier.
11+
#
12+
# rubocop:disable Lint/RescueException
13+
def execute(args = nil)
14+
super(args)
15+
rescue Exception => ex
16+
ExceptionNotifier::Rake.maybe_deliver_notification(
17+
ex,
18+
task_info,
19+
)
20+
raise ex
921
end
22+
# rubocop:enable Lint/RescueException
23+
24+
private
25+
26+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize
27+
def task_info
28+
info = {}
29+
30+
info[:rake_command_line] = reconstruct_command_line
31+
info[:name] = name
32+
info[:timestamp] = timestamp.to_s
33+
info[:investigation] = investigation
34+
35+
info[:full_comment] = full_comment if full_comment
36+
info[:arg_names] = arg_names if arg_names.any?
37+
info[:arg_description] = arg_description if arg_description
38+
info[:locations] = locations if locations.any?
39+
info[:sources] = sources if sources.any?
40+
41+
if prerequisite_tasks.any?
42+
info[:prerequisite_tasks] = prerequisite_tasks.map do |p|
43+
p.__send__(:task_info)
44+
end
45+
end
46+
47+
info
48+
end
49+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/AbcSize
1050

1151
def reconstruct_command_line
1252
"rake #{ARGV.join(' ')}"
1353
end
1454
end
1555
end
1656

17-
# Only do this if we're actually in a Rake context. In some contexts (e.g.,
18-
# in the Rails console) Rake might not be defined.
19-
if Object.const_defined?(:Rake) && Rake.respond_to?(:application)
20-
Rake.application.instance_eval do
21-
class << self
22-
prepend ExceptionNotifier::RakePatch
23-
end
57+
module Rake
58+
class Task
59+
prepend ExceptionNotifier::RakeTaskPatch
2460
end
2561
end

0 commit comments

Comments
 (0)