From b64823170faa597e365f63d2734ffb78187f6bcc Mon Sep 17 00:00:00 2001 From: Holger Just Date: Sun, 8 Oct 2017 13:12:20 +0200 Subject: [PATCH] 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. --- lib/rackstash/flow.rb | 44 +++++++++++++++++++++++++++---------- spec/rackstash/flow_spec.rb | 21 ++++++++++++++---- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/lib/rackstash/flow.rb b/lib/rackstash/flow.rb index 479dcc4..4b01313 100644 --- a/lib/rackstash/flow.rb +++ b/lib/rackstash/flow.rb @@ -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) diff --git a/spec/rackstash/flow_spec.rb b/spec/rackstash/flow_spec.rb index 73552ff..764233d 100644 --- a/spec/rackstash/flow_spec.rb +++ b/spec/rackstash/flow_spec.rb @@ -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