fetch 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f2e34b25ea8efe2d5681328da56ba8d5141775b
4
- data.tar.gz: 582f71ff59b4e30b02733a5f777096e4ac785aad
3
+ metadata.gz: 2f7ab6433f70dc724bd8e7640a072ee3ca26555c
4
+ data.tar.gz: f25919595f00945f1bb6b1b41e40ca1940ac2421
5
5
  SHA512:
6
- metadata.gz: 750a2cbaca8debe5512947da70ae5c01a35eb72e71873390013f059ce9694d7777fc3978d224b4292c7039900673c171842438df1c3c15524f584e64f03b3bd3
7
- data.tar.gz: 1da726ec7708099401e988d66702f37d4b49ba90b0d80be9dcbdfe667808d49f16e1e37b361a9b04c890b9eb910b4c9e7512269325db9b25ba648d5d60957439
6
+ metadata.gz: cf303622302ebf70d0f071fc14d99f55dc8b748aae0789397c4afc09c76c1eb5a1c2c60332d28e742f6f9c504ff240d79415dc7122432c3f11d70b4497653e06
7
+ data.tar.gz: 41b866ef62c1d97f23b2f634b384898023aee0ee61f3caac231d4d11971e34b91b558b812bdf5564018a5dc7b8df75e4a2ab74cd741fc1a8701e9e34a1164c64
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 0.0.3
4
+
5
+ * Adds an general `error` callback to `Fetch::Base` for catching any unhandled
6
+ errors that might occur.
7
+
3
8
  ## Version 0.0.2
4
9
 
5
10
  * Sends fetchable to fetch modules by default.
data/README.md CHANGED
@@ -177,7 +177,7 @@ callback isn't called.
177
177
  **Note:** If you don't specify a `failure` callback at all, HTTP failures are ignored,
178
178
  and processing skipped for the failed request.
179
179
 
180
- ### Handling errors
180
+ ### Handling fetch errors
181
181
 
182
182
  Sometimes a URL will return something that potentially makes your processing
183
183
  code fail. To prevent this from breaking your whole fetch, you can handle
@@ -223,6 +223,24 @@ isn't run.
223
223
  above, any exceptions that occur when processing will be raised, causing the
224
224
  whole fetch to fail. So please add error handling :blush:
225
225
 
226
+ ### General error handling
227
+
228
+ You add a "catch all" `error` callback to your fetcher subclassed from
229
+ `Fetch::Base`. This enables you to handle any unhandled errors, including errors
230
+ in your fetcher callbacks.
231
+
232
+ ```ruby
233
+ class UserFetcher < Fetch::Base
234
+ modules Facebook::UserInfoFetch,
235
+ Github::UserInfoFetch
236
+
237
+ error do |e|
238
+ # Do something that must be done,
239
+ # even if the fetch fails.
240
+ end
241
+ end
242
+ ```
243
+
226
244
  ### Parsing JSON
227
245
 
228
246
  Fetch has a module for automatically parsing the request body as JSON before
@@ -21,7 +21,8 @@ module Fetch
21
21
  :init,
22
22
  :before_fetch,
23
23
  :after_fetch,
24
- :progress
24
+ :progress,
25
+ :error
25
26
 
26
27
  def initialize(fetchable = nil)
27
28
  @fetchable = fetchable
@@ -45,6 +46,10 @@ module Fetch
45
46
  after_fetch
46
47
 
47
48
  true
49
+ rescue => e
50
+ raise e unless callback?(:error)
51
+ error(e)
52
+ false
48
53
  end
49
54
 
50
55
  private
@@ -1,3 +1,3 @@
1
1
  module Fetch
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,84 @@
1
+ require "test_helper"
2
+
3
+ class ErrorTest < Minitest::Test
4
+ def test_error_callback_gets_run_for_unhandled_fetch_module_errors
5
+ actions = []
6
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
7
+ mod = Class.new(Fetch::Module) do
8
+ request do |req|
9
+ req.url = "http://test.com/one"
10
+ req.process do |body|
11
+ this_wont_work!
12
+ end
13
+ end
14
+ end
15
+ klass = Class.new(MockFetcher(mod)) do
16
+ error do |e|
17
+ actions << "got #{e.message}"
18
+ end
19
+ end
20
+ klass.new.fetch
21
+ assert_match /got undefined method `this_wont_work!'/, actions.first
22
+ end
23
+
24
+ def test_exception_is_raised_if_no_error_callback_specified
25
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
26
+ mod = Class.new(Fetch::Module) do
27
+ request do |req|
28
+ req.url = "http://test.com/one"
29
+ req.process do |body|
30
+ this_wont_work!
31
+ end
32
+ end
33
+ end
34
+ assert_raises NoMethodError do
35
+ MockFetcher(mod).new.fetch
36
+ end
37
+ end
38
+
39
+ [:modules, :load, :init, :before_fetch, :after_fetch, :progress].each do |callback|
40
+ define_method "test_error_gets_run_when_#{callback}_callback_fails" do
41
+ actions = []
42
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
43
+ mod = Class.new(Fetch::Module) do
44
+ request do |req|
45
+ req.url = "http://test.com/one"
46
+ end
47
+ end
48
+ klass = Class.new(MockFetcher(mod)) do
49
+ send(callback) do
50
+ send "this_#{callback}_fails!"
51
+ end
52
+
53
+ error do |e|
54
+ actions << "got #{e.message}"
55
+ end
56
+ end
57
+ klass.new.fetch
58
+ assert_match /got undefined method `this_#{callback}_fails!'/, actions.first
59
+ end
60
+ end
61
+
62
+ def test_error_callback_doesnt_run_for_handled_fetch_module_errors
63
+ actions = []
64
+ stub_request(:get, "http://test.com/one").to_return(body: "got one")
65
+ mod = Class.new(Fetch::Module) do
66
+ request do |req|
67
+ req.url = "http://test.com/one"
68
+ req.process do |body|
69
+ this_wont_work!
70
+ end
71
+ end
72
+
73
+ error do |e|
74
+ end
75
+ end
76
+ klass = Class.new(MockFetcher(mod)) do
77
+ error do |e|
78
+ actions << "got #{e.message}"
79
+ end
80
+ end
81
+ klass.new.fetch
82
+ assert_equal [], actions
83
+ end
84
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lasse Bunk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-04 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -110,6 +110,7 @@ files:
110
110
  - lib/fetch/version.rb
111
111
  - test/callbacks_test.rb
112
112
  - test/defaults_test.rb
113
+ - test/error_test.rb
113
114
  - test/fetch_test.rb
114
115
  - test/init_test.rb
115
116
  - test/json_test.rb
@@ -144,6 +145,7 @@ summary: Coming
144
145
  test_files:
145
146
  - test/callbacks_test.rb
146
147
  - test/defaults_test.rb
148
+ - test/error_test.rb
147
149
  - test/fetch_test.rb
148
150
  - test/init_test.rb
149
151
  - test/json_test.rb