Skip to content

Commit 284244b

Browse files
committed
Improve and add test for Rake task integration
+Simplify README's explanation of integrations +Make Rake integration automatic +Remove TODO (it has an issue now) Monkey patching #execute isn't as nice as patching #display_error_message, so we'll do that instead. I think monkey patching #execute may also cause Raven to not catch errors called with Rake's #invoke method.
1 parent 520a37d commit 284244b

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

README.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ You'll still want to ensure you've disabled anything that would prevent errors f
3333
config.action_dispatch.show_exceptions = false # this is the default setting in production
3434
```
3535

36-
#### Delayed::Job
37-
38-
No extra configuration required. Usage of [delayed-plugins-raven](https://github.com/qiushihe/delayed-plugins-raven) gem is deprecated.
39-
4036
### Rack
4137

4238
Add ```use Raven::Rack``` to your ```config.ru``` or other rackup file (this is automatically inserted in Rails).
@@ -45,14 +41,9 @@ Add ```use Raven::Rack``` to your ```config.ru``` or other rackup file (this is
4541

4642
Like any other Rack middleware, add ```use Raven::Rack``` to your Sinatra app.
4743

48-
### Sidekiq
49-
50-
Raven includes [Sidekiq middleware](https://github.com/mperham/sidekiq/wiki/Middleware) which takes
51-
care of reporting errors that occur in Sidekiq jobs. To use it, just ```require 'raven/sidekiq'``` after you require Sidekiq. If you are using Sidekiq with Rails, just put this require somewhere in the initializers.
52-
53-
### Rake
44+
### Sidekiq, Delayed::Job and Rake
5445

55-
To report errors within rake tasks, require `raven/rake`. Under Rails, you should place this in an initializer.
46+
Raven works out-of-the-box with all these tools!
5647

5748
## Capturing Events
5849

lib/raven/base.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ def rack_context(env)
207207

208208
# Injects various integrations
209209
def inject
210-
# TODO(dcramer): integrations should have a way to opt-out
211210
require 'raven/integrations/delayed_job' if defined?(::Delayed::Plugin)
212211
require 'raven/railtie' if defined?(::Rails::Railtie)
213212
require 'raven/sidekiq' if defined?(Sidekiq)
214-
require 'raven/tasks' if defined?(Rake)
213+
if defined?(Rake)
214+
require 'raven/rake'
215+
require 'raven/tasks'
216+
end
215217
end
216218

217219
# For cross-language compat

lib/raven/rake.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
require 'raven'
22
require 'rake/task'
33

4-
# Monkey patch Rake::Task#execute to capture and report exceptions on sentry
5-
# Include this file in an initializer
64
module Rake
7-
class Task
8-
alias :orig_execute :execute
9-
def execute(args = nil)
10-
Raven.capture :logger => 'rake', :tags => { 'rake_task' => @name } do
11-
orig_execute(args)
12-
end
5+
class Application
6+
alias :orig_display_error_messsage :display_error_message
7+
def display_error_message(ex)
8+
Raven.capture_exception ex, :logger => 'rake', :tags => { 'rake_task' => @name }
9+
orig_display_error_messsage(ex)
1310
end
1411
end
1512
end
16-
13+

spec/raven/rake_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'rake'
2+
3+
describe 'Rake tasks' do
4+
5+
it 'should capture exceptions in Rake tasks' do
6+
exception = build_exception
7+
8+
expect(`cd spec/support && bundle exec rake raise_exception 2>&1`).to match(/Sending event/)
9+
end
10+
11+
end

spec/support/Rakefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'rake'
2+
require 'rubygems'
3+
require 'raven'
4+
5+
task :raise_exception do
6+
1 / 0
7+
end

0 commit comments

Comments
 (0)