ae_network_connection_exception 1.0.0 → 1.1.0

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: 69c47de028f1f0de4f3625fdf83a28fca3d6bbd1
4
- data.tar.gz: 27cbec84b9eaa7002eacaba19b3ace203a8d2b85
3
+ metadata.gz: 57008730ed008f3e7650a1e544dbdc05a8baebf5
4
+ data.tar.gz: dbfe0dc6d6ce931af76f6756de8f1f94aedc16b4
5
5
  SHA512:
6
- metadata.gz: d62132d7ed9febb5a1e1fe2eb0bbac4c33273708161ebad9731f1a1226ac699516c52434b2c29c3517f1493f239a17da11bdb685136aa36f6e071c0a8640a88b
7
- data.tar.gz: ca6a7edd4917f54e299ff3374c42e5a74f8a81d31bde26530c471a3d4d3f69ac1f1a234400a2a89a4e6ac2ebe266de6c212961c6f62f643c178d5a5b47694bf2
6
+ metadata.gz: 556c1d3da505100cc80436e88f041016dbf8ea4e708215d179c1c2bfcff064b949df50c1f4bd1714de1649bee03e528693a1a6b2f0b16a784e767bed0c3c9ca9
7
+ data.tar.gz: d5b1f855026a713f9c2cfddd1bf3ef8fe8f277f00ddd6199711830c9260bc933fa12db1b5834eecb1890904ec2b2588004729226756bded90f3a5aa488598967
data/.gitignore CHANGED
@@ -11,4 +11,5 @@
11
11
  *.so
12
12
  *.o
13
13
  *.a
14
+ .idea
14
15
  mkmf.log
data/Gemfile CHANGED
@@ -3,5 +3,5 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in ae_network_connection_exception.gemspec
4
4
  gemspec
5
5
 
6
- gem 'test-unit'
6
+ gem 'minitest', '~> 5.0'
7
7
 
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
- # If your network call failed to establise a network connection, an exception of type ```AeNetworkConnectionException::ConnectionNotEstablished``` will be thrown
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
 
@@ -14,7 +14,6 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
17
  spec.require_paths = ["lib"]
19
18
 
20
19
  spec.add_development_dependency "bundler", "~> 1.7"
@@ -4,34 +4,39 @@ require "socket"
4
4
  module AeNetworkConnectionException
5
5
 
6
6
  class ConnectionNotEstablished < StandardError
7
- attr_reader :cause
8
-
9
- def initialize(message = nil, cause = $!)
10
- super(message)
11
- @cause = cause
12
- end
7
+ end
13
8
 
14
- def message
15
- own_message = super
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
- if cause
18
- message = "#{own_message}, "
19
- message + "cause => #{cause.class}: #{cause.message}"
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
- own_message
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
@@ -1,3 +1,3 @@
1
1
  module AeNetworkConnectionException
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  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 'test/unit'
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::Unit::TestCase
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
- raise StandardError.new("New Child Message")
21
- rescue => e
22
- child_exception = e
23
- parent_exception = AeNetworkConnectionException::ConnectionNotEstablished.new("New Parent Message")
24
- end
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
- assert_not_nil child_exception
27
- assert_equal child_exception, parent_exception.cause
28
- assert_equal "New Parent Message, cause => StandardError: New Child Message", parent_exception.message
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
- e = return_raised_error do
43
- AeNetworkConnectionException.try do
44
- raise SocketError.new("getaddrinfo: Name or service not known")
45
- end
46
- end
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 Errno::ECONNRESET.new('Connection timed out - connect(2) for "example.com" port 443')
56
+ raise exception
71
57
  end
72
58
  end
73
- assert_equal Errno::ECONNRESET, e.class
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 Errno::ETIMEDOUT.new('Connection timed out - recvfrom(2) for "example.com" port 443')
66
+ raise exception
78
67
  end
79
68
  end
80
- assert_equal Errno::ETIMEDOUT, e.class
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.0.0
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-05 00:00:00.000000000 Z
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.1
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: []