public_exception_extension 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +37 -0
- data/Rakefile +27 -0
- data/lib/public_exception_extension.rb +61 -0
- data/lib/public_exception_extension/railtie.rb +11 -0
- data/lib/public_exception_extension/version.rb +5 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6e0c3b1661f8a237893bb8ce00185b9a95c03e5b25e6d3a46e47c5bb73ce430
|
4
|
+
data.tar.gz: 0a50631186326f2e822016153a55b3eb29eda80fd217eabbf6ee4e767391ec47
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3bfad3fdb0408a9b7099d762a65f76bd5f5f5e9563fff7a8d123e4a2d25ce346fe400410d13c612a4125def81580ea227310bb5444167ad2e8331ce8d4e8f3ca
|
7
|
+
data.tar.gz: 2b703d55e388af0bd62f03b1dcfd06d504b1b8a7c22c57eeedca21ddf13d314be34a3493fe65ad4fba4bf05e1c99835f9085641b6640913ea22c0d26ae575a77
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Jun0kada
|
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/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# PublicExceptionExtension
|
2
|
+
Custom public error template
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
```ruby
|
6
|
+
# config/initializers/public_exception.rb
|
7
|
+
|
8
|
+
Rails.application.config.public_exception_template = -> (request, status) do
|
9
|
+
if request.original_fullpath.start_with?('/admin')
|
10
|
+
"/admin/#{status}.html" # => render /public/admin/xxx.html
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
```
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'public_exception_extension'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
```bash
|
25
|
+
$ bundle
|
26
|
+
```
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
```bash
|
30
|
+
$ gem install public_exception_extension
|
31
|
+
```
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
Contribution directions go here.
|
35
|
+
|
36
|
+
## License
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'PublicExceptionExtension'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'public_exception_extension/railtie'
|
4
|
+
|
5
|
+
module PublicExceptionExtension
|
6
|
+
def call(env)
|
7
|
+
request = ActionDispatch::Request.new(env)
|
8
|
+
status = request.path_info[1..-1].to_i
|
9
|
+
|
10
|
+
begin
|
11
|
+
content_type = request.formats.first
|
12
|
+
rescue Mime::Type::InvalidMimeType
|
13
|
+
content_type = Mime[:text]
|
14
|
+
end
|
15
|
+
|
16
|
+
body = { status: status, error: Rack::Utils::HTTP_STATUS_CODES.fetch(status, Rack::Utils::HTTP_STATUS_CODES[500]) }
|
17
|
+
|
18
|
+
render(request, status, content_type, body)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def render(request, status, content_type, body)
|
24
|
+
format = "to_#{content_type.to_sym}" if content_type
|
25
|
+
|
26
|
+
if format && body.respond_to?(format)
|
27
|
+
render_format(status, content_type, body.public_send(format))
|
28
|
+
else
|
29
|
+
render_html(request, status)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def render_format(status, content_type, body)
|
34
|
+
[
|
35
|
+
status,
|
36
|
+
{
|
37
|
+
"Content-Type" => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}",
|
38
|
+
"Content-Length" => body.bytesize.to_s,
|
39
|
+
},
|
40
|
+
[body],
|
41
|
+
]
|
42
|
+
end
|
43
|
+
|
44
|
+
def render_html(request, status)
|
45
|
+
if Rails.application.config.respond_to?(:public_exception_template)
|
46
|
+
file_path = Rails.application.config.public_exception_template.call(request, status)
|
47
|
+
path = File.join(public_path, file_path) if file_path
|
48
|
+
end
|
49
|
+
|
50
|
+
path ||= ''
|
51
|
+
|
52
|
+
path = "#{public_path}/#{status}.#{I18n.locale}.html" unless (found = File.exist?(path))
|
53
|
+
path = "#{public_path}/#{status}.html" unless (found = File.exist?(path))
|
54
|
+
|
55
|
+
if found || File.exist?(path)
|
56
|
+
render_format(status, "text/html", File.read(path))
|
57
|
+
else
|
58
|
+
[404, { "X-Cascade" => "pass" }, []]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PublicExceptionExtension
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
initializer 'public_exception_extension' do
|
6
|
+
ActiveSupport.on_load :action_controller do
|
7
|
+
ActionDispatch::PublicExceptions.prepend(PublicExceptionExtension)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: public_exception_extension
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jun0kada
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
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
|
+
description: Custom public error template
|
42
|
+
email:
|
43
|
+
- jun.0kada.dev@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/public_exception_extension.rb
|
52
|
+
- lib/public_exception_extension/railtie.rb
|
53
|
+
- lib/public_exception_extension/version.rb
|
54
|
+
homepage: https://github.com/Jun0kada/public_exception_extension
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.0.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Custom public error template
|
77
|
+
test_files: []
|