custom_errors_handler 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +3 -0
- data/CHANGELOG.rdoc +6 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +9 -0
- data/README.md +72 -0
- data/Rakefile +13 -0
- data/custom_errors_handler.gemspec +37 -0
- data/init.rb +1 -0
- data/lib/custom_errors_handler.rb +29 -0
- data/lib/custom_errors_handler_controller.rb +58 -0
- metadata +115 -0
- metadata.gz.sig +0 -0
data.tar.gz.sig
ADDED
data/CHANGELOG.rdoc
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Maciej Mensfeld
|
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
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# CustomErrorsHandler
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
gem install custom_error_handler
|
6
|
+
|
7
|
+
and in your Gemfile:
|
8
|
+
|
9
|
+
gem 'custom_error_handler'
|
10
|
+
|
11
|
+
## About
|
12
|
+
|
13
|
+
Custom Errors Handler is intended as an easy alternative to manage showing/rendering exceptions templates (404, 500) in Rails3.
|
14
|
+
|
15
|
+
The intent behind it was to KISS as much as it is possible. It allows you to render different 404/500 templates for different controllers/modules. If no errors templates are found, it renders default public/error_code template. It works only with Rails3 and it integrates to its middleware, swapping with default ActionDispatch::ShowExceptions.
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
The basics of Custom Errors Handler are quite simple:
|
20
|
+
|
21
|
+
It fetches all errors comming from controllers and it decides frm which file
|
22
|
+
Rails should render error.
|
23
|
+
|
24
|
+
It search throught all the directories from the place where error occured, up
|
25
|
+
to views root path.
|
26
|
+
|
27
|
+
It also checks in subdir "template" in any subdir.
|
28
|
+
|
29
|
+
## Example
|
30
|
+
|
31
|
+
First easy one:
|
32
|
+
|
33
|
+
Error 404 (not found) occurred in controller ErrorMakingController.
|
34
|
+
|
35
|
+
Custom Errors Handler searches for template called "404.erb" in following directories:
|
36
|
+
|
37
|
+
/views/error_making/layouts
|
38
|
+
/views/error_making/
|
39
|
+
/views/layouts
|
40
|
+
/views/
|
41
|
+
|
42
|
+
After it find template - it just render it.
|
43
|
+
|
44
|
+
|
45
|
+
Controller in Module:
|
46
|
+
|
47
|
+
Error 500 occurred in controller MyModule::ErrorMakingController in action index.
|
48
|
+
|
49
|
+
Our Custom Errors Handler searches in (searches for "500.erb"):
|
50
|
+
|
51
|
+
/views/my_module/error_making/layouts
|
52
|
+
/views/my_module/error_making/
|
53
|
+
/views/my_module/layouts
|
54
|
+
/views/my_module/
|
55
|
+
/views/layouts
|
56
|
+
/views/
|
57
|
+
|
58
|
+
So as you can see you can use different error templates for both controllers and modules.
|
59
|
+
|
60
|
+
## Note on Patches/Pull Requests
|
61
|
+
|
62
|
+
* Fork the project.
|
63
|
+
* Make your feature addition or bug fix.
|
64
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
65
|
+
* Commit, do not mess with Rakefile, version, or history.
|
66
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
67
|
+
* Send me a pull request. Bonus points for topic branches.
|
68
|
+
|
69
|
+
## Copyright
|
70
|
+
|
71
|
+
Copyright (c) 2011 Maciej Mensfeld. See LICENSE for details.
|
72
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('custom_errors_handler', '0.2.0') do |p|
|
6
|
+
p.description = "Custom Errors Handler is intended as an easy alternative to manage showing/rendering exceptions templates (404, 500) in Rails3"
|
7
|
+
p.url = "https://github.com/mensfeld/custom_errors_handler"
|
8
|
+
p.author = "Maciej Mensfeld"
|
9
|
+
p.email = "maciej@mensfeld.pl"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = ["rspec >=2.0.0", "rails >=3.0.0"]
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{custom_errors_handler}
|
5
|
+
s.version = "0.2.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Maciej Mensfeld"]
|
9
|
+
s.cert_chain = ["/home/mencio/.cert_keys/gem-public_cert.pem"]
|
10
|
+
s.date = %q{2011-04-10}
|
11
|
+
s.description = %q{Custom Errors Handler is intended as an easy alternative to manage showing/rendering exceptions templates (404, 500) in Rails3}
|
12
|
+
s.email = %q{maciej@mensfeld.pl}
|
13
|
+
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.md", "lib/custom_errors_handler.rb", "lib/custom_errors_handler_controller.rb"]
|
14
|
+
s.files = ["CHANGELOG.rdoc", "Gemfile", "MIT-LICENSE", "Manifest", "README.md", "Rakefile", "init.rb", "lib/custom_errors_handler.rb", "lib/custom_errors_handler_controller.rb", "custom_errors_handler.gemspec"]
|
15
|
+
s.homepage = %q{https://github.com/mensfeld/custom_errors_handler}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Custom_errors_handler", "--main", "README.md"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{custom_errors_handler}
|
19
|
+
s.rubygems_version = %q{1.5.2}
|
20
|
+
s.signing_key = %q{/home/mencio/.cert_keys/gem-private_key.pem}
|
21
|
+
s.summary = %q{Custom Errors Handler is intended as an easy alternative to manage showing/rendering exceptions templates (404, 500) in Rails3}
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
28
|
+
s.add_development_dependency(%q<rails>, [">= 3.0.0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
31
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
35
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
36
|
+
end
|
37
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'custom_errors_handler'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'custom_errors_handler_controller'
|
2
|
+
|
3
|
+
# Use our show exception dispatcher localized in errors_controller to handle showing/rendering exceptions
|
4
|
+
module MyActionDispatch
|
5
|
+
# Basicly - we do same stuff as ActionDispatch::ShowExceptions - but we render different things
|
6
|
+
class ShowExceptions < ActionDispatch::ShowExceptions
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def render_exception_with_template(env, exception)
|
11
|
+
log_error(exception)
|
12
|
+
begin
|
13
|
+
CustomErrorsHandlerController.action(rescue_responses[exception.class.name]).call(env)
|
14
|
+
rescue
|
15
|
+
render_exception_without_template(env, exception)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
alias_method_chain :render_exception, :template
|
20
|
+
end
|
21
|
+
|
22
|
+
# Swap actiondispatcher
|
23
|
+
class Railtie < Rails::Railtie
|
24
|
+
initializer "app.insert_my_errors_handler" do |app|
|
25
|
+
app.config.middleware.swap ActionDispatch::ShowExceptions, MyActionDispatch::ShowExceptions
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'action_controller'
|
3
|
+
|
4
|
+
class CustomErrorsHandlerController < ActionController::Base
|
5
|
+
# Where it should look for error templates
|
6
|
+
VALID_ERRORS_SUBDIRS = ['layouts']
|
7
|
+
|
8
|
+
ERRORS = [
|
9
|
+
:internal_server_error,
|
10
|
+
:not_found,
|
11
|
+
:unprocessable_entity
|
12
|
+
].freeze
|
13
|
+
|
14
|
+
# Handle error by creating action corresponding to error type
|
15
|
+
ERRORS.each do |e|
|
16
|
+
define_method e do
|
17
|
+
# Get path parts to check if error template exists
|
18
|
+
path = env["action_dispatch.request.path_parameters"]
|
19
|
+
path = "#{path[:controller]}/#{path[:action]}/#{path[:d]}"
|
20
|
+
respond_to do |format|
|
21
|
+
format.html { render error_layout(path, e), :status => e }
|
22
|
+
format.any { head e }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# Returns the file from which Rails should render error template
|
30
|
+
def error_layout(path, e)
|
31
|
+
e = translate_error(e)
|
32
|
+
path= path.split('/')
|
33
|
+
path.size.downto(0) do |i|
|
34
|
+
VALID_ERRORS_SUBDIRS.each { |lay_path|
|
35
|
+
template_path = File.join((path[0,i]).join('/'), lay_path, e)
|
36
|
+
return template_path if template?(template_path)
|
37
|
+
}
|
38
|
+
template_path = File.join(path[0,i], e)
|
39
|
+
return template_path if template?(template_path)
|
40
|
+
end
|
41
|
+
e
|
42
|
+
end
|
43
|
+
|
44
|
+
def template?(template)
|
45
|
+
FileTest.exist?(File.join(Rails.root, 'app', 'views', "#{template}.html.erb"))
|
46
|
+
end
|
47
|
+
|
48
|
+
# Converts "name" of error into its number
|
49
|
+
def translate_error(e)
|
50
|
+
case e
|
51
|
+
when :internal_server_error then '500'
|
52
|
+
when :not_found then '404'
|
53
|
+
when :unprocessable_entity then '422'
|
54
|
+
else '500'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: custom_errors_handler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maciej Mensfeld
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain:
|
12
|
+
- |
|
13
|
+
-----BEGIN CERTIFICATE-----
|
14
|
+
MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MQ8wDQYDVQQDDAZtYWNp
|
15
|
+
ZWoxGDAWBgoJkiaJk/IsZAEZFghtZW5zZmVsZDESMBAGCgmSJomT8ixkARkWAnBs
|
16
|
+
MB4XDTExMDQwOTA5NDcyMloXDTEyMDQwODA5NDcyMlowPzEPMA0GA1UEAwwGbWFj
|
17
|
+
aWVqMRgwFgYKCZImiZPyLGQBGRYIbWVuc2ZlbGQxEjAQBgoJkiaJk/IsZAEZFgJw
|
18
|
+
bDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL0+nG3V4/exIeiJ0IN+
|
19
|
+
wVfq8Utcu4Qpo+58EIVMIu3FiK+8w6MBvatZnUrRu12pqWLw9xrUkCiYeRErD+jF
|
20
|
+
AmdggIM/tu9CcjvURXH7VeTzOVA+pnV+eJWMD61o8HljFVcb/nyEYYVKErtr9/O4
|
21
|
+
QrIGv5lnszq1PMj2sBMy2gOP1YnzawncMLmkpp/T5SU4JZ5gAktGMRVz8RxmZzF5
|
22
|
+
6NVqFLbuqSRSU5U//WJvZVJt8dycCGgQzBM4Vi3nkOWyjIF0BANf1TqnlU2u6s8d
|
23
|
+
UK1AoDZfg5feef5e8eqoomHebX1opNGM/SOQhu3LRgax4rJfnl6VS3I2wighohsf
|
24
|
+
AgcCAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQUGlrWBqxVieAPk7NEzBDp
|
25
|
+
kM+iAMMwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IBAQAJMoyBaJs8boiz
|
26
|
+
lFpbw6MWjk+7ZhqoHpFrWEV4nzb5GzyHZ7GU/pa1fSEQR0SCs+LnTLQbAYNQyUTT
|
27
|
+
O+UsTuA7xzI//v6cSodv3Q9NbfoDlou74xv1NXorWoosQFMpVWrXv+c/1RqU3cq4
|
28
|
+
WUr+rRiveEXG4tXOwkrpX8KH8xVp2vQZcGw3AXPqhzfqDGzpHd6ws3lk+8HoSrSo
|
29
|
+
2L68tDoxraF2Z2toAg9vfFw1+mOeDk1xVIPVcBy3tJxstHfHGHlQuMiRiDQX2b2D
|
30
|
+
YYU8UWVt2841IwB5Dgl4O+atXhe9ZTBO0W32pl4Bq5CP9lhQRT1KL7sxfznJlF7Y
|
31
|
+
BH3YFsdk
|
32
|
+
-----END CERTIFICATE-----
|
33
|
+
|
34
|
+
date: 2011-04-10 00:00:00 +02:00
|
35
|
+
default_executable:
|
36
|
+
dependencies:
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.0.0
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id001
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rails
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 3.0.0
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id002
|
59
|
+
description: Custom Errors Handler is intended as an easy alternative to manage showing/rendering exceptions templates (404, 500) in Rails3
|
60
|
+
email: maciej@mensfeld.pl
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
66
|
+
- CHANGELOG.rdoc
|
67
|
+
- README.md
|
68
|
+
- lib/custom_errors_handler.rb
|
69
|
+
- lib/custom_errors_handler_controller.rb
|
70
|
+
files:
|
71
|
+
- CHANGELOG.rdoc
|
72
|
+
- Gemfile
|
73
|
+
- MIT-LICENSE
|
74
|
+
- Manifest
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- init.rb
|
78
|
+
- lib/custom_errors_handler.rb
|
79
|
+
- lib/custom_errors_handler_controller.rb
|
80
|
+
- custom_errors_handler.gemspec
|
81
|
+
has_rdoc: true
|
82
|
+
homepage: https://github.com/mensfeld/custom_errors_handler
|
83
|
+
licenses: []
|
84
|
+
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options:
|
87
|
+
- --line-numbers
|
88
|
+
- --inline-source
|
89
|
+
- --title
|
90
|
+
- Custom_errors_handler
|
91
|
+
- --main
|
92
|
+
- README.md
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: "0"
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: "1.2"
|
107
|
+
requirements: []
|
108
|
+
|
109
|
+
rubyforge_project: custom_errors_handler
|
110
|
+
rubygems_version: 1.5.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Custom Errors Handler is intended as an easy alternative to manage showing/rendering exceptions templates (404, 500) in Rails3
|
114
|
+
test_files: []
|
115
|
+
|
metadata.gz.sig
ADDED
Binary file
|