Class: Faraday::Response
- Inherits:
-
Object
- Object
- Faraday::Response
- Defined in:
- lib/better-faraday.rb
Instance Method Summary collapse
- #assert_2xx! ⇒ Object (also: #ok!, #assert_success!)
- #describe ⇒ Object
Instance Method Details
#assert_2xx! ⇒ Object Also known as: ok!, assert_success!
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/better-faraday.rb', line 27 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 |
#describe ⇒ Object
44 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 |
# File 'lib/better-faraday.rb', line 44 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 { |hash| sanitize(hash) } [ "", "-- #{status} #{reason_phrase} --", "", "-- Request URL --", env.url.to_s, "", "-- Request method --", env.method.to_s.upcase, "", "-- Request headers --", ::JSON.generate(request_headers), "", "-- Request body --", request_json ? ::JSON.generate(request_json) : env.request_body.to_s, "", "-- Response headers --", response_headers ? ::JSON.generate(response_headers) : env.response_headers.to_s, "", "-- Response body --", response_json ? ::JSON.generate(response_json) : env.body.to_s, "" ].join("\n") end |