From 2507ccc3d13a0155261cde0210d51a1f8e44601d Mon Sep 17 00:00:00 2001 From: Marcin Wyszynski Date: Wed, 24 Feb 2016 12:42:48 +0100 Subject: [PATCH] Add an option to softly fail a pipe --- codequest_pipes.gemspec | 2 +- lib/codequest_pipes/context.rb | 10 +++++++++- lib/codequest_pipes/pipe.rb | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/codequest_pipes.gemspec b/codequest_pipes.gemspec index 8e11dc1..8c7bfec 100644 --- a/codequest_pipes.gemspec +++ b/codequest_pipes.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |spec| spec.name = 'codequest_pipes' - spec.version = '0.2.0' + spec.version = '0.2.1' spec.author = 'codequest' spec.email = 'hello@codequest.com' diff --git a/lib/codequest_pipes/context.rb b/lib/codequest_pipes/context.rb index dc12cda..17adc4d 100644 --- a/lib/codequest_pipes/context.rb +++ b/lib/codequest_pipes/context.rb @@ -32,6 +32,14 @@ def add(values) end end + # Quietly fail the pipe, allowing the error to be saved and accessed from + # the Context. + # + # @param error [Any] Error to be set. + def halt(error = 'Execution stopped') + @error = error + end + # Explicitly fail the pipe, allowing the error to be saved and accessed from # the Context. # @@ -39,7 +47,7 @@ def add(values) # # @raise [ExecutionTerminated] def terminate(error) - @error = error + halt(error) fail ExecutionTerminated, error end diff --git a/lib/codequest_pipes/pipe.rb b/lib/codequest_pipes/pipe.rb index 5e1add5..d2eff11 100644 --- a/lib/codequest_pipes/pipe.rb +++ b/lib/codequest_pipes/pipe.rb @@ -20,10 +20,10 @@ def self.|(other) end def self.call(ctx) + return ctx if ctx.error _validate_ctx(_required_context_elements, ctx) new(ctx).call _validate_ctx(_provided_context_elements, ctx) - ctx end def self.require_context(*args)