frontend_rescue 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/README.md +10 -8
- data/Rakefile +10 -1
- data/frontend_rescue.gemspec +2 -0
- data/lib/frontend_rescue/error.rb +1 -1
- data/lib/frontend_rescue/middleware.rb +31 -8
- data/lib/frontend_rescue/version.rb +1 -1
- data/test/lib/frontend_rescue/middleware_test.rb +82 -0
- data/test/lib/frontend_rescue/version_test.rb +7 -0
- data/test/mock_rack_application.rb +24 -0
- data/test/test_helper.rb +7 -0
- metadata +40 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f46c33388484b2bcfdc187c3cfcaab52ee25407d
|
4
|
+
data.tar.gz: e7c6fd26ce64f0142bdb5010c9a50641df0ba2f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 238e7f96750be12c652e7721078159fe56c5c5eb848f4915cedb25fef2992a68560070285484ea9f179bf4ec0da2cb47d7e01ba950a31ff37f62276bd9a1e0f1
|
7
|
+
data.tar.gz: ad567c6f91ab1860f41aaff23154c971d58e391265e8e60a0933722ee971bb978338553ffb80f9ce34a392f9027123797dd332fff3d9405a6dac7cf2d7e37443
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
#### Frontend Rescue
|
2
2
|
|
3
|
+
[](https://travis-ci.org/jdurand/frontend_rescue)
|
4
|
+
|
3
5
|
frontend_rescue provides a backend endpoint as a rack middleware for your frontend JavaScript application to send errors to when they’re caught.
|
4
6
|
|
5
7
|
This makes it easier to integrate your frontend stack traces to your backend analytics.
|
@@ -27,7 +29,7 @@ Or install it yourself as:
|
|
27
29
|
Use the frontend_rescue middleware :
|
28
30
|
|
29
31
|
Rails.application.configure do
|
30
|
-
config.middleware.use
|
32
|
+
config.middleware.use FrontendRescue::Middleware, paths: ['/frontend-error']
|
31
33
|
end
|
32
34
|
|
33
35
|
#### Options
|
@@ -38,7 +40,7 @@ By default, frontend_rescue will respond with a ```500 (Server Error)```.
|
|
38
40
|
|
39
41
|
You can override this value with any HTTP status code you like :
|
40
42
|
|
41
|
-
config.middleware.use
|
43
|
+
config.middleware.use FrontendRescue::Middleware, paths: ['/frontend-error'],
|
42
44
|
status_code: 200
|
43
45
|
|
44
46
|
**silent**
|
@@ -47,11 +49,15 @@ By default, frontend_rescue will output the frontend error to the logs.
|
|
47
49
|
|
48
50
|
You can pass in ```silent: true```, frontend errors are not logged. You will likely use this option when passing a block.
|
49
51
|
|
52
|
+
**exclude_user_agent**
|
53
|
+
|
54
|
+
You might want to ignore certain user agents. You can exlude user agents with a Regexp: ```exclude_user_agent: /Googlebot/```.
|
55
|
+
|
50
56
|
**&block**
|
51
57
|
|
52
58
|
You can pass in a block to frontend_rescue and it will be called and passed a FrontendRescue::Error and a Rack::Request :
|
53
59
|
|
54
|
-
config.middleware.use
|
60
|
+
config.middleware.use FrontendRescue::Middleware, paths: ['/frontend-error'],
|
55
61
|
status_code: 200,
|
56
62
|
silent: true do |error, request|
|
57
63
|
NewRelic::Agent.notice_error(error)
|
@@ -60,7 +66,7 @@ You can pass in a block to frontend_rescue and it will be called and passed a Fr
|
|
60
66
|
|
61
67
|
#### Sinatra
|
62
68
|
|
63
|
-
use
|
69
|
+
use FrontendRescue::Middleware, paths: ['/frontend-error']
|
64
70
|
|
65
71
|
With the options described above.
|
66
72
|
|
@@ -90,7 +96,3 @@ Ember example :
|
|
90
96
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
91
97
|
4. Push to the branch (`git push origin my-new-feature`)
|
92
98
|
5. Create a new Pull Request
|
93
|
-
|
94
|
-
## TODO
|
95
|
-
|
96
|
-
1. Tests...
|
data/Rakefile
CHANGED
@@ -1,2 +1,11 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/testtask'
|
2
4
|
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << 'lib/frontend_rescue'
|
7
|
+
t.test_files = FileList['test/lib/frontend_rescue/*_test.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
task default: :test
|
data/frontend_rescue.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module FrontendRescue
|
2
2
|
class Error < StandardError
|
3
3
|
def initialize(name, user_agent, message, trace)
|
4
|
-
@trace = trace.split("\n")
|
4
|
+
@trace = trace.split("\n") rescue []
|
5
5
|
@user_agent = user_agent
|
6
6
|
super "Uncaught #{name} Error: #{message}"
|
7
7
|
end
|
@@ -1,13 +1,15 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
1
3
|
module FrontendRescue
|
2
4
|
class Middleware
|
3
|
-
def initialize(app, opts, &block)
|
5
|
+
def initialize(app, opts={}, &block)
|
4
6
|
@app = app
|
5
|
-
@opts = opts
|
7
|
+
@opts = default_options.merge(opts)
|
6
8
|
@block = block
|
7
9
|
end
|
8
10
|
|
9
11
|
def call(env)
|
10
|
-
if
|
12
|
+
if frontend_error_request?(env)
|
11
13
|
handle_error Rack::Request.new(env)
|
12
14
|
else
|
13
15
|
@app.call(env)
|
@@ -21,21 +23,42 @@ module FrontendRescue
|
|
21
23
|
request.params['message'],
|
22
24
|
request.params['stack']
|
23
25
|
|
24
|
-
request.env['rack.errors'].puts "Processing #{error.class}" unless
|
26
|
+
request.env['rack.errors'].puts "Processing #{error.class}" unless silent?
|
25
27
|
|
26
28
|
if @block
|
27
29
|
@block.call(error, request)
|
28
30
|
end
|
29
31
|
|
30
|
-
unless
|
32
|
+
unless silent?
|
31
33
|
request.env['rack.errors'].puts error.message
|
32
34
|
request.env['rack.errors'].puts error.backtrace.join("\n")
|
33
35
|
request.env['rack.errors'].flush
|
34
36
|
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
38
|
+
request.env['rack.errors'].puts "Completed #{status_code} OK" unless silent?
|
39
|
+
[status_code, {}, []]
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_options
|
43
|
+
{
|
44
|
+
paths: ['/frontend-error'],
|
45
|
+
silent: false,
|
46
|
+
status_code: 500
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def frontend_error_request?(env)
|
51
|
+
env['REQUEST_METHOD'] == 'POST' &&
|
52
|
+
@opts[:paths].include?(env['PATH_INFO']) &&
|
53
|
+
(@opts[:exclude_user_agent].nil? || env['HTTP_USER_AGENT'] !~ @opts[:exclude_user_agent])
|
54
|
+
end
|
55
|
+
|
56
|
+
def silent?
|
57
|
+
@opts[:silent]
|
58
|
+
end
|
59
|
+
|
60
|
+
def status_code
|
61
|
+
@opts[:status_code]
|
39
62
|
end
|
40
63
|
|
41
64
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
describe FrontendRescue::Middleware do
|
4
|
+
let(:app) { MockRackApplication.new({'CONTENT_TYPE' => content_type, 'HTTP_USER_AGENT' => user_agent}) }
|
5
|
+
let(:middleware) { FrontendRescue::Middleware.new(app) }
|
6
|
+
let(:request) { Rack::MockRequest.new(middleware) }
|
7
|
+
let(:response) { request.post(request_path, input: post_data) }
|
8
|
+
|
9
|
+
let(:request_path) { "/some/path" }
|
10
|
+
let(:post_data) { "String or IO post data" }
|
11
|
+
let(:content_type) { "text/plain" }
|
12
|
+
let(:user_agent) { "Mozilla/5.0 (iPad; CPU OS 8_1_3 like Mac OS X)..." }
|
13
|
+
|
14
|
+
describe "when called with a POST request" do
|
15
|
+
describe "with any data" do
|
16
|
+
it "passes the request through unchanged" do
|
17
|
+
response['Content-Type'].must_equal 'text/plain'
|
18
|
+
response['Content-Length'].to_i.must_equal post_data.length
|
19
|
+
response.body.must_equal post_data
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "when called with a GET request to a given endpoint" do
|
25
|
+
let(:request_path) { "/frontend-error" }
|
26
|
+
let(:response) { request.get(request_path, input: post_data, 'CONTENT_TYPE' => 'text/plain') }
|
27
|
+
|
28
|
+
it "should not handle the request" do
|
29
|
+
response.body.wont_be_empty
|
30
|
+
end
|
31
|
+
it "should return HTTP status 200" do
|
32
|
+
response.status.must_equal 200
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "when called with a POST request to a given endpoint" do
|
37
|
+
let(:request_path) { "/frontend-error" }
|
38
|
+
|
39
|
+
describe "with any data" do
|
40
|
+
it "should handle the request" do
|
41
|
+
response.body.must_be_empty
|
42
|
+
end
|
43
|
+
it "should return HTTP status 500" do
|
44
|
+
response.status.must_equal 500
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "when a custom endpoint and HTTP code are passed" do
|
49
|
+
let(:middleware) { FrontendRescue::Middleware.new(app, status_code: 200, paths: ["/my/custom/path"]) }
|
50
|
+
let(:request_path) { "/my/custom/path" }
|
51
|
+
it "should handle the request" do
|
52
|
+
response.body.must_be_empty
|
53
|
+
end
|
54
|
+
it "should return HTTP status 200" do
|
55
|
+
response.status.must_equal 200
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "when silent option is passed" do
|
60
|
+
let(:middleware) { FrontendRescue::Middleware.new(app, silent: true) }
|
61
|
+
it "should not log to std out" do
|
62
|
+
response.errors.must_be_empty
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "when a block is passed" do
|
67
|
+
let(:middleware) { FrontendRescue::Middleware.new(app) {|e,r| r.env['rack.errors'].puts 'The answer is 42' }}
|
68
|
+
it "should be executed" do
|
69
|
+
response.errors.must_match /The\sanswer\sis\s42/
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "when exclude_user_agent is passed" do
|
74
|
+
let(:middleware) { FrontendRescue::Middleware.new(app, exclude_user_agent: /Googlebot/) }
|
75
|
+
let(:user_agent) { 'Googlebot/2.1; +http://www.google.com/bot.html...' }
|
76
|
+
it "should ignore matching user agent" do
|
77
|
+
skip("I don't know how to test this")
|
78
|
+
response.status.must_equal 404
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class MockRackApplication
|
2
|
+
def initialize(headers={})
|
3
|
+
@request_headers = default_request_headers.merge(headers)
|
4
|
+
end
|
5
|
+
|
6
|
+
def call(env)
|
7
|
+
@env = env = env.merge(@request_headers)
|
8
|
+
request_body = env['rack.input'].read
|
9
|
+
[200, {'Content-Type' => 'text/plain'}, [request_body]]
|
10
|
+
end
|
11
|
+
|
12
|
+
def [](key)
|
13
|
+
@env[key]
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def default_request_headers
|
18
|
+
{
|
19
|
+
'CONTENT_TYPE' => 'text/plain',
|
20
|
+
'HTTP_USER_AGENT' => 'Mozilla/5.0 (iPad; CPU OS 8_1_3 like Mac OS X)...'
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: frontend_rescue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Durand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -52,6 +52,34 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 5.5.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 5.5.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.10.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.10.1
|
55
83
|
description: Provides a backend endpoint for your frontend JavaScript application
|
56
84
|
to send errors to when they’re caught. This makes it easier to integrate your frontend
|
57
85
|
stack traces to your backend analytics.
|
@@ -62,6 +90,7 @@ extensions: []
|
|
62
90
|
extra_rdoc_files: []
|
63
91
|
files:
|
64
92
|
- ".gitignore"
|
93
|
+
- ".travis.yml"
|
65
94
|
- Gemfile
|
66
95
|
- LICENSE.txt
|
67
96
|
- README.md
|
@@ -71,6 +100,10 @@ files:
|
|
71
100
|
- lib/frontend_rescue/error.rb
|
72
101
|
- lib/frontend_rescue/middleware.rb
|
73
102
|
- lib/frontend_rescue/version.rb
|
103
|
+
- test/lib/frontend_rescue/middleware_test.rb
|
104
|
+
- test/lib/frontend_rescue/version_test.rb
|
105
|
+
- test/mock_rack_application.rb
|
106
|
+
- test/test_helper.rb
|
74
107
|
homepage: ''
|
75
108
|
licenses:
|
76
109
|
- MIT
|
@@ -96,5 +129,9 @@ signing_key:
|
|
96
129
|
specification_version: 4
|
97
130
|
summary: Provides a backend endpoint for your frontend JavaScript application to send
|
98
131
|
errors to when they’re caught
|
99
|
-
test_files:
|
132
|
+
test_files:
|
133
|
+
- test/lib/frontend_rescue/middleware_test.rb
|
134
|
+
- test/lib/frontend_rescue/version_test.rb
|
135
|
+
- test/mock_rack_application.rb
|
136
|
+
- test/test_helper.rb
|
100
137
|
has_rdoc:
|