Class: Faraday::ExtendedParseJson

Inherits:
FaradayMiddleware::ParseJson
  • Object
show all
Defined in:
lib/faraday/extended_parse_json.rb

Instance Method Summary collapse

Instance Method Details

#exception(env) ⇒ Object



33
34
35
36
# File 'lib/faraday/extended_parse_json.rb', line 33

def exception env
  # decide to retry or not
  (env[:retries].to_i.zero? ? LC::LCProtocolError : LC::LCProtocolRetry)
end

#process_response(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/faraday/extended_parse_json.rb', line 6

def process_response(env)
  env[:raw_body] = env[:body] if preserve_raw?(env)


  if env[:status] >= 400
    data = parse(env[:body]) || {} rescue {}

    array_codes = [
      LC::Protocol::ERROR_INTERNAL,
      LC::Protocol::ERROR_TIMEOUT,
      LC::Protocol::ERROR_EXCEEDED_BURST_LIMIT
    ]
    error_hash = { "error" => "HTTP Status #{env[:status]} Body #{env[:body]}", "http_status_code" => env[:status] }.merge(data)
    if data['code'] && array_codes.include?(data['code'])
      sleep 60 if data['code'] == LC::Protocol::ERROR_EXCEEDED_BURST_LIMIT
      raise exception(env).new(error_hash.merge(data))
    elsif env[:status] >= 500
      raise exception(env).new(error_hash.merge(data))
    end
    raise LC::LCProtocolError.new(error_hash)
  else
    data = parse(env[:body]) || {}

    env[:body] = data
  end
end