ae_network_connection_exception 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/README.md +3 -2
- data/ae_network_connection_exception.gemspec +0 -1
- data/lib/ae_network_connection_exception.rb +29 -24
- data/lib/ae_network_connection_exception/version.rb +1 -1
- data/test/test_helper.rb +1 -5
- data/test/unit/ae_network_connection_exception_test.rb +35 -49
- metadata +4 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57008730ed008f3e7650a1e544dbdc05a8baebf5
|
4
|
+
data.tar.gz: dbfe0dc6d6ce931af76f6756de8f1f94aedc16b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 556c1d3da505100cc80436e88f041016dbf8ea4e708215d179c1c2bfcff064b949df50c1f4bd1714de1649bee03e528693a1a6b2f0b16a784e767bed0c3c9ca9
|
7
|
+
data.tar.gz: d5b1f855026a713f9c2cfddd1bf3ef8fe8f277f00ddd6199711830c9260bc933fa12db1b5834eecb1890904ec2b2588004729226756bded90f3a5aa488598967
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -19,12 +19,13 @@ Or install it yourself as:
|
|
19
19
|
$ gem install ae_network_connection_exception
|
20
20
|
|
21
21
|
## Usage
|
22
|
-
|
22
|
+
```
|
23
23
|
AeNetworkConnectionException.try do
|
24
24
|
# your code that will make a network call
|
25
25
|
end
|
26
|
+
```
|
26
27
|
|
27
|
-
|
28
|
+
If your network call failed to establise a network connection, an exception of type ```AeNetworkConnectionException::ConnectionNotEstablished``` will be thrown
|
28
29
|
|
29
30
|
## Contributing
|
30
31
|
|
@@ -4,34 +4,39 @@ require "socket"
|
|
4
4
|
module AeNetworkConnectionException
|
5
5
|
|
6
6
|
class ConnectionNotEstablished < StandardError
|
7
|
-
|
8
|
-
|
9
|
-
def initialize(message = nil, cause = $!)
|
10
|
-
super(message)
|
11
|
-
@cause = cause
|
12
|
-
end
|
7
|
+
end
|
13
8
|
|
14
|
-
|
15
|
-
|
9
|
+
class << self
|
10
|
+
def try
|
11
|
+
yield
|
12
|
+
rescue SocketError, Net::OpenTimeout => e
|
13
|
+
# SocketError happens when we fail to connect to a socket. Common problems here are DNS resolution (i.e. getaddrinfo)
|
14
|
+
# Net::OpenTimeout happens when we are unable to establish an HTTP connection before the open_timeout
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
raise ConnectionNotEstablished
|
17
|
+
rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED => e
|
18
|
+
# Errno::ECONNREFUSED happens when we can not connect to the port
|
19
|
+
# Errno::ETIMEDOUT happens when we timeout durring the tcp handshake
|
20
|
+
# It is important to note, Errno::ETIMEDOUT can also happen after we have established a connection and are waiting for a response.
|
21
|
+
# Because of this, we also check that the sys call that was made is connect(2).
|
22
|
+
# Errno::* Exceptions have the following error message format
|
23
|
+
# "#{Message string} - #{syscall} for "#{host}" port #{port}"
|
24
|
+
|
25
|
+
raise_if_exception_message_matches(e, /connect\(2\)/)
|
26
|
+
rescue Errno::ECONNRESET => e
|
27
|
+
# Errno::ECONNRESET happens when the connection is reset. This can happen during ssl negotiation
|
28
|
+
|
29
|
+
raise_if_exception_message_matches(e, /SSL_connect/)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def raise_if_exception_message_matches(exception, pattern)
|
35
|
+
if exception.message =~ pattern
|
36
|
+
raise ConnectionNotEstablished
|
20
37
|
else
|
21
|
-
|
38
|
+
raise exception
|
22
39
|
end
|
23
40
|
end
|
24
41
|
end
|
25
|
-
|
26
|
-
def self.try
|
27
|
-
yield
|
28
|
-
rescue SocketError => e
|
29
|
-
raise ConnectionNotEstablished
|
30
|
-
rescue SystemCallError => e
|
31
|
-
if ([Errno::ETIMEDOUT, Errno::ECONNREFUSED].include? e.class) && (e.message =~ /connect\(2\)/)
|
32
|
-
raise ConnectionNotEstablished
|
33
|
-
else
|
34
|
-
raise e
|
35
|
-
end
|
36
|
-
end
|
37
42
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,8 +1,4 @@
|
|
1
|
-
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
-
$LOAD_PATH << File.join(File.dirname(__FILE__), '..')
|
3
|
-
$LOAD_PATH << File.join(File.dirname(__FILE__))
|
4
|
-
|
5
1
|
require 'rubygems'
|
6
|
-
require '
|
2
|
+
require 'minitest/autorun'
|
7
3
|
require 'ae_network_connection_exception'
|
8
4
|
|
@@ -1,31 +1,30 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
module AeNetworkConnectionException
|
4
|
-
class AeNetworkConnectionExceptionTest < Test
|
4
|
+
class AeNetworkConnectionExceptionTest < Minitest::Test
|
5
5
|
def test_connection_not_established_exception
|
6
|
+
# Exception Causes are standard with ruby 2.1
|
7
|
+
# http://devblog.avdi.org/2013/12/25/exception-causes-in-ruby-2-1
|
8
|
+
|
6
9
|
parent_exception = AeNetworkConnectionException::ConnectionNotEstablished.new("Parent Message")
|
7
10
|
|
8
11
|
assert_equal nil, parent_exception.cause
|
9
12
|
assert_equal "Parent Message", parent_exception.message
|
10
13
|
|
11
|
-
child_exception = StandardError.new("Child Message")
|
12
|
-
parent_exception = AeNetworkConnectionException::ConnectionNotEstablished.new("Parent Message", child_exception)
|
13
|
-
|
14
|
-
assert_equal child_exception, parent_exception.cause
|
15
|
-
assert_equal "Parent Message, cause => StandardError: Child Message", parent_exception.message
|
16
|
-
|
17
|
-
child_exception = nil
|
18
|
-
parent_exception = nil
|
19
14
|
begin
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
15
|
+
child_exception = nil
|
16
|
+
begin
|
17
|
+
raise StandardError.new("New Child Message")
|
18
|
+
rescue => e
|
19
|
+
child_exception = e
|
20
|
+
parent_exception = AeNetworkConnectionException::ConnectionNotEstablished.new("New Parent Message")
|
21
|
+
end
|
22
|
+
rescue AeNetworkConnectionException::ConnectionNotEstablished => parent_exception
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
refute_nil child_exception
|
25
|
+
assert_equal child_exception, parent_exception.cause
|
26
|
+
assert_equal "New Parent Message, cause => StandardError: New Child Message", parent_exception.message
|
27
|
+
end
|
29
28
|
end
|
30
29
|
|
31
30
|
def test_ae_network_connection_exception_try__doesnt_catch_non_network_exceptions
|
@@ -39,50 +38,37 @@ module AeNetworkConnectionException
|
|
39
38
|
end
|
40
39
|
|
41
40
|
def test_ae_network_connection_exception_try__raises_connection_not_establised_exception
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
assert_equal AeNetworkConnectionException::ConnectionNotEstablished, e.class
|
48
|
-
assert_equal SocketError, e.cause.class
|
49
|
-
|
50
|
-
e = return_raised_error do
|
51
|
-
AeNetworkConnectionException.try do
|
52
|
-
raise Errno::ECONNREFUSED.new('Connection refused - connect(2) for "example.com" port 443')
|
53
|
-
end
|
54
|
-
end
|
55
|
-
assert_equal AeNetworkConnectionException::ConnectionNotEstablished, e.class
|
56
|
-
assert_equal Errno::ECONNREFUSED, e.cause.class
|
57
|
-
|
58
|
-
e = return_raised_error do
|
59
|
-
AeNetworkConnectionException.try do
|
60
|
-
raise Errno::ETIMEDOUT.new('Connection timed out - connect(2) for "example.com" port 443')
|
61
|
-
end
|
62
|
-
end
|
63
|
-
assert_equal AeNetworkConnectionException::ConnectionNotEstablished, e.class
|
64
|
-
assert_equal Errno::ETIMEDOUT, e.cause.class
|
65
|
-
|
41
|
+
assert_connection_not_established_thrown_for(SocketError.new("getaddrinfo: Name or service not known"))
|
42
|
+
assert_connection_not_established_thrown_for(Errno::ECONNREFUSED.new('Connection refused - connect(2) for "example.com" port 443'))
|
43
|
+
assert_connection_not_established_thrown_for(Errno::ETIMEDOUT.new('Connection timed out - connect(2) for "example.com" port 443'))
|
44
|
+
assert_connection_not_established_thrown_for(Net::OpenTimeout.new('message'))
|
45
|
+
assert_connection_not_established_thrown_for(Errno::ECONNRESET.new('Connection reset by peer - SSL_connect'))
|
66
46
|
|
47
|
+
assert_connection_not_established_not_thrown_for(Errno::ECONNRESET.new('Connection timed out - connect(2) for "example.com" port 443'))
|
48
|
+
assert_connection_not_established_not_thrown_for(Errno::ETIMEDOUT.new('Connection timed out - recvfrom(2) for "example.com" port 443'))
|
49
|
+
end
|
67
50
|
|
51
|
+
private
|
52
|
+
|
53
|
+
def assert_connection_not_established_thrown_for(exception)
|
68
54
|
e = return_raised_error do
|
69
55
|
AeNetworkConnectionException.try do
|
70
|
-
raise
|
56
|
+
raise exception
|
71
57
|
end
|
72
58
|
end
|
73
|
-
assert_equal
|
74
|
-
|
59
|
+
assert_equal AeNetworkConnectionException::ConnectionNotEstablished, e.class
|
60
|
+
assert_equal exception, e.cause
|
61
|
+
end
|
62
|
+
|
63
|
+
def assert_connection_not_established_not_thrown_for(exception)
|
75
64
|
e = return_raised_error do
|
76
65
|
AeNetworkConnectionException.try do
|
77
|
-
raise
|
66
|
+
raise exception
|
78
67
|
end
|
79
68
|
end
|
80
|
-
assert_equal
|
81
|
-
|
69
|
+
assert_equal exception, e
|
82
70
|
end
|
83
71
|
|
84
|
-
private
|
85
|
-
|
86
72
|
def return_raised_error
|
87
73
|
yield
|
88
74
|
rescue => e
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ae_network_connection_exception
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Boyd
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,11 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
78
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.4.
|
79
|
+
rubygems_version: 2.4.3
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: Provides sane exceptions for network failures
|
83
|
-
test_files:
|
84
|
-
- test/test_helper.rb
|
85
|
-
- test/unit/ae_network_connection_exception_test.rb
|
86
|
-
has_rdoc:
|
83
|
+
test_files: []
|