Skip to content

Commit 3dccaf1

Browse files
authored
Merge pull request #91 from dblock/raise-error
Respect Faraday::Response::RaiseError::DEFAULT_OPTIONS when raising errors.
2 parents 09f1fc3 + 7eb759b commit 3dccaf1

File tree

13 files changed

+118
-238
lines changed

13 files changed

+118
-238
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +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:15:33 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'
15-
16-
# Offense count: 13
17-
# This cop supports unsafe autocorrection (--autocorrect-all).
18-
# Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants.
19-
# SupportedStyles: described_class, explicit
20-
RSpec/DescribedClass:
21-
Exclude:
22-
- 'spec/strava/models/athlete_spec.rb'
23-
- 'spec/strava/oauth/client_spec.rb'
24-
- 'spec/strava/webhooks/client_spec.rb'
25-
26-
# Offense count: 1
27-
RSpec/MultipleDescribes:
28-
Exclude:
29-
- 'spec/strava/api/client/endpoints/activities/activity_spec.rb'
12+
- 'spec/strava/api/client_spec.rb'
3013

3114
# Offense count: 5
3215
# Configuration parameters: AllowedGroups.

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
### 2.3.0 (Next)
22

3+
* [#91](https://github.com/dblock/strava-ruby-client/pull/91): Respects `Faraday::Response::RaiseError::DEFAULT_OPTIONS` when raising errors - [@dblock](https://github.com/dblock).
34
* [#87](https://github.com/dblock/strava-ruby-client/pull/87): Prepares v2.3.0 by improving the event specs - [@simonneutert](https://github.com/simonneutert).
4-
* [#86](https://github.com/dblock/strava-ruby-client/pull/86): Add description to club event model - [@tobiaszwaszak](https://github.com/tobiaszwaszak).
5+
* [#86](https://github.com/dblock/strava-ruby-client/pull/86): Adds description to club event model - [@tobiaszwaszak](https://github.com/tobiaszwaszak).
56
* Your contribution here.
67

78
### 2.2.0 (2024/9/17)

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'

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,12 @@ rescue Strava::Errors::Fault => e
11221122
end
11231123
```
11241124

1125+
Faraday can optionally exclude HTTP method, path and query params from the errors raised. The client implementation options will now default to `Faraday::Response::RaiseError::DEFAULT_OPTIONS` with `include_request` set to `true`. You can change this behavior by setting `Strava::Web::RaiseResponseError::DEFAULT_OPTIONS`.
1126+
1127+
```ruby
1128+
Strava::Web::RaiseResponseError::DEFAULT_OPTIONS = { include_request: false }
1129+
```
1130+
11251131
## Tools
11261132

11271133
For a complete set of command-line tools, check out [strava-ruby-cli](https://github.com/dblock/strava-ruby-cli) built on top of this gem.

UPGRADING.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Upgrading Strava-Ruby-Client
22

3+
### Upgrading to >= 2.3.0
4+
5+
Faraday can optionally exclude HTTP method, path and query params from the errors raised. The client implementation options will now default to `Faraday::Response::RaiseError::DEFAULT_OPTIONS` with `include_request` set to `true`. You can change this behavior by setting `Strava::Web::RaiseResponseError::DEFAULT_OPTIONS`.
6+
7+
```ruby
8+
Strava::Web::RaiseResponseError::DEFAULT_OPTIONS = { include_request: false }
9+
```
10+
11+
See [#91](https://github.com/dblock/strava-ruby-client/pull/91) for details.
12+
313
### Upgrading to >= 2.2.0
414

515
Support for Ruby 2.x has been dropped. The minimum required Ruby version is now 3.0.0.
@@ -45,4 +55,4 @@ client = Strava::Api::Client.new(ca_file: OpenSSL::X509::DEFAULT_CERT_FILE, ca_p
4555

4656
The library was upgraded to require Faraday 1.0 or newer. Change all references to `Faraday::Error::ResourceNotFound` or `Faraday::Error::ConnectionFailed` to `Faraday::ConnectionFailed` and `Faraday::ResourceNotFound` respectively.
4757

48-
See [#30](https://github.com/dblock/strava-ruby-client/pull/30) for more details.
58+
See [#30](https://github.com/dblock/strava-ruby-client/pull/30) for details.

lib/strava/errors/ratelimit_error.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
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

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)