Class: Faraday::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/better-faraday.rb

Instance Method Summary collapse

Instance Method Details

#assert_2xx!Object Also known as: ok!, assert_success!



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/better-faraday.rb', line 28

def assert_2xx!
  return self if status >= 200 && status <= 299
  klass = if status == 422
    Faraday::HTTP422
  elsif status >= 400 && status <= 499
    Faraday::HTTP4xx
  else
    Faraday::Error
  end
  error = klass.new(describe)
  error.instance_variable_set(:@response, self)
  raise error
end

#describeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/better-faraday.rb', line 45

def describe
  request_headers  = env.request_headers.deep_dup
  request_json     = parse_json(env.request_body)
  response_headers = ::Hash === env.response_headers ? env.response_headers.deep_dup : {}
  response_json    = parse_json(env.body)
  [request_headers, request_json, response_headers, response_json].compact.each(&method(:protect!))

  [ "",
    "-- #{status} #{reason_phrase} --",
    "",
    "-- Request URL --",
    env.url.to_s,
    "",
    "-- Request method --",
    env.method.to_s.upcase,
    "",
    "-- Request headers --",
    ::JSON.generate(request_headers).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
    "",
    "-- Request body --",
    (request_json ? ::JSON.generate(request_json) : env.request_body.to_s).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
    "",
    "-- Response headers --",
    (response_headers ? ::JSON.generate(response_headers) : env.response_headers.to_s).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
    "",
    "-- Response body --",
    (response_json ? ::JSON.generate(response_json) : env.body.to_s).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
    ""
  ].join("\n")
end