tanga-http-exceptions 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +2 -0
- data/lib/http/exceptions.rb +41 -0
- data/lib/http/exceptions/http_exception.rb +16 -0
- data/lib/http/exceptions/version.rb +5 -0
- data/spec/http_exception_spec.rb +67 -0
- data/spec/spec_helper.rb +91 -0
- data/tanga-http-exceptions.gemspec +23 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7aa3cb25bdec012849872b82ffb4f741d22e4c95
|
4
|
+
data.tar.gz: 822d2f792be1db16d6468b463f83cd10af681a17
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cc0c178143e16737806db636a3d2e3a6f4366744c4b68de3bd0fa9d98c5cd0a5cf4966e3f1782c788f1f3b7afdee86dd77643d4ba444bf0f997a7f250812dbee
|
7
|
+
data.tar.gz: 8fdf5bea3299eaa80da4b27acb00949c0416b5e6ffec423d21ea1f033e60c0e831a44e28e6dcc5a27f4bf2f4abbf0417497f1fe0196691ed559d8882d7778509
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
tags
|
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 2.2.0@http-exceptions --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Rainforest QA
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/rainforestapp/http-exceptions.png?branch=master)](https://travis-ci.org/rainforestapp/http-exceptions)
|
2
|
+
|
3
|
+
# Http::Exceptions
|
4
|
+
|
5
|
+
Http::Exceptions provides an easy way to rescue exceptions that might be thrown by your Http library.
|
6
|
+
|
7
|
+
If you're using a library such as the excellent [HTTParty](https://github.com/jnunemaker/httparty), you still have to deal with various types of exceptions. In an ideal world, the return code of the HTTP request would be the sole indicator of failures, but HTTP libraries can raise a large number of exceptions (such as `SocketError` or `Net::ReadTimeout`) that you need to handle.
|
8
|
+
|
9
|
+
Http::Exceptions converts any error that might be raised by your HTTP library and wrap it in a `Http::Exceptions::HttpException`.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'http-exceptions'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install http-exceptions
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Only rescue raised exceptions.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
response = Http::Exceptions.wrap_exception do
|
31
|
+
HTTParty.get "http://www.google.com"
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Raise an exception if the return code of the API call is not `2XX`.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
response = Http::Exceptions.wrap_and_check do
|
39
|
+
HTTParty.get "http://www.google.com"
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
You can then rescue the exception in the following way:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
begin
|
47
|
+
response = Http::Exceptions.wrap_and_check do
|
48
|
+
HTTParty.get "http://www.google.com"
|
49
|
+
end
|
50
|
+
rescue Http::Exceptions::HttpException => e
|
51
|
+
# ...
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
### Support
|
56
|
+
|
57
|
+
Currently, this only has been tested with HTTParty. It should however work with any library that delegates to the ruby http library.
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it ( https://github.com/rainforestapp/http-exceptions/fork )
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "http/exceptions/version"
|
2
|
+
require "http/exceptions/http_exception"
|
3
|
+
|
4
|
+
module Http
|
5
|
+
module Exceptions
|
6
|
+
EXCEPTIONS = [
|
7
|
+
SocketError,
|
8
|
+
Errno::ETIMEDOUT,
|
9
|
+
(Net.const_defined?(:ReadTimeout) ? Net::ReadTimeout : EOFError),
|
10
|
+
(Net.const_defined?(:OpenTimeout) ? Net::OpenTimeout : EOFError),
|
11
|
+
Net::ProtocolError,
|
12
|
+
Errno::ECONNREFUSED,
|
13
|
+
Errno::EHOSTDOWN,
|
14
|
+
Errno::ECONNRESET,
|
15
|
+
Errno::ENETUNREACH,
|
16
|
+
Errno::EHOSTUNREACH,
|
17
|
+
Errno::ECONNABORTED,
|
18
|
+
OpenSSL::SSL::SSLError,
|
19
|
+
EOFError,
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
def self.wrap_exception
|
23
|
+
begin
|
24
|
+
yield
|
25
|
+
rescue *Exceptions::EXCEPTIONS => e
|
26
|
+
raise HttpException.new original_exception: e
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.check_response!(res)
|
31
|
+
raise HttpException.new(response: res) unless (200...300).include?(res.code)
|
32
|
+
res
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.wrap_and_check
|
36
|
+
wrap_exception do
|
37
|
+
check_response! yield
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Http
|
2
|
+
module Exceptions
|
3
|
+
class HttpException < RuntimeError
|
4
|
+
attr_reader :response, :original_exception
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@original_exception = options[:original_exception]
|
8
|
+
@response = options[:response]
|
9
|
+
msg = "An error as occured while processing response."
|
10
|
+
msg += " Status #{response.code}\n#{response.body}" if response
|
11
|
+
msg += " Original Exception: #{original_exception}" if original_exception
|
12
|
+
super msg
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Http::Exceptions do
|
4
|
+
let(:invalid_response) { double(code: 400, body: '') }
|
5
|
+
let(:valid_response) { double(code: 200) }
|
6
|
+
|
7
|
+
class TestException < RuntimeError
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ".wrap_exception" do
|
11
|
+
it "raises an HttpException on supported http exceptions" do
|
12
|
+
expect do
|
13
|
+
described_class.wrap_exception do
|
14
|
+
raise SocketError
|
15
|
+
end
|
16
|
+
end.to raise_error(Http::Exceptions::HttpException)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "saves the original exception against the HttpException" do
|
20
|
+
begin
|
21
|
+
described_class.wrap_exception do
|
22
|
+
raise SocketError
|
23
|
+
end
|
24
|
+
rescue Http::Exceptions::HttpException => e
|
25
|
+
expect(e.original_exception).to be_a(SocketError)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "ignores other exceptions" do
|
30
|
+
expect do
|
31
|
+
described_class.wrap_exception do
|
32
|
+
raise TestException
|
33
|
+
end
|
34
|
+
end.to raise_error(TestException)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe ".check_response!" do
|
39
|
+
it "raises exception on non-200 response" do
|
40
|
+
expect do
|
41
|
+
described_class.check_response!(invalid_response)
|
42
|
+
end.to raise_error(Http::Exceptions::HttpException)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "the raised exception contains the response" do
|
46
|
+
begin
|
47
|
+
described_class.check_response!(invalid_response)
|
48
|
+
rescue Http::Exceptions::HttpException => e
|
49
|
+
expect(e.response).to eq(invalid_response)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns the response on valid response" do
|
54
|
+
expect(described_class.check_response!(valid_response)).to eq(valid_response)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe ".wrap_and_check" do
|
59
|
+
it "raises exception on bad response" do
|
60
|
+
expect do
|
61
|
+
described_class.wrap_and_check do
|
62
|
+
invalid_response
|
63
|
+
end
|
64
|
+
end.to raise_error(Http::Exceptions::HttpException)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'http/exceptions'
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
6
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
7
|
+
#
|
8
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
9
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
10
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
11
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
12
|
+
# a separate helper file that requires the additional dependencies and performs
|
13
|
+
# the additional setup, and require it from the spec files that actually need it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
54
|
+
# For more details, see:
|
55
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
56
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
57
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
58
|
+
config.disable_monkey_patching!
|
59
|
+
|
60
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
61
|
+
# be too noisy due to issues in dependencies.
|
62
|
+
config.warnings = true
|
63
|
+
|
64
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
65
|
+
# file, and it's useful to allow more verbose output when running an
|
66
|
+
# individual spec file.
|
67
|
+
if config.files_to_run.one?
|
68
|
+
# Use the documentation formatter for detailed output,
|
69
|
+
# unless a formatter has already been configured
|
70
|
+
# (e.g. via a command-line flag).
|
71
|
+
config.default_formatter = 'doc'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Print the 10 slowest examples and example groups at the
|
75
|
+
# end of the spec run, to help surface which specs are running
|
76
|
+
# particularly slow.
|
77
|
+
config.profile_examples = 10
|
78
|
+
|
79
|
+
# Run specs in random order to surface order dependencies. If you find an
|
80
|
+
# order dependency and want to debug it, you can fix the order by providing
|
81
|
+
# the seed, which is printed after each run.
|
82
|
+
# --seed 1234
|
83
|
+
config.order = :random
|
84
|
+
|
85
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
86
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
87
|
+
# test failures related to randomization by passing the same `--seed` value
|
88
|
+
# as the one that triggered the failure.
|
89
|
+
Kernel.srand config.seed
|
90
|
+
=end
|
91
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'http/exceptions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tanga-http-exceptions"
|
8
|
+
spec.version = Http::Exceptions::VERSION
|
9
|
+
spec.authors = ["Simon Mathieu"]
|
10
|
+
spec.email = ["simon.math@gmail.com"]
|
11
|
+
spec.summary = %q{An easy way to rescue exceptions that might be thrown by your Http library}
|
12
|
+
spec.description = %q{An easy way to rescue exceptions that might be thrown by your Http library}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tanga-http-exceptions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Mathieu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-31 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: An easy way to rescue exceptions that might be thrown by your Http library
|
42
|
+
email:
|
43
|
+
- simon.math@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".rvmrc"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/http/exceptions.rb
|
57
|
+
- lib/http/exceptions/http_exception.rb
|
58
|
+
- lib/http/exceptions/version.rb
|
59
|
+
- spec/http_exception_spec.rb
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
- tanga-http-exceptions.gemspec
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: An easy way to rescue exceptions that might be thrown by your Http library
|
86
|
+
test_files:
|
87
|
+
- spec/http_exception_spec.rb
|
88
|
+
- spec/spec_helper.rb
|