timeout_errors 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9faa0296a894770ca85ab6d03668ee24dfba3bd8
4
+ data.tar.gz: 6365ff6f93710aa36e9f1131fa3ab50001099ac9
5
+ SHA512:
6
+ metadata.gz: e35695f4457163c8980b50d637181639789580529d7635a7440ad24bce6630a2c329741ed7a0d28b7831fb9d3565a85dc9b4ae41061c6a905348e8ac783a9216
7
+ data.tar.gz: c3d03500061d5d84645c709e93ef69dbb0173c027fca5291fcd8499066e28e505eddb4b9a50544b340439bf7a87f1682eb686a3777cf6942da9e31804f758a69
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in timeout_errors.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Genadi Samokovarov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,104 @@
1
+ # Timeout Errors
2
+
3
+ Catch all of them Net::HTTP timeout errors. Why? Because there are lots of
4
+ them.
5
+
6
+ ## Install
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'timeout_errors'
12
+ ```
13
+
14
+ ## Play
15
+
16
+ Let's playing a game. You do a request. It times out. You try to guess which
17
+ error it timed out with. Here's how you win every time.
18
+
19
+ ```ruby
20
+ require 'uri'
21
+ require 'timeout_errors'
22
+
23
+ begin
24
+ uri = URI.parse("http://imsuresomeonewillregisterthatjusttotroll.me/")
25
+ Net::HTTP.get_response(uri)
26
+ rescue TimeoutErrors => e
27
+ puts "And the winner is: #{e.inspect}"
28
+ end
29
+ ```
30
+
31
+ Now, imagine your favourite 3rd party HTTP client library, that uses `net/http`
32
+ under the hook invents yet another one of those pesky timeout errors. If you
33
+ play the game with that specific 3rd party library, you are quite likely to
34
+ loose. Here's how you can improve your chances:
35
+
36
+ ```ruby
37
+ require 'best_http_client_ever_because_we_need_yet_another_one'
38
+ require 'timeout_errors'
39
+
40
+ TimeoutErrors.include_error(
41
+ BestHttpClientEverBecauseWeNeedYetAnotherOne::YetAnotherTimeoutToSolveItAllError
42
+ )
43
+
44
+ begin
45
+ BestHttpClientEverBecauseWeNeedYetAnotherOne.get(
46
+ "http://imsuresomeonewillregisterthatjusttotroll.me/"
47
+ )
48
+ rescue TimeoutErrors => e
49
+ puts "Your favourite timeout error is: #{e.inspect}"
50
+ end
51
+ ```
52
+
53
+ Quite useful, heh?
54
+
55
+ ## Winners
56
+
57
+ Here's the list of errors caught by `TimeoutErrors` by default.
58
+
59
+ - `EOFError`
60
+ - `Errno::ECONNREFUSED`
61
+ - `Errno::ECONNRESET`
62
+ - `Errno::EHOSTUNREACH`
63
+ - `Errno::EINVAL`
64
+ - `Errno::ENETUNREACH`
65
+ - `Errno::EPIPE`
66
+ - `Errno::ETIMEDOUT`
67
+ - `Net::HTTPBadResponse`
68
+ - `Net::HTTPHeaderSyntaxError`
69
+ - `Net::ProtocolError`
70
+ - `SocketError`
71
+ - `Timeout::Error`
72
+
73
+ Not that you can fill this list during runtime with
74
+ `TimeoutErrors.include_error`.
75
+
76
+ ## Why
77
+
78
+ There is a lot of previous art on this topic. Why create another one?
79
+
80
+ ### [net_http_timeout_errors]
81
+
82
+ This library is a great choice. It uses standard Ruby interface and clearly
83
+ lists all the errors. However, I wanna catch them all with a single `rescue
84
+ ErrorMatcher` clause. Which leads us to [net_http_exception_fix].
85
+
86
+ ### [net_http_exception_fix]
87
+
88
+ [net_http_exception_fix] is a hotfix to give those common exceptions that pop
89
+ up from Net:HTTP usage a shared parent exception class. This is the interface I
90
+ like, however it is achieved through polluting builtin' errors ancestor chain.
91
+
92
+ ## How
93
+
94
+ Instead of introducing extra interface, or monkey patching existing errors,
95
+ this library exploits a fun little fact about Ruby's exception handling. Errors
96
+ listed on the `rescue` clause are matched with the case (`===`) operator.
97
+
98
+ ## Credits
99
+
100
+ Thanks to the authors and contributors of [net_http_timeout_errors] and
101
+ [net_http_exception_fix].
102
+
103
+ [net_http_timeout_errors]: https://github.com/barsoom/net_http_timeout_errors
104
+ [net_http_exception_fix]: https://github.com/edward/net_http_exception_fix
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,36 @@
1
+ require "timeout_errors/version"
2
+ require "net/http"
3
+
4
+ module TimeoutErrors
5
+ extend self
6
+
7
+ def include_error(error)
8
+ errors << error
9
+ end
10
+
11
+ def ===(other)
12
+ errors.any? { |error| error === other }
13
+ end
14
+
15
+ private
16
+
17
+ def errors
18
+ @@errors ||= []
19
+ end
20
+ end
21
+
22
+ [
23
+ EOFError,
24
+ Errno::ECONNREFUSED,
25
+ Errno::ECONNRESET,
26
+ Errno::EHOSTUNREACH,
27
+ Errno::EINVAL,
28
+ Errno::ENETUNREACH,
29
+ Errno::EPIPE,
30
+ Errno::ETIMEDOUT,
31
+ Net::HTTPBadResponse,
32
+ Net::HTTPHeaderSyntaxError,
33
+ Net::ProtocolError,
34
+ SocketError,
35
+ Timeout::Error,
36
+ ].each(&TimeoutErrors.method(:include_error))
@@ -0,0 +1,3 @@
1
+ module TimeoutErrors
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,47 @@
1
+ $LOAD_PATH << File.expand_path("../../lib", __FILE__)
2
+
3
+ require "minitest/autorun"
4
+ require "timeout_errors"
5
+
6
+ class TimeoutErrorsTest < MiniTest::Test
7
+ def test_catches_the_default_list_of_errors
8
+ default_errors.each(&method(:assert_catches))
9
+ end
10
+
11
+ def test_includes_custom_errors
12
+ custom_error = Class.new(StandardError)
13
+ TimeoutErrors.include_error(custom_error)
14
+
15
+ assert_catches(custom_error)
16
+ end
17
+
18
+ private
19
+
20
+ def default_errors
21
+ [
22
+ EOFError,
23
+ Errno::ECONNREFUSED,
24
+ Errno::ECONNRESET,
25
+ Errno::EHOSTUNREACH,
26
+ Errno::EINVAL,
27
+ Errno::ENETUNREACH,
28
+ Errno::EPIPE,
29
+ Errno::ETIMEDOUT,
30
+ Net::HTTPBadResponse,
31
+ Net::HTTPHeaderSyntaxError,
32
+ Net::ProtocolError,
33
+ SocketError,
34
+ Timeout::Error,
35
+ ]
36
+ end
37
+
38
+ def assert_catches(error)
39
+ begin
40
+ raise error
41
+ rescue TimeoutErrors
42
+ # Great success!
43
+ else
44
+ flunk "Expected to catch #{error}, but didn't"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH << File.expand_path("../lib", __FILE__)
2
+
3
+ require "timeout_errors/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "timeout_errors"
7
+ spec.version = TimeoutErrors::VERSION
8
+ spec.authors = ["Genadi Samokovarov"]
9
+ spec.email = ["gsamokovarov@gmail.com"]
10
+ spec.summary = %q(Catch all of them Net::HTTP timeout errors)
11
+ spec.homepage = "https://github.com/gsamokovarov/timeout_errors"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.7"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "minitest", "~> 5.0"
22
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timeout_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Genadi Samokovarov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - gsamokovarov@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/timeout_errors.rb
68
+ - lib/timeout_errors/version.rb
69
+ - test/timeout_errors_test.rb
70
+ - timeout_errors.gemspec
71
+ homepage: https://github.com/gsamokovarov/timeout_errors
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Catch all of them Net::HTTP timeout errors
95
+ test_files:
96
+ - test/timeout_errors_test.rb