Class: Strava::Web::RaiseResponseError

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

Overview

Faraday middleware that raises exceptions for HTTP error responses.

This middleware intercepts HTTP responses and raises appropriate exceptions based on the status code. It handles common HTTP error statuses and maps them to specific Strava error classes, including special handling for rate limiting (429) and resource not found (404) responses.

See Also:

Constant Summary collapse

DEFAULT_OPTIONS =

Returns Default options inherited from Faraday's RaiseError middleware.

Returns:

  • (Hash)

    Default options inherited from Faraday's RaiseError middleware

Faraday::Response::RaiseError::DEFAULT_OPTIONS
CLIENT_ERROR_STATUSES =

Returns HTTP status codes that are considered client errors (400-599).

Returns:

  • (Range)

    HTTP status codes that are considered client errors (400-599)

(400...600)

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ void

This method returns an undefined value.

Callback invoked when an HTTP response is complete.

Examines the HTTP status code and raises appropriate exceptions:

  • 404: Faraday::ResourceNotFound
  • 407: Faraday::ConnectionFailed (proxy authentication required)
  • 429: Strava::Errors::RatelimitError (rate limit exceeded)
  • 400-599: Strava::Errors::Fault (general client/server errors)

Parameters:

  • env (Faraday::Env)

    The Faraday request/response environment

Raises:

  • (Faraday::ResourceNotFound)

    When resource is not found (404)

  • (Faraday::ConnectionFailed)

    When proxy authentication fails (407)

  • (Strava::Errors::RatelimitError)

    When rate limit is exceeded (429)

  • (Strava::Errors::Fault)

    For other client/server errors (400-599)



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/strava/web/raise_response_error.rb', line 40

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) ⇒ Hash

Extracts and decodes query parameters from the request URL.

Parameters:

  • env (Faraday::Env)

    The Faraday request/response environment

Returns:

  • (Hash)

    Decoded query parameters



93
94
95
96
# File 'lib/strava/web/raise_response_error.rb', line 93

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

#response_values(env) ⇒ Hash

Extracts response values from the Faraday environment.

Builds a hash containing response information (status, headers, body) and optionally includes request details if the middleware is configured with include_request: true.

Parameters:

  • env (Faraday::Env)

    The Faraday request/response environment

Returns:

  • (Hash)

    Response values including status, headers, body, and optionally request details



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/strava/web/raise_response_error.rb', line 64

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