airbrake 3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +18 -0
  2. data/.yardopts +3 -0
  3. data/CHANGELOG +441 -0
  4. data/Gemfile +3 -0
  5. data/INSTALL +25 -0
  6. data/MIT-LICENSE +22 -0
  7. data/README.md +431 -0
  8. data/README_FOR_HEROKU_ADDON.md +93 -0
  9. data/Rakefile +188 -0
  10. data/SUPPORTED_RAILS_VERSIONS +14 -0
  11. data/TESTING.md +26 -0
  12. data/airbrake.gemspec +33 -0
  13. data/features/metal.feature +23 -0
  14. data/features/rack.feature +27 -0
  15. data/features/rails.feature +254 -0
  16. data/features/rails_with_js_notifier.feature +78 -0
  17. data/features/rake.feature +23 -0
  18. data/features/sinatra.feature +33 -0
  19. data/features/step_definitions/airbrake_shim.rb.template +15 -0
  20. data/features/step_definitions/file_steps.rb +10 -0
  21. data/features/step_definitions/metal_steps.rb +23 -0
  22. data/features/step_definitions/rack_steps.rb +20 -0
  23. data/features/step_definitions/rails_application_steps.rb +401 -0
  24. data/features/step_definitions/rake_steps.rb +17 -0
  25. data/features/support/airbrake_shim.rb.template +15 -0
  26. data/features/support/env.rb +18 -0
  27. data/features/support/matchers.rb +35 -0
  28. data/features/support/rails.rb +181 -0
  29. data/features/support/rake/Rakefile +57 -0
  30. data/features/support/terminal.rb +103 -0
  31. data/features/user_informer.feature +63 -0
  32. data/generators/airbrake/airbrake_generator.rb +90 -0
  33. data/generators/airbrake/lib/insert_commands.rb +34 -0
  34. data/generators/airbrake/lib/rake_commands.rb +24 -0
  35. data/generators/airbrake/templates/airbrake_tasks.rake +25 -0
  36. data/generators/airbrake/templates/capistrano_hook.rb +6 -0
  37. data/generators/airbrake/templates/initializer.rb +6 -0
  38. data/install.rb +1 -0
  39. data/lib/airbrake.rb +150 -0
  40. data/lib/airbrake/backtrace.rb +100 -0
  41. data/lib/airbrake/capistrano.rb +21 -0
  42. data/lib/airbrake/configuration.rb +247 -0
  43. data/lib/airbrake/notice.rb +348 -0
  44. data/lib/airbrake/rack.rb +42 -0
  45. data/lib/airbrake/rails.rb +41 -0
  46. data/lib/airbrake/rails/action_controller_catcher.rb +30 -0
  47. data/lib/airbrake/rails/controller_methods.rb +68 -0
  48. data/lib/airbrake/rails/error_lookup.rb +33 -0
  49. data/lib/airbrake/rails/javascript_notifier.rb +42 -0
  50. data/lib/airbrake/rails3_tasks.rb +82 -0
  51. data/lib/airbrake/railtie.rb +33 -0
  52. data/lib/airbrake/rake_handler.rb +65 -0
  53. data/lib/airbrake/sender.rb +83 -0
  54. data/lib/airbrake/shared_tasks.rb +30 -0
  55. data/lib/airbrake/tasks.rb +83 -0
  56. data/lib/airbrake/user_informer.rb +25 -0
  57. data/lib/airbrake/version.rb +3 -0
  58. data/lib/airbrake_tasks.rb +50 -0
  59. data/lib/rails/generators/airbrake/airbrake_generator.rb +96 -0
  60. data/lib/templates/javascript_notifier.erb +13 -0
  61. data/lib/templates/rescue.erb +91 -0
  62. data/rails/init.rb +1 -0
  63. data/script/integration_test.rb +38 -0
  64. data/test/airbrake_2_2.xsd +78 -0
  65. data/test/airbrake_tasks_test.rb +163 -0
  66. data/test/backtrace_test.rb +163 -0
  67. data/test/catcher_test.rb +333 -0
  68. data/test/configuration_test.rb +216 -0
  69. data/test/helper.rb +251 -0
  70. data/test/javascript_notifier_test.rb +52 -0
  71. data/test/logger_test.rb +85 -0
  72. data/test/notice_test.rb +459 -0
  73. data/test/notifier_test.rb +235 -0
  74. data/test/rack_test.rb +58 -0
  75. data/test/rails_initializer_test.rb +36 -0
  76. data/test/recursion_test.rb +10 -0
  77. data/test/sender_test.rb +193 -0
  78. data/test/user_informer_test.rb +29 -0
  79. metadata +365 -0
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'airbrake/rails'
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'logger'
4
+ require 'fileutils'
5
+
6
+ RAILS_ENV = "production"
7
+ RAILS_ROOT = FileUtils.pwd
8
+ RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
9
+
10
+ $: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ require 'airbrake'
12
+ require 'rails/init'
13
+
14
+ fail "Please supply an API Key as the first argument" if ARGV.empty?
15
+
16
+ host = ARGV[1]
17
+ host ||= "airbrakeapp.com"
18
+
19
+ secure = (ARGV[2] == "secure")
20
+
21
+ exception = begin
22
+ raise "Testing airbrake notifier with secure = #{secure}. If you can see this, it works."
23
+ rescue => foo
24
+ foo
25
+ end
26
+
27
+ Airbrake.configure do |config|
28
+ config.secure = secure
29
+ config.host = host
30
+ config.api_key = ARGV.first
31
+ end
32
+ puts "Configuration:"
33
+ Airbrake.configuration.to_hash.each do |key, value|
34
+ puts sprintf("%25s: %s", key.to_s, value.inspect.slice(0, 55))
35
+ end
36
+ puts "Sending #{secure ? "" : "in"}secure notification to project with key #{ARGV.first}"
37
+ Airbrake.notify(exception)
38
+
@@ -0,0 +1,78 @@
1
+ <?xml version="1.0"?>
2
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3
+
4
+ <xs:element name="notice">
5
+ <xs:complexType>
6
+ <xs:all>
7
+ <xs:element name="api-key" type="xs:string"/>
8
+ <xs:element name="notifier" type="notifier"/>
9
+ <xs:element name="error" type="error"/>
10
+ <xs:element name="request" type="request" minOccurs="0"/>
11
+ <xs:element name="server-environment" type="serverEnvironment"/>
12
+ </xs:all>
13
+ <xs:attribute name="version" type="xs:string" use="required"/>
14
+ </xs:complexType>
15
+ </xs:element>
16
+
17
+ <xs:complexType name="notifier">
18
+ <xs:all>
19
+ <xs:element name="name" type="xs:string"/>
20
+ <xs:element name="version" type="xs:string"/>
21
+ <xs:element name="url" type="xs:string"/>
22
+ </xs:all>
23
+ </xs:complexType>
24
+
25
+ <xs:complexType name="error">
26
+ <xs:all>
27
+ <xs:element name="class" type="xs:string"/>
28
+ <xs:element name="message" type="xs:string" minOccurs="0"/>
29
+ <xs:element name="backtrace" type="backtrace"/>
30
+ </xs:all>
31
+ </xs:complexType>
32
+
33
+ <xs:complexType name="backtrace">
34
+ <xs:sequence>
35
+ <xs:element name="line" maxOccurs="unbounded">
36
+ <xs:complexType>
37
+ <xs:attribute name="file" type="xs:string" use="required"/>
38
+ <xs:attribute name="number" type="xs:string" use="required"/>
39
+ <xs:attribute name="method" type="xs:string" use="optional"/>
40
+ </xs:complexType>
41
+ </xs:element>
42
+ </xs:sequence>
43
+ </xs:complexType>
44
+
45
+ <xs:complexType name="request">
46
+ <xs:all>
47
+ <xs:element name="url" type="xs:string"/>
48
+ <xs:element name="component" type="xs:string"/>
49
+ <xs:element name="action" type="xs:string" minOccurs="0"/>
50
+ <xs:element name="params" type="varList" minOccurs="0"/>
51
+ <xs:element name="session" type="varList" minOccurs="0"/>
52
+ <xs:element name="cgi-data" type="varList" minOccurs="0"/>
53
+ </xs:all>
54
+ </xs:complexType>
55
+
56
+ <xs:complexType name="varList">
57
+ <xs:sequence>
58
+ <xs:element name="var" type="var" maxOccurs="unbounded"/>
59
+ </xs:sequence>
60
+ </xs:complexType>
61
+
62
+ <xs:complexType name="var" mixed="true">
63
+ <xs:sequence>
64
+ <xs:element name="var" type="var" minOccurs="0" maxOccurs="unbounded"/>
65
+ </xs:sequence>
66
+ <xs:attribute name="key" type="xs:string" use="required"/>
67
+ </xs:complexType>
68
+
69
+ <xs:complexType name="serverEnvironment">
70
+ <xs:sequence>
71
+ <xs:element name="project-root" type="xs:string" minOccurs="0"/>
72
+ <xs:element name="environment-name" type="xs:string"/>
73
+ <xs:element name="app-version" type="xs:string" minOccurs="0"/>
74
+ <xs:element name="hostname" type="xs:string" minOccurs="0"/>
75
+ </xs:sequence>
76
+ </xs:complexType>
77
+
78
+ </xs:schema>
@@ -0,0 +1,163 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'rubygems'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/airbrake_tasks'
5
+ require 'fakeweb'
6
+
7
+ FakeWeb.allow_net_connect = false
8
+
9
+ class AirbrakeTasksTest < Test::Unit::TestCase
10
+ def successful_response(body = "")
11
+ response = Net::HTTPSuccess.new('1.2', '200', 'OK')
12
+ response.stubs(:body).returns(body)
13
+ return response
14
+ end
15
+
16
+ def unsuccessful_response(body = "")
17
+ response = Net::HTTPClientError.new('1.2', '200', 'OK')
18
+ response.stubs(:body).returns(body)
19
+ return response
20
+ end
21
+
22
+ context "being quiet" do
23
+ setup { AirbrakeTasks.stubs(:puts) }
24
+
25
+ context "in a configured project" do
26
+ setup { Airbrake.configure { |config| config.api_key = "1234123412341234" } }
27
+
28
+ context "on deploy({})" do
29
+ setup { @output = AirbrakeTasks.deploy({}) }
30
+
31
+ before_should "complain about missing rails env" do
32
+ AirbrakeTasks.expects(:puts).with(regexp_matches(/rails environment/i))
33
+ end
34
+
35
+ should "return false" do
36
+ assert !@output
37
+ end
38
+ end
39
+
40
+ context "given an optional HTTP proxy and valid options" do
41
+ setup do
42
+ @response = stub("response", :body => "stub body")
43
+ @http_proxy = stub("proxy", :post_form => @response)
44
+
45
+ Net::HTTP.expects(:Proxy).
46
+ with(Airbrake.configuration.proxy_host,
47
+ Airbrake.configuration.proxy_port,
48
+ Airbrake.configuration.proxy_user,
49
+ Airbrake.configuration.proxy_pass).
50
+ returns(@http_proxy)
51
+
52
+ @options = { :rails_env => "staging", :dry_run => false }
53
+ end
54
+
55
+ context "performing a dry run" do
56
+ setup { @output = AirbrakeTasks.deploy(@options.merge(:dry_run => true)) }
57
+
58
+ should "return true without performing any actual request" do
59
+ assert_equal true, @output
60
+ assert_received(@http_proxy, :post_form) do|expects|
61
+ expects.never
62
+ end
63
+ end
64
+ end
65
+
66
+ context "on deploy(options)" do
67
+ setup do
68
+ @output = AirbrakeTasks.deploy(@options)
69
+ end
70
+
71
+ before_should "post to http://airbrakeapp.com/deploys.txt" do
72
+ URI.stubs(:parse).with('http://airbrakeapp.com/deploys.txt').returns(:uri)
73
+ @http_proxy.expects(:post_form).with(:uri, kind_of(Hash)).returns(successful_response)
74
+ end
75
+
76
+ before_should "use the project api key" do
77
+ @http_proxy.expects(:post_form).
78
+ with(kind_of(URI), has_entries('api_key' => "1234123412341234")).
79
+ returns(successful_response)
80
+ end
81
+
82
+ before_should "use send the rails_env param" do
83
+ @http_proxy.expects(:post_form).
84
+ with(kind_of(URI), has_entries("deploy[rails_env]" => "staging")).
85
+ returns(successful_response)
86
+ end
87
+
88
+ [:local_username, :scm_repository, :scm_revision].each do |key|
89
+ before_should "use send the #{key} param if it's passed in." do
90
+ @options[key] = "value"
91
+ @http_proxy.expects(:post_form).
92
+ with(kind_of(URI), has_entries("deploy[#{key}]" => "value")).
93
+ returns(successful_response)
94
+ end
95
+ end
96
+
97
+ before_should "use the :api_key param if it's passed in." do
98
+ @options[:api_key] = "value"
99
+ @http_proxy.expects(:post_form).
100
+ with(kind_of(URI), has_entries("api_key" => "value")).
101
+ returns(successful_response)
102
+ end
103
+
104
+ before_should "puts the response body on success" do
105
+ AirbrakeTasks.expects(:puts).with("body")
106
+ @http_proxy.expects(:post_form).with(any_parameters).returns(successful_response('body'))
107
+ end
108
+
109
+ before_should "puts the response body on failure" do
110
+ AirbrakeTasks.expects(:puts).with("body")
111
+ @http_proxy.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
112
+ end
113
+
114
+ should "return false on failure", :before => lambda {
115
+ @http_proxy.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
116
+ } do
117
+ assert !@output
118
+ end
119
+
120
+ should "return true on success", :before => lambda {
121
+ @http_proxy.expects(:post_form).with(any_parameters).returns(successful_response('body'))
122
+ } do
123
+ assert @output
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ context "in a configured project with custom host" do
130
+ setup do
131
+ Airbrake.configure do |config|
132
+ config.api_key = "1234123412341234"
133
+ config.host = "custom.host"
134
+ end
135
+ end
136
+
137
+ context "on deploy(:rails_env => 'staging')" do
138
+ setup { @output = AirbrakeTasks.deploy(:rails_env => "staging") }
139
+
140
+ before_should "post to the custom host" do
141
+ URI.stubs(:parse).with('http://custom.host/deploys.txt').returns(:uri)
142
+ Net::HTTP.expects(:post_form).with(:uri, kind_of(Hash)).returns(successful_response)
143
+ end
144
+ end
145
+ end
146
+
147
+ context "when not configured" do
148
+ setup { Airbrake.configure { |config| config.api_key = "" } }
149
+
150
+ context "on deploy(:rails_env => 'staging')" do
151
+ setup { @output = AirbrakeTasks.deploy(:rails_env => "staging") }
152
+
153
+ before_should "complain about missing api key" do
154
+ AirbrakeTasks.expects(:puts).with(regexp_matches(/api key/i))
155
+ end
156
+
157
+ should "return false" do
158
+ assert !@output
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,163 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class BacktraceTest < Test::Unit::TestCase
4
+
5
+ should "parse a backtrace into lines" do
6
+ array = [
7
+ "app/models/user.rb:13:in `magic'",
8
+ "app/controllers/users_controller.rb:8:in `index'"
9
+ ]
10
+
11
+ backtrace = Airbrake::Backtrace.parse(array)
12
+
13
+ line = backtrace.lines.first
14
+ assert_equal '13', line.number
15
+ assert_equal 'app/models/user.rb', line.file
16
+ assert_equal 'magic', line.method
17
+
18
+ line = backtrace.lines.last
19
+ assert_equal '8', line.number
20
+ assert_equal 'app/controllers/users_controller.rb', line.file
21
+ assert_equal 'index', line.method
22
+ end
23
+
24
+ should "parse a windows backtrace into lines" do
25
+ array = [
26
+ "C:/Program Files/Server/app/models/user.rb:13:in `magic'",
27
+ "C:/Program Files/Server/app/controllers/users_controller.rb:8:in `index'"
28
+ ]
29
+
30
+ backtrace = Airbrake::Backtrace.parse(array)
31
+
32
+ line = backtrace.lines.first
33
+ assert_equal '13', line.number
34
+ assert_equal 'C:/Program Files/Server/app/models/user.rb', line.file
35
+ assert_equal 'magic', line.method
36
+
37
+ line = backtrace.lines.last
38
+ assert_equal '8', line.number
39
+ assert_equal 'C:/Program Files/Server/app/controllers/users_controller.rb', line.file
40
+ assert_equal 'index', line.method
41
+ end
42
+
43
+ should "be equal with equal lines" do
44
+ one = build_backtrace_array
45
+ two = one.dup
46
+ assert_equal one, two
47
+
48
+ assert_equal Airbrake::Backtrace.parse(one), Airbrake::Backtrace.parse(two)
49
+ end
50
+
51
+ should "parse massive one-line exceptions into multiple lines" do
52
+ original_backtrace = Airbrake::Backtrace.
53
+ parse(["one:1:in `one'\n two:2:in `two'\n three:3:in `three`"])
54
+ expected_backtrace = Airbrake::Backtrace.
55
+ parse(["one:1:in `one'", "two:2:in `two'", "three:3:in `three`"])
56
+
57
+ assert_equal expected_backtrace, original_backtrace
58
+ end
59
+
60
+ context "with a project root" do
61
+ setup do
62
+ @project_root = '/some/path'
63
+ Airbrake.configure {|config| config.project_root = @project_root }
64
+ end
65
+
66
+ teardown do
67
+ reset_config
68
+ end
69
+
70
+ should "filter out the project root" do
71
+ backtrace_with_root = Airbrake::Backtrace.parse(
72
+ ["#{@project_root}/app/models/user.rb:7:in `latest'",
73
+ "#{@project_root}/app/controllers/users_controller.rb:13:in `index'",
74
+ "/lib/something.rb:41:in `open'"],
75
+ :filters => default_filters)
76
+ backtrace_without_root = Airbrake::Backtrace.parse(
77
+ ["[PROJECT_ROOT]/app/models/user.rb:7:in `latest'",
78
+ "[PROJECT_ROOT]/app/controllers/users_controller.rb:13:in `index'",
79
+ "/lib/something.rb:41:in `open'"])
80
+
81
+ assert_equal backtrace_without_root, backtrace_with_root
82
+ end
83
+ end
84
+
85
+ context "with a project root equals to a part of file name" do
86
+ setup do
87
+ # Heroku-like
88
+ @project_root = '/app'
89
+ Airbrake.configure {|config| config.project_root = @project_root }
90
+ end
91
+
92
+ teardown do
93
+ reset_config
94
+ end
95
+
96
+ should "filter out the project root" do
97
+ backtrace_with_root = Airbrake::Backtrace.parse(
98
+ ["#{@project_root}/app/models/user.rb:7:in `latest'",
99
+ "#{@project_root}/app/controllers/users_controller.rb:13:in `index'",
100
+ "/lib/something.rb:41:in `open'"],
101
+ :filters => default_filters)
102
+ backtrace_without_root = Airbrake::Backtrace.parse(
103
+ ["[PROJECT_ROOT]/app/models/user.rb:7:in `latest'",
104
+ "[PROJECT_ROOT]/app/controllers/users_controller.rb:13:in `index'",
105
+ "/lib/something.rb:41:in `open'"])
106
+
107
+ assert_equal backtrace_without_root, backtrace_with_root
108
+ end
109
+ end
110
+
111
+ context "with a blank project root" do
112
+ setup do
113
+ Airbrake.configure {|config| config.project_root = '' }
114
+ end
115
+
116
+ teardown do
117
+ reset_config
118
+ end
119
+
120
+ should "not filter line numbers with respect to any project root" do
121
+ backtrace = ["/app/models/user.rb:7:in `latest'",
122
+ "/app/controllers/users_controller.rb:13:in `index'",
123
+ "/lib/something.rb:41:in `open'"]
124
+
125
+ backtrace_with_root =
126
+ Airbrake::Backtrace.parse(backtrace, :filters => default_filters)
127
+
128
+ backtrace_without_root =
129
+ Airbrake::Backtrace.parse(backtrace)
130
+
131
+ assert_equal backtrace_without_root, backtrace_with_root
132
+ end
133
+ end
134
+
135
+ should "remove notifier trace" do
136
+ inside_notifier = ['lib/airbrake.rb:13:in `voodoo`']
137
+ outside_notifier = ['users_controller:8:in `index`']
138
+
139
+ without_inside = Airbrake::Backtrace.parse(outside_notifier)
140
+ with_inside = Airbrake::Backtrace.parse(inside_notifier + outside_notifier,
141
+ :filters => default_filters)
142
+
143
+ assert_equal without_inside, with_inside
144
+ end
145
+
146
+ should "run filters on the backtrace" do
147
+ filters = [lambda { |line| line.sub('foo', 'bar') }]
148
+ input = Airbrake::Backtrace.parse(["foo:13:in `one'", "baz:14:in `two'"],
149
+ :filters => filters)
150
+ expected = Airbrake::Backtrace.parse(["bar:13:in `one'", "baz:14:in `two'"])
151
+ assert_equal expected, input
152
+ end
153
+
154
+ def build_backtrace_array
155
+ ["app/models/user.rb:13:in `magic'",
156
+ "app/controllers/users_controller.rb:8:in `index'"]
157
+ end
158
+
159
+ def default_filters
160
+ Airbrake::Configuration::DEFAULT_BACKTRACE_FILTERS
161
+ end
162
+
163
+ end