Class: Strava::Web::RaiseResponseError

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/strava/web/raise_response_error.rb

Constant Summary collapse

DEFAULT_OPTIONS =
Faraday::Response::RaiseError::DEFAULT_OPTIONS
CLIENT_ERROR_STATUSES =
(400...600)

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/strava/web/raise_response_error.rb', line 9

def on_complete(env)
  case env[:status]
  when 404
    raise Faraday::ResourceNotFound, response_values(env)
  when 407
    # mimic the behavior that we get with proxy requests with HTTPS
    raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
  when 429
    raise Strava::Errors::RatelimitError.new(env, response_values(env))
  when CLIENT_ERROR_STATUSES
    raise Strava::Errors::Fault, response_values(env)
  end
end

#query_params(env) ⇒ Object



46
47
48
49
# File 'lib/strava/web/raise_response_error.rb', line 46

def query_params(env)
  env.request.params_encoder ||= Faraday::Utils.default_params_encoder
  env.params_encoder.decode(env.url.query)
end

#response_values(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/strava/web/raise_response_error.rb', line 23

def response_values(env)
  response = {
    status: env.status,
    headers: env.response_headers,
    body: env.body
  }

  # Include the request data by default. If the middleware was explicitly
  # configured to _not_ include request data, then omit it.
  return response unless options[:include_request]

  response.merge(
    request: {
      method: env.method,
      url: env.url,
      url_path: env.url.path,
      params: query_params(env),
      headers: env.request_headers,
      body: env.request_body
    }
  )
end