Sha256: 6d4bb682abd93342c1365b6bff2efc051befbc9e00a4134bcf04b47434e1fb86

Contents?: true

Size: 1.72 KB

Versions: 17

Compression:

Stored size: 1.72 KB

Contents

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

    # @private
    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 attach_error(e)
        return unless e.response

        if respond_to?("error_message=")
          self.error_message = nil

          if (error = e.response[:body]) && error.is_a?(Hash)
            error = Hashie::Mash.new(error)
            self.error_message = (error.error || error.description)
          end
        end

        self.error = e if respond_to?("error=")
      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)
                attach_error(e)
                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)
            attach_error(e)
            opts[:with].respond_to?(:call) ? opts[:with].call : opts[:with]
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
zendesk_api-0.3.14 lib/zendesk_api/rescue.rb
zendesk_api-0.3.13 lib/zendesk_api/rescue.rb
zendesk_api-0.3.12 lib/zendesk_api/rescue.rb
zendesk_api-0.3.11 lib/zendesk_api/rescue.rb
zendesk_api-0.3.10 lib/zendesk_api/rescue.rb
zendesk_api-0.3.9 lib/zendesk_api/rescue.rb
zendesk_api-0.3.8 lib/zendesk_api/rescue.rb
zendesk_api-0.3.7 lib/zendesk_api/rescue.rb
zendesk_api-0.3.6 lib/zendesk_api/rescue.rb
zendesk_api-0.3.5 lib/zendesk_api/rescue.rb
zendesk_api-0.3.4 lib/zendesk_api/rescue.rb
zendesk_api-0.3.3 lib/zendesk_api/rescue.rb
zendesk_api-0.3.2 lib/zendesk_api/rescue.rb
zendesk_api-0.3.1 lib/zendesk_api/rescue.rb
zendesk_api-0.3.0 lib/zendesk_api/rescue.rb
zendesk_api-0.2.6 lib/zendesk_api/rescue.rb
zendesk_api-0.2.5 lib/zendesk_api/rescue.rb