lib/zendesk_api/rescue.rb in zendesk_api-0.2.2 vs lib/zendesk_api/rescue.rb in zendesk_api-0.2.4

- old
+ new

@@ -15,10 +15,23 @@ 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 error = e.response[:body] + if error.is_a?(Hash) + error = Hashie::Mash.new(error) + self.error_message = (error.error || error.description) if respond_to?("error_message=") + end + + self.error = error if respond_to?("error=") + end + end + def rescue_client_error(*args) opts = args.last.is_a?(Hash) ? args.pop : {} if args.any? args.each do |method| @@ -26,18 +39,20 @@ 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