flail 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+
3
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 John "asceth" Long
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ Flail is an exception catcher for Rack applications.
2
+
3
+ ##### Supports
4
+
5
+ * Rails 2.3.x
6
+ * Rails 3.x
7
+
8
+
9
+ #### Install
10
+
11
+ ```
12
+ $ [sudo] gem install flail
13
+ ```
14
+
15
+ For Rails 2.3.x, install the gem as a plugin
16
+
17
+
18
+ #### Usage
19
+
20
+
21
+ Add an initializer to configure (or call configure during application startup):
22
+
23
+ ```ruby
24
+ Flail.configure do
25
+ # configure a custom handler for the error payload
26
+ # don't call if you want to use the default http post handler
27
+ handler do |payload|
28
+ end
29
+
30
+ # endpoint for default handler
31
+ endpoint "https://flail.net/swing"
32
+
33
+ # environment of application, defaults to Rails.env
34
+ # included in payload
35
+ env "production"
36
+
37
+ # hostname to use of server, defaults to Socket.gethostname
38
+ # included in payload
39
+ host Socket.gethostname
40
+
41
+ # arbitrary api key which can identify
42
+ # your project or be anything else
43
+ api "custom_key"
44
+ end
45
+ ```
46
+
47
+
48
+ #### Author
49
+
50
+
51
+ Original author: John "asceth" Long
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ require "rspec"
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ gemspec = eval(File.read(File.join(Dir.pwd, "flail.gemspec")))
10
+
11
+ task :build => "#{gemspec.full_name}.gem"
12
+
13
+ task :test => :spec
14
+ task :default => :spec
15
+
16
+ file "#{gemspec.full_name}.gem" => gemspec.files + ["flail.gemspec"] do
17
+ system "gem build flail.gemspec"
18
+ system "gem install flail-#{Flail::VERSION}.gem"
19
+ end
20
+
data/flail.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "flail/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "flail"
6
+ s.version = Flail::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["John 'asceth' Long"]
9
+ s.email = ["machinist@asceth.com"]
10
+ s.homepage = "https://github.com/asceth/flail"
11
+ s.summary = "Rails exception handler"
12
+ s.description = "Handle Rails exceptions with the fail flail."
13
+
14
+ s.rubyforge_project = "flail"
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
+
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'rspec'
23
+ s.add_development_dependency 'rr'
24
+ end
data/lib/flail/base.rb ADDED
@@ -0,0 +1,16 @@
1
+ class Flail
2
+ module ClassMethods
3
+ def configure(&block)
4
+ configuration.instance_eval(&block)
5
+ end
6
+
7
+ def configuration
8
+ @configuration ||= Flail::Configuration.new.defaults!
9
+ end
10
+
11
+ def swing(payload)
12
+ Flail.configuration.handler.call(payload)
13
+ end
14
+ end
15
+ extend ClassMethods
16
+ end
@@ -0,0 +1,76 @@
1
+ class Flail
2
+ class Configuration
3
+ # custom handler for payloads
4
+ attr_reader :handler
5
+
6
+ # endpoint for default handler (used with flail-web)
7
+ attr_reader :endpoint
8
+
9
+ # environment of application
10
+ attr_reader :env
11
+
12
+ # hostname sending the error
13
+ attr_reader :hostname
14
+
15
+ # is the endpoint ssl?
16
+ attr_reader :secure_endpoint
17
+
18
+ # api key to use with payloads
19
+ attr_reader :api_key
20
+
21
+
22
+ def handle(&block)
23
+ @handler = block
24
+ end
25
+
26
+ def url(endpoint)
27
+ @endpoint = endpoint
28
+ end
29
+
30
+ def secure
31
+ @secure_endpoint = true
32
+ end
33
+
34
+ def environment(value)
35
+ @env = value
36
+ end
37
+
38
+ def host(value)
39
+ @hostname = value
40
+ end
41
+
42
+ def api(value)
43
+ @api_key = value
44
+ end
45
+
46
+ def defaults!
47
+ # configure some defaults
48
+ @secure_endpoint = false
49
+
50
+ handle do |payload|
51
+
52
+ url = URI.parse(Flail.configuration.endpoint)
53
+
54
+ http = Net::HTTP.new(url.host, url.port)
55
+
56
+ http.read_timeout = 5
57
+ http.open_timeout = 2
58
+
59
+ if Flail.configuration.secure_endpoint
60
+ http.use_ssl = true
61
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
62
+ else
63
+ http.use_ssl = false
64
+ end
65
+
66
+ begin
67
+ http.post(url.path, payload, HEADERS)
68
+ rescue *HTTP_ERRORS => e
69
+ nil
70
+ end
71
+ end
72
+
73
+ self
74
+ end # end defaults!
75
+ end
76
+ end
@@ -0,0 +1,67 @@
1
+ require 'socket'
2
+
3
+ class Flail
4
+ class Exception
5
+ def initialize(env, exception)
6
+ @exception = exception
7
+ @env = env
8
+ end
9
+
10
+ #
11
+ # Helpers
12
+ #
13
+ def request
14
+ @request ||= ActionDispatch::Request.new(@env)
15
+ end
16
+
17
+ def controller
18
+ @controller ||= @env['action_controller.instance']
19
+ end
20
+
21
+ def user
22
+ if controller.respond_to?(:current_user)
23
+ current_user = controller.current_user
24
+
25
+ {:id => current_user.id, :name => current_user.to_s}
26
+ else
27
+ {}
28
+ end
29
+ end
30
+
31
+
32
+ #
33
+ # Handling the exception
34
+ #
35
+ def handle!
36
+ Flail.swing(self.extract.to_json) unless self.ignore?
37
+ end
38
+
39
+ def extract
40
+ @extract ||= {}.tap do |info|
41
+ info[:class_name] = @exception.class.to_s # @exception class
42
+ info[:message] = @exception.to_s # error message
43
+ info[:trace] = @exception.backtrace.to_json # backtrace of error
44
+ info[:target_url] = request.url # url of request
45
+ info[:referer_url] = request.referer # referer
46
+ info[:params] = request.params.to_json # request parameters
47
+ info[:user_agent] = request.user_agent # user agent
48
+ info[:user] = self.user.to_json # current user
49
+
50
+ # special variables
51
+ info[:environment] = Flail.configuration.env
52
+ info[:hostname] = Flail.configuration.hostname
53
+ info[:api_key] = Flail.configuration.api_key
54
+ end
55
+ end
56
+
57
+ def ignore?
58
+ # Ignore requests with user agent string matching
59
+ # this regxp as they are surely made by bots
60
+ if @request.user_agent =~ /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg|Yandex|Jyxobot|Huaweisymantecspider|ApptusBot)\b/i
61
+ return true
62
+ end
63
+
64
+ false
65
+ end
66
+ end
67
+ end
data/lib/flail/rack.rb ADDED
@@ -0,0 +1,24 @@
1
+ class Flail
2
+ module Handlers
3
+ class Middleware
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ begin
10
+ response = @app.call(env)
11
+ rescue Exception => exception
12
+ Flail::Exception.new(env, exception).handle!
13
+ raise
14
+ end
15
+
16
+ if env['rack.exception']
17
+ Flail::Exception.new(env, env['rack.exception']).handle!
18
+ end
19
+
20
+ response
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ class Flail
2
+ module Rails
3
+ module ActionDispatch
4
+ def self.included(base)
5
+ base.send(:alias_method_chain, :render_exception, :flail)
6
+ end
7
+
8
+ def render_exception_with_flail(env, exception)
9
+ Flail::Exception.new(env, exception).handle!
10
+
11
+ render_exception_without_flail(env, exception)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ class Flail
2
+ module Rails
3
+ module RescueAction
4
+ # Sets up an alias chain to catch exceptions when Rails does
5
+ def self.included(base)
6
+ base.send(:alias_method, :rescue_action_in_public_without_flail, :rescue_action_in_public)
7
+ base.send(:alias_method, :rescue_action_in_public, :rescue_action_in_public_with_flail)
8
+ end
9
+
10
+ private
11
+
12
+ # Overrides the rescue_action method in ActionController::Base
13
+ # but uses any custom processing that is defined with
14
+ # Rails 2's exception helpers.
15
+ def rescue_action_in_public_with_flail(exception)
16
+ Flail::Exception.new(request.env, exception).handle!
17
+ rescue_action_in_public_without_flail(exception)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'flail'
2
+ require 'flail/rails/rescue_action'
3
+
4
+ if defined?(::ActionController::Base)
5
+ ::ActionController::Base.send(:include, Flail::Rails::RescueAction)
6
+ end
7
+
8
+ if defined?(::Rails.configuration) && ::Rails.configuration.respond_to?(:middleware)
9
+ ::Rails.configuration.middleware.insert_after 'ActionController::Failsafe', Flail::Rack
10
+ end
11
+
12
+ Flail.configure do
13
+ environment(defined?(::Rails.env) && ::Rails.env || defined?(RAILS_ENV) && RAILS_ENV)
14
+ host Socket.gethostname
15
+ end
@@ -0,0 +1,25 @@
1
+ class Flail
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "flail.use_rack_middleware" do |app|
4
+ app.config.middleware.insert 0, "Flail::Rack"
5
+ end
6
+
7
+ config.after_initialize do
8
+ Flail.configure do
9
+ environment ::Rails.env
10
+ host Socket.gethostname
11
+ end
12
+
13
+ if defined?(::ActionDispatch::DebugExceptions)
14
+ # Rails 3.2.x
15
+ require 'flail/rails/action_dispatch'
16
+ ::ActionDispatch::DebugExceptions.send(:include, Flail::Rails::ActionDispatch)
17
+
18
+ elsif defined?(::ActionDispatch::ShowExceptions)
19
+ # Rails 3.0.x || 3.1.x
20
+ require 'flail/rails/action_dispatch'
21
+ ::ActionDispatch::ShowExceptions.send(:include, Flail::Rails::ActionDispatch)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ class Flail
2
+ VERSION = "0.0.1"
3
+ end
data/lib/flail.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'socket'
4
+
5
+ require 'flail/base'
6
+ require 'flail/configuration'
7
+ require 'flail/exception'
8
+ require 'flail/rack'
9
+
10
+ require 'flail/railtie' if defined?(::Rails::Railtie)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'flail/rails'
data/spec/base_spec.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flail do
4
+
5
+ context "#configuration" do
6
+ it "should return the same object for multiple calls" do
7
+ Flail.configuration.should == Flail.configuration
8
+ end
9
+ end
10
+
11
+ context "#configure" do
12
+ it "should fail without a block" do
13
+ lambda { Flail.configure }.should raise_error
14
+ end
15
+
16
+ it "should instance_eval the block onto configuration" do
17
+ block = Proc.new { handle {|payload| } }
18
+ mock(Flail).configuration.stub!.instance_eval(&block)
19
+ Flail.configure(&block)
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ require 'rspec'
6
+ require 'flail'
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :rr
10
+ end
11
+
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John 'asceth' Long
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
47
+ name: rr
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Handle Rails exceptions with the fail flail.
63
+ email:
64
+ - machinist@asceth.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gemtest
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - LICENSE
74
+ - README.md
75
+ - Rakefile
76
+ - flail.gemspec
77
+ - lib/flail.rb
78
+ - lib/flail/base.rb
79
+ - lib/flail/configuration.rb
80
+ - lib/flail/exception.rb
81
+ - lib/flail/rack.rb
82
+ - lib/flail/rails.rb
83
+ - lib/flail/rails/action_dispatch.rb
84
+ - lib/flail/rails/rescue_action.rb
85
+ - lib/flail/railtie.rb
86
+ - lib/flail/version.rb
87
+ - rails/init.rb
88
+ - spec/base_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/asceth/flail
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project: flail
110
+ rubygems_version: 1.8.24
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Rails exception handler
114
+ test_files:
115
+ - spec/base_spec.rb
116
+ - spec/spec_helper.rb