simple_errors 0.0.1
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/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +2 -0
- data/lib/generators/simple_errors/USAGE +10 -0
- data/lib/generators/simple_errors/simple_errors_generator.rb +25 -0
- data/lib/generators/simple_errors/templates/404.html.erb +1 -0
- data/lib/generators/simple_errors/templates/500.html.erb +1 -0
- data/lib/generators/simple_errors/templates/error.html.erb +3 -0
- data/lib/simple_errors.rb +4 -0
- data/lib/simple_errors/rescue.rb +76 -0
- data/lib/simple_errors/version.rb +3 -0
- data/simple_errors.gemspec +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 07c4fdfabfc7951d7ecae5da3a1c4f22f5a279b1
|
4
|
+
data.tar.gz: 5e52770ae6dcb327e5ccf5dd47fb32d6c9646713
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 608b210fe517c7eb6aa2e67b7ccca0c3a4d413f15afbb04c5f7b3a102c7805a8c569e5865aa1cd364d4cf570598b70f58825c2eab2417a9ba33adde1c36ecfb4
|
7
|
+
data.tar.gz: 622a949c311932714a912964f2f90c50cb3857206490248a3a29e1aaa2f3916f5519c16df9a43b5944e196ec77acd0f58b068a6295105c00384595e5c008f93c
|
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
|
+
.idea
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Ed Jones
|
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,81 @@
|
|
1
|
+
# SimpleErrors - a simple way to rescue errors in Rails applications
|
2
|
+
|
3
|
+
On practically every job we rescue errors in the ApplicationController in the same way, so it was time for a gem. This gem rescues from defined error classes (along with the standard Rails ActiveRecordNotFound and RoutingError classes) to either a 404 or 500 error page.
|
4
|
+
|
5
|
+
If you need something more complicated than that (e.g. 403 errors), you still need to rescue in ApplicationController. They're not mutually exclusive, though.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'simple_errors'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install simple_errors
|
20
|
+
|
21
|
+
## Configuration and setup
|
22
|
+
|
23
|
+
There's a generator to set up the files you need, and add a mixin in the application controller:
|
24
|
+
|
25
|
+
rails generate simple_errors
|
26
|
+
|
27
|
+
This will set up:
|
28
|
+
|
29
|
+
* /app/views/layouts/error.html.erb - a layout for errors.
|
30
|
+
* /app/views/errors/500.html.erb - the 500 error page
|
31
|
+
* /app/views/errors/404.html.erb - the 404 error page
|
32
|
+
|
33
|
+
It also removes the default error pages in /public.
|
34
|
+
|
35
|
+
## Configuring `ApplicationController`
|
36
|
+
|
37
|
+
After you've run the generator, your Application Controller will look like this:
|
38
|
+
|
39
|
+
```
|
40
|
+
class ApplicationController < ActionController::Base
|
41
|
+
include SimpleErrors::Rescue
|
42
|
+
# other stuff
|
43
|
+
end
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
There are a couple of class methods provided by this gem to configure it.
|
48
|
+
|
49
|
+
### Rescuing from other classes
|
50
|
+
If your application raises errors of other types, you might want to rescue to 404 instead of 500. Do that by adding a call to `rescue_with_not_found`, passing one or many classes:
|
51
|
+
|
52
|
+
```
|
53
|
+
class ApplicationController < ActionController::Base
|
54
|
+
rescue_with_not_found Rooftop::RecordNotFoundError, Rooftop::Rails::AncestorMismatch
|
55
|
+
include SimpleErrors::Rescue
|
56
|
+
# other stuff
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
### Doing something before rescuing
|
61
|
+
Sometime you want your error page to render something - for example, call the same method as a before_filter does to get navigation items.
|
62
|
+
|
63
|
+
```
|
64
|
+
class ApplicationController < ActionController::Base
|
65
|
+
rescue_with_not_found Rooftop::RecordNotFoundError, Rooftop::Rails::AncestorMismatch
|
66
|
+
include SimpleErrors::Rescue
|
67
|
+
before_rescue do
|
68
|
+
@foo = SomeMenu.find(1234)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
The `before_rescue` block is evaluated in the context of the rendering call, so if you set @foo in the example above it'll be available in your view.
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
1. Fork it ( https://github.com/errorstudio/simple_errors/fork )
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class SimpleErrorsGenerator < Rails::Generators::Base
|
2
|
+
source_root File.expand_path("../templates", __FILE__)
|
3
|
+
desc "This generator adds error page views and layout to your application."
|
4
|
+
def copy_layout
|
5
|
+
copy_file "error.html.erb", "app/views/layouts/error.html.erb"
|
6
|
+
end
|
7
|
+
|
8
|
+
def copy_error_pages
|
9
|
+
%w(404.html.erb 500.html.erb).each do |file|
|
10
|
+
copy_file file, "app/views/errors/#{file}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def include_simple_errors
|
15
|
+
inject_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::Base\n" do <<-RUBY
|
16
|
+
include SimpleErrors::Rescue
|
17
|
+
RUBY
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def remove_default_files
|
22
|
+
remove_file 'public/404.html'
|
23
|
+
remove_file 'public/500.html'
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<p>This is the 404 error page, available at <%= __FILE__ %>.</p>
|
@@ -0,0 +1 @@
|
|
1
|
+
<p>This is the 500 error page, available at <%= __FILE__ %>.</p>
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module SimpleErrors
|
2
|
+
# A mixin for ApplicationController which rescues from common errors. If you have specific ones you want to rescue
|
3
|
+
# with a 404, call the class method rescue_with_not_found, passing one or more error classes
|
4
|
+
module Rescue
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
@@rescue_with_not_found_from ||= []
|
9
|
+
rescue_from StandardError do |e|
|
10
|
+
#if we're considering all requests to be local, raise the error
|
11
|
+
if ::Rails.application.config.consider_all_requests_local
|
12
|
+
raise e
|
13
|
+
end
|
14
|
+
|
15
|
+
if e.class.in?(@@rescue_with_not_found_from + [ActiveRecord::RecordNotFound, ActionController::RoutingError])
|
16
|
+
render_not_found(e)
|
17
|
+
else
|
18
|
+
render_error(e)
|
19
|
+
end
|
20
|
+
|
21
|
+
# If rollbar is defined, send a message to it.
|
22
|
+
if defined?(Rollbar)
|
23
|
+
Rollbar.error(e, rollbar_request_data, rollbar_person_data)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class_methods do
|
30
|
+
def rescue_with_not_found(*klasses)
|
31
|
+
@@rescue_with_not_found_from ||= []
|
32
|
+
@@rescue_with_not_found_from << klasses
|
33
|
+
@@rescue_with_not_found_from.flatten!
|
34
|
+
end
|
35
|
+
|
36
|
+
def before_rescue(&block)
|
37
|
+
@@before_rescue = block
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
def render_not_found(exception = nil)
|
44
|
+
call_before_rescue_block
|
45
|
+
@exception = exception
|
46
|
+
respond_to do |format|
|
47
|
+
format.html do
|
48
|
+
render 'errors/404', status: 404, layout: "layouts/error"
|
49
|
+
end
|
50
|
+
format.all do
|
51
|
+
render nothing: true, status: 404
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def render_error(exception = nil)
|
57
|
+
call_before_rescue_block
|
58
|
+
@exception = exception
|
59
|
+
respond_to do |format|
|
60
|
+
format.html do
|
61
|
+
render 'errors/500', status: 500, layout: 'layouts/error'
|
62
|
+
end
|
63
|
+
format.all do
|
64
|
+
render nothing: true, status: 500
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def call_before_rescue_block
|
70
|
+
if defined?(@@before_rescue) && @@before_rescue.is_a?(Proc)
|
71
|
+
instance_eval(&@@before_rescue)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_errors/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_errors"
|
8
|
+
spec.version = SimpleErrors::VERSION
|
9
|
+
spec.authors = ["Ed Jones"]
|
10
|
+
spec.email = ["ed@errorstudio.co.uk"]
|
11
|
+
spec.summary = %q{A simple way to rescue errors in rails apps}
|
12
|
+
spec.description = %q{We're forever writing the same rescue code for our Rails apps. This gem will hopefully stop that :-)}
|
13
|
+
spec.homepage = "https://github.com/errorstudio/simple_errors"
|
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
|
+
|
24
|
+
spec.add_dependency "rails", "~> 4"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_errors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ed Jones
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-04 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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4'
|
55
|
+
description: We're forever writing the same rescue code for our Rails apps. This gem
|
56
|
+
will hopefully stop that :-)
|
57
|
+
email:
|
58
|
+
- ed@errorstudio.co.uk
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/generators/simple_errors/USAGE
|
69
|
+
- lib/generators/simple_errors/simple_errors_generator.rb
|
70
|
+
- lib/generators/simple_errors/templates/404.html.erb
|
71
|
+
- lib/generators/simple_errors/templates/500.html.erb
|
72
|
+
- lib/generators/simple_errors/templates/error.html.erb
|
73
|
+
- lib/simple_errors.rb
|
74
|
+
- lib/simple_errors/rescue.rb
|
75
|
+
- lib/simple_errors/version.rb
|
76
|
+
- simple_errors.gemspec
|
77
|
+
homepage: https://github.com/errorstudio/simple_errors
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: A simple way to rescue errors in rails apps
|
101
|
+
test_files: []
|