Skip to content

Commit 67a3ba6

Browse files
committed
Respect Faraday::Response::RaiseError::DEFAULT_OPTIONS when raising errors.
1 parent 09f1fc3 commit 67a3ba6

File tree

7 files changed

+83
-214
lines changed

7 files changed

+83
-214
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-02-02 03:09:26 UTC using RuboCop version 1.68.0.
3+
# on 2025-10-16 14:00:36 UTC using RuboCop version 1.68.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

99
# Offense count: 1
10-
# Configuration parameters: AllowedPatterns.
11-
# AllowedPatterns: (?-mix:(exactly|at_least|at_most)\(\d+\)\.times)
12-
Lint/UnreachableLoop:
10+
RSpec/AnyInstance:
1311
Exclude:
14-
- 'bin/strava-activities.rb'
12+
- 'spec/strava/api/client_spec.rb'
1513

1614
# Offense count: 13
1715
# This cop supports unsafe autocorrection (--autocorrect-all).
@@ -23,11 +21,6 @@ RSpec/DescribedClass:
2321
- 'spec/strava/oauth/client_spec.rb'
2422
- 'spec/strava/webhooks/client_spec.rb'
2523

26-
# Offense count: 1
27-
RSpec/MultipleDescribes:
28-
Exclude:
29-
- 'spec/strava/api/client/endpoints/activities/activity_spec.rb'
30-
3124
# Offense count: 5
3225
# Configuration parameters: AllowedGroups.
3326
RSpec/NestedGroups:

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ source 'http://rubygems.org'
55
gemspec
66

77
group :development, :test do
8+
gem 'csv'
89
gem 'dotenv'
910
gem 'faraday-retry'
1011
gem 'gpx'

lib/strava/errors/ratelimit_error.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,23 @@
33
module Strava
44
module Errors
55
class RatelimitError < ::Faraday::ClientError
6-
attr_reader :http_response, :ratelimit, :error_message
6+
attr_reader :ratelimit
77

8-
def initialize(http_response, error_message = nil)
9-
@response = http_response.response
10-
@ratelimit = Strava::Api::Ratelimit.new(@response)
11-
@error_message = error_message || message
12-
super({
13-
status: http_response.status,
14-
headers: http_response.response_headers,
15-
body: http_response.body
16-
})
8+
def initialize(env, response)
9+
@ratelimit = Strava::Api::Ratelimit.new(env.response)
10+
super(response)
1711
end
1812

1913
def message
20-
response[:body]['message'] || super
14+
response.body['message'] || super
2115
end
2216

2317
def headers
24-
response[:headers]
18+
response.headers
2519
end
2620

2721
def errors
28-
response[:body]['errors']
22+
response.body['errors']
2923
end
3024
end
3125
end

lib/strava/web/raise_response_error.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Strava
44
module Web
55
class RaiseResponseError < ::Faraday::Middleware
6+
DEFAULT_OPTIONS = Faraday::Response::RaiseError::DEFAULT_OPTIONS
67
CLIENT_ERROR_STATUSES = (400...600)
78

89
def on_complete(env)
@@ -13,18 +14,38 @@ def on_complete(env)
1314
# mimic the behavior that we get with proxy requests with HTTPS
1415
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
1516
when 429
16-
raise Strava::Errors::RatelimitError.new(env, 'Too Many Requests')
17+
raise Strava::Errors::RatelimitError.new(env, response_values(env))
1718
when CLIENT_ERROR_STATUSES
1819
raise Strava::Errors::Fault, response_values(env)
1920
end
2021
end
2122

2223
def response_values(env)
23-
{
24+
response = {
2425
status: env.status,
2526
headers: env.response_headers,
2627
body: env.body
2728
}
29+
30+
# Include the request data by default. If the middleware was explicitly
31+
# configured to _not_ include request data, then omit it.
32+
return response unless options[:include_request]
33+
34+
response.merge(
35+
request: {
36+
method: env.method,
37+
url: env.url,
38+
url_path: env.url.path,
39+
params: query_params(env),
40+
headers: env.request_headers,
41+
body: env.request_body
42+
}
43+
)
44+
end
45+
46+
def query_params(env)
47+
env.request.params_encoder ||= Faraday::Utils.default_params_encoder
48+
env.params_encoder.decode(env.url.query)
2849
end
2950
end
3051
end

0 commit comments

Comments
 (0)