Skip to content

Commit 48bb08f

Browse files
author
Edward Ocampo-Gooding
committed
First post
0 parents  commit 48bb08f

8 files changed

+146
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.gem
2+
.bundle
3+
Gemfile.lock
4+
pkg/*

Diff for: Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "http://rubygems.org"
2+
3+
# Specify your gem's dependencies in net_http_exception_fix.gemspec
4+
gemspec

Diff for: README.rdoc

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
= Net::HTTP Exception Fix
2+
3+
Using Net::HTTP can lead to headaches when it comes to rescuing all the different kinds of exceptions it might raise.
4+
5+
This is a hotfix to give those common exceptions that pop up from Net:HTTP usage a shared parent exception class.
6+
7+
== Usage
8+
9+
require 'net_http_exception_fix'
10+
11+
begin
12+
response = Net::HTTP.get_response(uri)
13+
rescue Net::HTTPBroken => e
14+
...
15+
end
16+
17+
== Thanks
18+
19+
Mad props to Tammer Saleh [1] and all the find folks on Stack Overflow [2] who confirmed my WTFness when it comes to handling Net::HTTP exceptions right.
20+
21+
[1] http://tammersaleh.com/posts/rescuing-net-http-exceptions
22+
[2] http://stackoverflow.com/questions/5370697/whats-the-best-way-to-handle-exceptions-from-nethttp
23+
24+
== Running tests
25+
26+
Run the tests with
27+
28+
$ rake test
29+
30+
== Download
31+
32+
Currently this library is available with git from:
33+
34+
git://github.com/edward/net_http_exception_fix.git
35+
36+
== Installation
37+
38+
=== From Git
39+
40+
You can check out the latest source from git:
41+
42+
> git pull git://github.com/edward/net_http_exception_fix.git
43+
44+
=== From Ruby Gems
45+
46+
Installation from RubyGems
47+
48+
> gem install net_http_exception_fix
49+
50+
== Author
51+
52+
Edward Ocampo-Gooding <[email protected]>
53+
54+
== Copyright
55+
56+
Copyright (C) 2011 Edward Ocampo-Gooding
57+
58+
Permission is hereby granted, free of charge, to any person obtaining a copy
59+
of this software and associated documentation files (the "Software"), to
60+
deal in the Software without restriction, including without limitation the
61+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
62+
sell copies of the Software, and to permit persons to whom the Software is
63+
furnished to do so, subject to the following conditions:
64+
65+
The above copyright notice and this permission notice shall be included in
66+
all copies or substantial portions of the Software.
67+
68+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
69+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
70+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
71+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
72+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
73+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: Rakefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'rake'
2+
require 'rake/testtask'
3+
require 'rake/clean'
4+
require 'rake/gempackagetask'
5+
require 'bundler'
6+
Bundler::GemHelper.install_tasks
7+
8+
task :default => [:test]
9+
10+
desc "Run tests"
11+
Rake::TestTask.new(:test) do |t|
12+
t.pattern = 'test/test_*.rb'
13+
t.verbose = true
14+
t.warning = true
15+
end

Diff for: lib/net_http_exception_fix.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'net/http'
2+
3+
module Net::HTTPBroken; end
4+
5+
[Timeout::Error, Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET,
6+
Errno::ECONNREFUSED, EOFError, Net::HTTPBadResponse,
7+
Net::HTTPHeaderSyntaxError, Net::ProtocolError].each do |m|
8+
m.send(:include, Net::HTTPBroken)
9+
end

Diff for: lib/net_http_exception_fix/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module NetHttpExceptionFix
2+
VERSION = "1.0.0"
3+
end

Diff for: net_http_exception_fix.gemspec

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
require "net_http_exception_fix/version"
4+
5+
Gem::Specification.new do |s|
6+
s.name = "net_http_exception_fix"
7+
s.version = NetHttpExceptionFix::VERSION
8+
s.platform = Gem::Platform::RUBY
9+
s.authors = ["Edward Ocampo-Gooding", "Tammer Saleh"]
10+
s.email = ["[email protected]"]
11+
s.homepage = ""
12+
s.summary = %q{Adds Net::HTTPBroken as a blanket exception you can rescue from when making Net::HTTP calls}
13+
s.description = %q{Slips Net::HTTPBroken into the inheritance ancestry of common exceptions Net::HTTP tends to raise.}
14+
s.rubyforge_project = "net_http_exception_fix"
15+
16+
s.files = `git ls-files`.split("\n")
17+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19+
s.require_paths = ["lib"]
20+
end

Diff for: test/test_net_http_exception_fix.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'test/unit'
2+
require 'net_http_exception_fix'
3+
4+
class TestNetHttpExceptionFix < Test::Unit::TestCase
5+
def test_valid_exception
6+
assert_raise Net::HTTPBroken do
7+
raise Net::HTTPBadResponse
8+
end
9+
end
10+
11+
def test_invalid_exception
12+
begin
13+
raise Errno::EACCES
14+
rescue StandardError => e
15+
assert !e.is_a?(Net::HTTPBroken)
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)