|
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 | + |
3 | 7 | 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 |
9 | 21 | 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 |
10 | 50 |
|
11 | 51 | def reconstruct_command_line |
12 | 52 | "rake #{ARGV.join(' ')}" |
13 | 53 | end |
14 | 54 | end |
15 | 55 | end |
16 | 56 |
|
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 |
24 | 60 | end |
25 | 61 | end |
0 commit comments