Class: Strava::Web::RaiseResponseError
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Strava::Web::RaiseResponseError
- 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.
Constant Summary collapse
- DEFAULT_OPTIONS =
Returns 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).
(400...600)
Instance Method Summary collapse
-
#on_complete(env) ⇒ void
Callback invoked when an HTTP response is complete.
-
#query_params(env) ⇒ Hash
Extracts and decodes query parameters from the request URL.
-
#response_values(env) ⇒ Hash
Extracts response values from the Faraday environment.
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)
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.
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.
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 [: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 |