lib/sfn/command_module/callbacks.rb in sfn-3.0.28 vs lib/sfn/command_module/callbacks.rb in sfn-3.0.30

- old
+ new

@@ -3,24 +3,29 @@ module Sfn module CommandModule # Callback processor helpers module Callbacks - include Bogo::Memoization # Run expected callbacks around action # # @yieldblock api action to run # @yieldresult [Object] result from call # @return [Object] result of yield block def api_action!(*args) type = self.class.name.split('::').last.downcase run_callbacks_for(["before_#{type}", :before], *args) - result = yield if block_given? - run_callbacks_for(["after_#{type}", :after], *args) - result + result = nil + begin + result = yield if block_given? + run_callbacks_for(["after_#{type}", :after], *args) + result + rescue => err + run_callbacks_for(["failed_#{type}", :failed], *(args + [err])) + raise + end end # Process requested callbacks # # @param type [Symbol, String] name of callback type @@ -32,11 +37,11 @@ callbacks_for(c_type) end.flatten(1).compact.uniq.each do |item| callback_name, callback, quiet = item quiet = true if config[:print_only] ui.info "Callback #{ui.color(type.to_s, :bold)} #{callback_name}: #{ui.color('starting', :yellow)}" unless quiet - if(args.empty?) + if args.empty? callback.call else callback.call(*args) end ui.info "Callback #{ui.color(type.to_s, :bold)} #{callback_name}: #{ui.color('complete', :green)}" unless quiet @@ -55,17 +60,16 @@ begin klass = Sfn::Callback.const_get(Bogo::Utility.camel(c_name.to_s)) klass.new(ui, config, arguments, provider) rescue NameError => e ui.debug "Callback type lookup error: #{e.class} - #{e}" - raise "Unknown #{type} callback requested: #{c_name} (not found)" + raise NameError.new("Unknown #{type} callback requested: #{c_name} (not found)") end end - if(instance.respond_to?(type)) + if instance.respond_to?(type) [c_name, instance.method(type), instance.respond_to?(:quiet) ? instance.quiet : false] end end.compact end - end end end