errorinbox 0.0.1a

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ gem "rack"
5
+ gem "rspec", "~> 2.14.1"
6
+ gem "webmock", "~> 1.13.0"
7
+ gem "timecop", "~> 0.6.3"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rafael Souza
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,29 @@
1
+ # ErrorInbox
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'error_inbox'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install error_inbox
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 "error_inbox/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "errorinbox"
8
+ spec.version = ErrorInbox::VERSION
9
+ spec.authors = ["Rafael Souza"]
10
+ spec.email = ["me@rafaelss.com"]
11
+ spec.description = %q{Send exceptions to errorinbox.com}
12
+ spec.summary = %q{Capture and send all exceptions raised by your app to errorinbox.com}
13
+ spec.homepage = "http://github.com/rafaelss/errorinbox"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,5 @@
1
+ module ErrorInbox
2
+ class Configuration
3
+ attr_accessor :username, :password
4
+ end
5
+ end
@@ -0,0 +1,72 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ module ErrorInbox
5
+ class Notifier
6
+ def initialize(options)
7
+ @options = options
8
+ end
9
+
10
+ def save(ex)
11
+ unless ErrorInbox.configuration.username && ErrorInbox.configuration.password
12
+ raise MissingCredentialsError
13
+ end
14
+
15
+ uri = URI("http://oops.errorinbox.com/")
16
+ req = Net::HTTP::Post.new(uri.path)
17
+ req.basic_auth(ErrorInbox.configuration.username, ErrorInbox.configuration.password)
18
+ req["Content-Type"] = "application/json"
19
+ req.body = prepare_body(ex)
20
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
21
+ http.request(req)
22
+ end
23
+
24
+ case res
25
+ when Net::HTTPCreated
26
+ JSON.load(res.body)["id"]
27
+ when Net::HTTPForbidden
28
+ raise InvalidCredentialsError
29
+ else
30
+ raise "Unknow error: #{res}"
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def prepare_body(ex)
37
+ body = {
38
+ :type => ex.class.name,
39
+ :message => ex.message,
40
+ :backtrace => ex.backtrace.join("\n"),
41
+ :environmentName => ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development",
42
+ :occurredAt => Time.now.xmlschema
43
+ }
44
+
45
+ if rack_env
46
+ require "rack"
47
+ body[:request] = { :url => ::Rack::Request.new(rack_env).url }
48
+
49
+ body[:environment] = {}
50
+ rack_env.each do |key, value|
51
+ body[:environment][key] = value.to_s
52
+ end
53
+
54
+ if rack_session
55
+ rack_session.each do |key, value|
56
+ body[:session][key] = value.to_s
57
+ end
58
+ end
59
+ end
60
+
61
+ JSON.dump(body)
62
+ end
63
+
64
+ def rack_env
65
+ @rack_env ||= @options[:rack_env] if @options[:rack_env].respond_to?(:each)
66
+ end
67
+
68
+ def rack_session
69
+ @rack_session ||= rack_env["rack.session"] if rack_env["rack.session"].respond_to?(:each)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,28 @@
1
+ module ErrorInbox
2
+ class Rack
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ begin
9
+ response = @app.call(env)
10
+ rescue Exception => raised
11
+ ErrorInbox.notify(raised, env)
12
+ raise raised
13
+ end
14
+
15
+ if framework_exception(env)
16
+ ErrorInbox.notify(framework_exception(env), env)
17
+ end
18
+
19
+ response
20
+ end
21
+
22
+ protected
23
+
24
+ def framework_exception(env)
25
+ env["rack.exception"] || env["action_dispatch.exception"] || env["sinatra.error"]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module ErrorInbox
2
+ VERSION = "0.0.1a"
3
+ end
@@ -0,0 +1,21 @@
1
+ require "error_inbox/version"
2
+ require "error_inbox/notifier"
3
+ require "error_inbox/configuration"
4
+
5
+ module ErrorInbox
6
+ class MissingCredentialsError < StandardError; end
7
+ class InvalidCredentialsError < StandardError; end
8
+
9
+ def self.notify(ex, env)
10
+ notifier = Notifier.new(env)
11
+ notifier.save(ex)
12
+ end
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield configuration if block_given?
20
+ end
21
+ end
data/lib/errorinbox.rb ADDED
@@ -0,0 +1 @@
1
+ require "error_inbox"
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe ErrorInbox::Notifier do
4
+ let(:ex) { double("exception", :message => "some message", :backtrace => ["a.rb:10", "b.rb:11"]) }
5
+
6
+ around do |example|
7
+ Timecop.travel(2013, 8, 21, 11, 57, 0) do
8
+ example.run
9
+ end
10
+ end
11
+
12
+ it "sends rack exceptions" do
13
+ ErrorInbox.stub(:configuration => double("configuration", :username => "foo", :password => "bar"))
14
+
15
+ stub_request(:post, "http://foo:bar@oops.errorinbox.com").
16
+ with(
17
+ :body => "{\"type\":\"RSpec::Mocks::Mock\",\"message\":\"some message\",\"backtrace\":\"a.rb:10\\nb.rb:11\",\"environmentName\":null,\"occurredAt\":\"2013-08-21T11:57:00-03:00\",\"request\":{\"url\":\"://::0\"},\"environment\":{\"foo\":\"bar\"}}",
18
+ :headers => { "Content-Type" => "application/json" }
19
+ ).
20
+ to_return(
21
+ :status => 201,
22
+ :body => "{\"id\":1}",
23
+ :headers => { "Content-Type" => "application/json" }
24
+ )
25
+
26
+ notifier = described_class.new(:rack_env => { :foo => "bar" })
27
+ expect(notifier.save(ex)).to eq(1)
28
+ end
29
+
30
+ it "raises an error if credentials are missing" do
31
+ ErrorInbox.stub(:configuration => double("configuration", :username => nil, :password => nil))
32
+
33
+ notifier = described_class.new(:rack_env => { :foo => "bar" })
34
+ expect { notifier.save(ex) }.to raise_error(ErrorInbox::MissingCredentialsError)
35
+ end
36
+
37
+ it "raises an error if credentials are invalid" do
38
+ ErrorInbox.stub(:configuration => double("configuration", :username => "foo", :password => "bar"))
39
+
40
+ stub_request(:post, "http://foo:bar@oops.errorinbox.com").
41
+ with(
42
+ :body => "{\"type\":\"RSpec::Mocks::Mock\",\"message\":\"some message\",\"backtrace\":\"a.rb:10\\nb.rb:11\",\"environmentName\":null,\"occurredAt\":\"2013-08-21T11:57:00-03:00\",\"request\":{\"url\":\"://::0\"},\"environment\":{\"foo\":\"bar\"}}",
43
+ :headers => { 'Content-Type'=>'application/json' }
44
+ ).
45
+ to_return(
46
+ :status => 403,
47
+ :body => "{\"error\":\"forbidden\"}",
48
+ :headers => { "Content-Type" => "application/json" }
49
+ )
50
+
51
+ notifier = described_class.new(:rack_env => { :foo => "bar" })
52
+ expect { notifier.save(ex) }.to raise_error(ErrorInbox::InvalidCredentialsError)
53
+ end
54
+ end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+ require "error_inbox/rack"
3
+
4
+ describe ErrorInbox::Rack do
5
+ let(:app) { double("app") }
6
+ subject { described_class.new(app) }
7
+
8
+ it "sends exception raised by app#call" do
9
+ app.
10
+ should_receive(:call).
11
+ with({}).
12
+ and_raise(RuntimeError)
13
+
14
+ ErrorInbox.
15
+ should_receive(:notify).
16
+ with(an_instance_of(RuntimeError), {}).
17
+ once
18
+
19
+ expect { subject.call({}) }.to raise_error(RuntimeError)
20
+ end
21
+
22
+ it "sends exception stored in rack.exception env variable" do
23
+ error = double("error")
24
+ env = { "rack.exception" => error }
25
+
26
+ app.
27
+ should_receive(:call).
28
+ with(env).
29
+ and_return("response")
30
+
31
+ ErrorInbox.
32
+ should_receive(:notify).
33
+ with(error, env).
34
+ once
35
+
36
+ expect(subject.call(env)).to eq("response")
37
+ end
38
+
39
+ it "sends exception stored in action_dispatch.exception env variable" do
40
+ error = double("error")
41
+ env = { "action_dispatch.exception" => error }
42
+
43
+ app.
44
+ should_receive(:call).
45
+ with(env).
46
+ and_return("response")
47
+
48
+ ErrorInbox.
49
+ should_receive(:notify).
50
+ with(error, env).
51
+ once
52
+
53
+ expect(subject.call(env)).to eq("response")
54
+ end
55
+
56
+ it "sends exception stored in sinatra.error env variable" do
57
+ error = double("error")
58
+ env = { "sinatra.error" => error }
59
+
60
+ app.
61
+ should_receive(:call).
62
+ with(env).
63
+ and_return("response")
64
+
65
+ ErrorInbox.
66
+ should_receive(:notify).
67
+ with(error, env).
68
+ once
69
+
70
+ expect(subject.call(env)).to eq("response")
71
+ end
72
+
73
+ it "does nothing if no errors at all" do
74
+ app.
75
+ should_receive(:call).
76
+ with({}).
77
+ and_return("response")
78
+
79
+ expect(subject.call({})).to eq("response")
80
+ end
81
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe ErrorInbox do
4
+ describe ".notify" do
5
+ let(:ex) { double("exception") }
6
+ let(:env) { { :foo => "bar" } }
7
+
8
+ it "calls notifier to handle the exception" do
9
+ ErrorInbox::Notifier.
10
+ should_receive(:new).
11
+ with(env).
12
+ and_return(notifier = double("notifier"))
13
+
14
+ notifier.
15
+ should_receive(:save).
16
+ with(ex).
17
+ and_return(true)
18
+
19
+ expect(described_class.notify(ex, env)).to eq(true)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ require "errorinbox"
2
+ require "webmock/rspec"
3
+ require "timecop"
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = "random"
10
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: errorinbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1a
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Rafael Souza
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Send exceptions to errorinbox.com
47
+ email:
48
+ - me@rafaelss.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - errorinbox.gemspec
60
+ - lib/error_inbox.rb
61
+ - lib/error_inbox/configuration.rb
62
+ - lib/error_inbox/notifier.rb
63
+ - lib/error_inbox/rack.rb
64
+ - lib/error_inbox/version.rb
65
+ - lib/errorinbox.rb
66
+ - spec/error_inbox/notifier_spec.rb
67
+ - spec/error_inbox/rack_spec.rb
68
+ - spec/error_inbox_spec.rb
69
+ - spec/spec_helper.rb
70
+ homepage: http://github.com/rafaelss/errorinbox
71
+ licenses:
72
+ - MIT
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -1940987844180716958
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>'
90
+ - !ruby/object:Gem::Version
91
+ version: 1.3.1
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.23
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Capture and send all exceptions raised by your app to errorinbox.com
98
+ test_files:
99
+ - spec/error_inbox/notifier_spec.rb
100
+ - spec/error_inbox/rack_spec.rb
101
+ - spec/error_inbox_spec.rb
102
+ - spec/spec_helper.rb