lhc 6.2.0 → 6.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -1
- data/lib/lhc/request.rb +7 -2
- data/lib/lhc/response.rb +2 -2
- data/lib/lhc/version.rb +1 -1
- data/spec/request/ignore_errors_spec.rb +13 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 503f2e88095de04d6f071fd2285459f106ff2f7d
|
4
|
+
data.tar.gz: 1e7c8bd5c1878d11ce293208c43f7886714891bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e8fbabecb039bfe29377cdd7ee30f52aea980d459918953f5df9b1e92b48c9e15ce15392122dc1dabb9934ca3fc8ac5fb44d2c6f64a1545996bebe24ca6d12e
|
7
|
+
data.tar.gz: a21700e4b7979715066c2659bc492fee013dee54a0471427f6c6dac414c3ff94855bb1b289f3c6900d36714e7200b105e52ec1f46067aeb16909d6a837458d05
|
data/README.md
CHANGED
@@ -139,7 +139,7 @@ Anything but a response code indicating success (2**) throws an exception.
|
|
139
139
|
|
140
140
|
→ [Read more about exceptions](docs/exceptions.md)
|
141
141
|
|
142
|
-
|
142
|
+
### Custom error handling
|
143
143
|
|
144
144
|
You can provide custom error handlers to handle errors happening during the request.
|
145
145
|
|
@@ -153,6 +153,20 @@ response = LHC.get('http://something', error_handler: handler)
|
|
153
153
|
response.data.name # 'unknown'
|
154
154
|
```
|
155
155
|
|
156
|
+
### Ignore certain errors
|
157
|
+
|
158
|
+
As it's discouraged to rescue errors and then don't handle them (ruby styleguide),
|
159
|
+
but you often want to continue working with `nil`, LHC provides the `ignored_errors` option.
|
160
|
+
|
161
|
+
Errors listed in this option will not be raised and will leave the `response.body` and `response.data` to stay `nil`.
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
response = LHC.get('http://something', ignored_errors: [LHC::NotFound])
|
165
|
+
|
166
|
+
response.body # nil
|
167
|
+
response.data # nil
|
168
|
+
```
|
169
|
+
|
156
170
|
## Interceptors
|
157
171
|
|
158
172
|
To monitor and manipulate the http communication done with LHC, you can define interceptors.
|
data/lib/lhc/request.rb
CHANGED
@@ -8,9 +8,10 @@ class LHC::Request
|
|
8
8
|
|
9
9
|
TYPHOEUS_OPTIONS ||= [:params, :method, :body, :headers, :follow_location]
|
10
10
|
|
11
|
-
attr_accessor :response, :options, :raw, :format, :error_handler
|
11
|
+
attr_accessor :response, :options, :raw, :format, :error_handler, :errors_ignored
|
12
12
|
|
13
13
|
def initialize(options, self_executing = true)
|
14
|
+
self.errors_ignored = options.fetch(:ignored_errors, [])
|
14
15
|
self.options = options.deep_dup || {}
|
15
16
|
self.error_handler = options.delete :error_handler
|
16
17
|
use_configured_endpoint!
|
@@ -101,12 +102,16 @@ class LHC::Request
|
|
101
102
|
end
|
102
103
|
|
103
104
|
def handle_error(response)
|
105
|
+
return if errors_ignored.include?(error)
|
104
106
|
throw_error(response) unless error_handler
|
105
107
|
response.body_replacement = error_handler.call(response)
|
106
108
|
end
|
107
109
|
|
110
|
+
def error
|
111
|
+
@error ||= LHC::Error.find(response)
|
112
|
+
end
|
113
|
+
|
108
114
|
def throw_error(response)
|
109
|
-
error = LHC::Error.find(response)
|
110
115
|
fail error.new(error, response)
|
111
116
|
end
|
112
117
|
end
|
data/lib/lhc/response.rb
CHANGED
@@ -20,7 +20,7 @@ class LHC::Response
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def data
|
23
|
-
@data ||= LHC::Response::Data.new(self)
|
23
|
+
@data ||= body.present? ? LHC::Response::Data.new(self) : nil
|
24
24
|
end
|
25
25
|
|
26
26
|
def [](key)
|
@@ -28,7 +28,7 @@ class LHC::Response
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def body
|
31
|
-
body_replacement || raw.body
|
31
|
+
body_replacement || raw.body.presence
|
32
32
|
end
|
33
33
|
|
34
34
|
# Provides response time in ms.
|
data/lib/lhc/version.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
describe LHC::Request do
|
4
|
+
context 'ignore errors' do
|
5
|
+
it 'raises errors for anything but 2XX response codes' do
|
6
|
+
stub_request(:get, 'http://local.ch').to_return(status: 404)
|
7
|
+
response = LHC.get('http://local.ch', ignored_errors: [LHC::NotFound])
|
8
|
+
expect(response.body).to eq nil
|
9
|
+
expect(response.data).to eq nil
|
10
|
+
expect(response.success?).to eq false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.
|
4
|
+
version: 6.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhc/contributors
|
@@ -278,6 +278,7 @@ files:
|
|
278
278
|
- spec/request/encoding_spec.rb
|
279
279
|
- spec/request/error_handling_spec.rb
|
280
280
|
- spec/request/headers_spec.rb
|
281
|
+
- spec/request/ignore_errors_spec.rb
|
281
282
|
- spec/request/option_dup_spec.rb
|
282
283
|
- spec/request/parallel_requests_spec.rb
|
283
284
|
- spec/request/request_without_rails_spec.rb
|
@@ -320,7 +321,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
320
321
|
requirements:
|
321
322
|
- Ruby >= 2.0.0
|
322
323
|
rubyforge_project:
|
323
|
-
rubygems_version: 2.6.
|
324
|
+
rubygems_version: 2.6.12
|
324
325
|
signing_key:
|
325
326
|
specification_version: 4
|
326
327
|
summary: LocalHttpClient
|
@@ -405,6 +406,7 @@ test_files:
|
|
405
406
|
- spec/request/encoding_spec.rb
|
406
407
|
- spec/request/error_handling_spec.rb
|
407
408
|
- spec/request/headers_spec.rb
|
409
|
+
- spec/request/ignore_errors_spec.rb
|
408
410
|
- spec/request/option_dup_spec.rb
|
409
411
|
- spec/request/parallel_requests_spec.rb
|
410
412
|
- spec/request/request_without_rails_spec.rb
|