Sha256: ed11eb4f669526b6c87bdb7e983d51a5496d3c2c21388b3ebe0dc869a4a32c2b

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 KB

Contents

module ZendeskAPI
  module Rescue
    def self.included(klass)
      klass.extend(Methods)
      klass.send(:include, Methods)
    end

    module Methods
      def log_error(e, method = false)
        if logger = (@client ? @client.config.logger : Kernel)
          logger.warn "#{self} - #{method}" if method
          logger.warn e.message
          logger.warn e.backtrace.join("\n")
          logger.warn "\t#{e.response[:body].inspect}" if e.response
        end
      end

      def rescue_client_error(*args)
        opts = args.last.is_a?(Hash) ? args.pop : {}

        if args.any?
          args.each do |method|
            class_eval("alias :orig_#{method} :#{method}")
            define_method method do |*args|
              begin
                send("orig_#{method}", *args)
              rescue Faraday::Error::ClientError => e
                log_error(e, method)
                opts[:with].respond_to?(:call) ? opts[:with].call : opts[:with]
              end
            end
          end
        elsif block_given?
          begin
            yield
          rescue Faraday::Error::ClientError => e
            log_error(e)
            opts[:with].respond_to?(:call) ? opts[:with].call : opts[:with]
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
zendesk_api-0.1.7 lib/zendesk_api/rescue.rb
zendesk_api-0.1.6 lib/zendesk_api/rescue.rb
zendesk_api-0.1.5 lib/zendesk_api/rescue.rb
zendesk_api-0.1.4 lib/zendesk_api/rescue.rb
zendesk_api-0.1.3 lib/zendesk_api/rescue.rb
zendesk_api-0.1.2 lib/zendesk_api/rescue.rb
zendesk_api-0.1.1 lib/zendesk_api/rescue.rb
zendesk_api-0.0.9 lib/zendesk_api/rescue.rb