add error handling to deferred execution
This commit is contained in:
parent
f8ebf9fbb1
commit
1c2744a3aa
12
README.md
12
README.md
@ -27,16 +27,20 @@ def call(env)
|
||||
end
|
||||
```
|
||||
|
||||
Nothing to configure, just drop it in to your Rack middleware stack and
|
||||
use Thin as your server:
|
||||
Only one thing to configure, the error handler if something explodes during the deferred
|
||||
callback (since you no longer have your Rack handlers at that point):
|
||||
|
||||
``` ruby
|
||||
# for Rails:
|
||||
|
||||
config.middleware.insert_before(::Rack::Lock, ::Rack::EMStream)
|
||||
config.middleware.insert_before(::Rack::Lock, ::Rack::EMStream) do |exception, environment|
|
||||
# do something when there's a deferred error
|
||||
end
|
||||
|
||||
# for Rack::Builder and derivatives:
|
||||
|
||||
use Rack::EMStream
|
||||
use Rack::EMStream do |exception, environment|
|
||||
# do something when there's a deferred error
|
||||
end
|
||||
```
|
||||
|
||||
|
@ -4,8 +4,8 @@ module Rack
|
||||
class EMStream
|
||||
include EventMachine::Deferrable
|
||||
|
||||
def initialize(app)
|
||||
@app = app
|
||||
def initialize(app, &block)
|
||||
@app, @block = app, block
|
||||
end
|
||||
|
||||
def each(&b)
|
||||
@ -22,7 +22,20 @@ module Rack
|
||||
EM.next_tick {
|
||||
env['async.callback'].call [ result[0], result[1], self ]
|
||||
|
||||
result[2].each { |data| EM.next_tick { @callback.call(data) } }
|
||||
begin
|
||||
result[2].each { |data|
|
||||
EM.next_tick {
|
||||
begin
|
||||
@callback.call(data)
|
||||
rescue => e
|
||||
@callback.call(@block.call(e, env)) if @block
|
||||
end
|
||||
}
|
||||
}
|
||||
rescue => e
|
||||
@callback.call(@block.call(e, env)) if @block
|
||||
end
|
||||
|
||||
EM.next_tick { succeed }
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
module Rack
|
||||
class EMStream
|
||||
VERSION = "0.1.0"
|
||||
VERSION = "0.1.1"
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user