1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-02-01 01:37:12 +00:00

Add Rackstash::Flow.error_flow= as a real setter

We now also allow to explicitly set the local error_flow of an
individual flow to nil to force it to use the global
Rackstash.error_flow again.
This commit is contained in:
Holger Just 2017-10-08 13:12:20 +02:00
parent e532be28c3
commit b64823170f
2 changed files with 49 additions and 16 deletions

View File

@ -80,12 +80,7 @@ module Rackstash
@adapter = Rackstash::Adapters[adapter]
self.encoder = encoder || @adapter.default_encoder
@filter_chain = Rackstash::FilterChain.new(filters)
if error_flow.nil?
@error_flow = nil
else
self.error_flow(error_flow)
end
self.error_flow = error_flow
if block_given?
if block.arity == 0
@ -144,13 +139,38 @@ module Rackstash
@encoder = encoder
end
def error_flow(flow = nil)
if flow.nil?
@error_flow || Rackstash.error_flow
else
flow = Flow.new(flow) unless flow.is_a?(Rackstash::Flow)
@error_flow = flow
# Get or set a separate {Flow} which is used by this flow to write details
# about any unexpected errors during interaction with the {#adapter}. If no
# explicit value is set here, we use {Rackstash.error_flow} by default.
#
# @param error_flow [Flow, nil] if given, set the separate error flow to
# this object
# @return [Rackstash::Flow] the newly set error flow (if given) or the
# currently defined one
# @see #error_flow=
def error_flow(error_flow = nil)
return @error_flow || Rackstash.error_flow if error_flow.nil?
self.error_flow = error_flow
@error_flow
end
# Set a separate {Flow} which is used by this flow to write details
# about any unexpected errors during interaction with the {#adapter}.
#
# If the given object is not already a {Flow}, we will wrap in into one.
# This allows you to also give an adapter or just a plain log target which
# can be wrapped in an adapter.
#
# @param error_flow [Flow, Adapter, Object, nil] the separate error flow or
# `nil` to unset the custom error_flow ant to use the global
# {Rackstash.error_flow} again
# @return [Rackstash::Flow] the newly set error_flow
def error_flow=(error_flow)
unless error_flow.nil? || error_flow.is_a?(Rackstash::Flow)
error_flow = Flow.new(error_flow)
end
@error_flow = error_flow
end
# (see FilterChain#insert_after)

View File

@ -144,8 +144,10 @@ describe Rackstash::Flow do
describe '#error_flow' do
it 'returns the global error_flow by default' do
expect(Rackstash).to receive(:error_flow).and_call_original
expect(Rackstash).to receive(:error_flow).twice.and_call_original
expect(flow.error_flow).to be_instance_of described_class
expect(flow.error_flow(nil)).to be_instance_of described_class
end
it 'can set a custom error_flow' do
@ -155,16 +157,27 @@ describe Rackstash::Flow do
# The error_flow is persisted and is returned afterwards
expect(flow.error_flow).to equal error_flow
end
end
describe '#error_flow=' do
it 'creates a flow object when setting a value' do
# load the flow helper so that the receive test below counts correctly
flow = self.flow
flow
expect(described_class).to receive(:new).with(adapter).and_call_original
new_flow = flow.error_flow(adapter)
flow.error_flow = adapter
expect(flow.error_flow).to be_instance_of described_class
expect(flow.error_flow).to equal new_flow
expect(flow.error_flow.adapter).to equal adapter
end
it 'resets the error_flow when setting nil' do
flow.error_flow = flow
expect(flow.error_flow).to equal flow
expect(flow.error_flow).not_to equal Rackstash.error_flow
flow.error_flow = nil
expect(flow.error_flow).to equal Rackstash.error_flow
end
end