exceptions_app 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +18 -0
- data/exceptions_app.gemspec +21 -0
- data/features/rake_task_integration.feature +13 -0
- data/features/step_definitions/rails_steps.rb +7 -0
- data/features/support/env.rb +7 -0
- data/lib/exceptions_app.rb +18 -0
- data/lib/exceptions_app/config.rb +11 -0
- data/lib/exceptions_app/railtie.rb +14 -0
- data/lib/exceptions_app/simple_response.rb +26 -0
- data/lib/exceptions_app/version.rb +3 -0
- data/lib/tasks/exceptions_app.rake +15 -0
- data/test/lib/config_test.rb +11 -0
- data/test/lib/exceptions_app_test.rb +22 -0
- data/test/lib/simple_response_test.rb +28 -0
- data/test/test_helper.rb +7 -0
- metadata +94 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Igor Kuznetsov
|
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,60 @@
|
|
1
|
+
[![BuildStatus](https://travis-ci.org/kaize/exceptions_app.png?branch=master)](https://travis-ci.org/kaize/exceptions_app)
|
2
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/kaize/exceptions_app)
|
3
|
+
|
4
|
+
# ExceptionsApp
|
5
|
+
|
6
|
+
`exceptions_app` is a gem, that provides a simple way of handling exceptions in Rails applications.
|
7
|
+
It changes the default *PublicExceptions* rack middleware to *SimpleResponse*. It adds a Rake task "rake gen:static_pages" that generates static pages for your custom exceptions.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'exceptions_app'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install exceptions_app
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Configuring
|
26
|
+
|
27
|
+
*ExceptionsApp* have method __configure__, that yelds block inside itself. You can change the behaviour by adding this code to **initializers/exceptions_app.rb**
|
28
|
+
|
29
|
+
ExceptionsApp.configure do |config|
|
30
|
+
config.envs = ["production", "another_production"]
|
31
|
+
config.errors_path = ["/404"]
|
32
|
+
config.statics_path = "path_for_statc"
|
33
|
+
end
|
34
|
+
|
35
|
+
There are several options that can be changed in initializer:
|
36
|
+
|
37
|
+
- `envs` - Array of environments in which default *exceptions_app* should be changed to gem's middleware
|
38
|
+
- `errors_path` - Array of route paths from which custom errors pages will be generated
|
39
|
+
- `statics_path` - String path (directory) where to put generated static pages
|
40
|
+
|
41
|
+
### Preparations
|
42
|
+
|
43
|
+
*ExceptionsApp* is a simple Railtie that changes default *exceptions_app* in environments, mentioned in *envs* config option, to *SimpleResponse* rack middleware. So, to generate correct static error pages you should make custom error pages by yourself. It will be a great idea to make your own *ErrorsController* to handle exceptions and generate errors pages. The example of such controller you can see in *features* directory with cucumber integration tests.
|
44
|
+
|
45
|
+
### How to work with it
|
46
|
+
|
47
|
+
*ExceptionsApp* is ready to use after *bundle install*. It has default config options, so if you won't change them and won't provide any custom pages it will by default generate static error pages from default Rails 404, 500 and 422 html pages in public directory. In fact it won't change application's behaviour if you won't configure it. Default config options are:
|
48
|
+
|
49
|
+
ExceptionsApp.config.envs = ["production"]
|
50
|
+
ExceptionsApp.config.errors_path = ["/404", "/500", "/422"]
|
51
|
+
ExceptionsApp.config.statics_path = "public"
|
52
|
+
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
require "cucumber/rake/task"
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.test_files = FileList['test/lib/**/*.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
Cucumber::Rake::Task.new(:cucumber) do |t|
|
12
|
+
t.fork = true
|
13
|
+
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
|
14
|
+
end
|
15
|
+
|
16
|
+
task test_suite: [:test, :cucumber]
|
17
|
+
|
18
|
+
task :default => :test_suite
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'exceptions_app/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "exceptions_app"
|
8
|
+
gem.version = ExceptionsApp::VERSION
|
9
|
+
gem.authors = ["Igor Kuznetsov"]
|
10
|
+
gem.email = ["igkuznetsov@gmail.com"]
|
11
|
+
gem.description = %q{exceptions_app is gem, that provides a simple way of handling exceptions in Rails applications.}
|
12
|
+
gem.summary = %q{exceptions_app provides a simple way of handling exceptions in Rails applications}
|
13
|
+
gem.homepage = "http://github.com/kaize/exceptions_app"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "rails", "~> 3.2.9"
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
@disable_bundler
|
2
|
+
Feature: Rake task for generating static pages
|
3
|
+
should be added into rails application
|
4
|
+
|
5
|
+
Background:
|
6
|
+
Given I successfully run `bundle exec rails new testapp --skip-bundle --skip-sprockets`
|
7
|
+
And I cd to "testapp"
|
8
|
+
And I add "exceptions_app" from this project as a dependency
|
9
|
+
And I successfully run `bundle install`
|
10
|
+
|
11
|
+
Scenario: Check the existence of rake task
|
12
|
+
When I successfully run `bundle exec rake -T --trace`
|
13
|
+
Then the output should contain "rake gen:static_pages"
|
@@ -0,0 +1,7 @@
|
|
1
|
+
When /^I add "([^"]+)" from this project as a dependency$/ do |gem_name|
|
2
|
+
append_to_file('Gemfile', %{gem "#{gem_name}", :path => "#{PROJECT_ROOT}"\n})
|
3
|
+
end
|
4
|
+
|
5
|
+
When /^I add "([^"]+)" as a dependency$/ do |gem_name|
|
6
|
+
append_to_file('Gemfile', %{gem "#{gem_name}"\n})
|
7
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "exceptions_app/version"
|
2
|
+
|
3
|
+
module ExceptionsApp
|
4
|
+
|
5
|
+
autoload :SimpleResponse, 'exceptions_app/simple_response'
|
6
|
+
autoload :Config, 'exceptions_app/config'
|
7
|
+
|
8
|
+
def self.configure
|
9
|
+
yield self.config
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.config
|
13
|
+
@config ||= Config.new
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'exceptions_app/railtie' if defined?(Rails)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ExceptionsApp
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
|
4
|
+
initializer "exceptions_app.install_app" do |app|
|
5
|
+
if ExceptionsApp.config.envs.include?(Rails.env)
|
6
|
+
app.config.exceptions_app = ExceptionsApp::SimpleResponse.new
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
rake_tasks do
|
11
|
+
load 'tasks/exceptions_app.rake'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ExceptionsApp
|
2
|
+
class SimpleResponse
|
3
|
+
|
4
|
+
def initialize(headers = nil)
|
5
|
+
@headers = headers
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
status = env["PATH_INFO"][1..-1]
|
10
|
+
@headers ||= {}
|
11
|
+
statics_path = "#{ExceptionsApp.config.statics_path}/#{status}.html"
|
12
|
+
|
13
|
+
if File.exists?(statics_path)
|
14
|
+
render(status, File.read(statics_path))
|
15
|
+
else
|
16
|
+
[status, @headers.merge!({"X-Cascade" => "pass"}), []]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def render(status, body)
|
23
|
+
[status, {'Content-Length' => body.bytesize.to_s}, [body]]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
namespace :gen do
|
2
|
+
|
3
|
+
desc "Generate static error pages"
|
4
|
+
task :static_pages => :environment do
|
5
|
+
app = ActionDispatch::Integration::Session.new Rails.application
|
6
|
+
ExceptionsApp.config.errors_path.each do |path|
|
7
|
+
app.get path
|
8
|
+
response = app.response
|
9
|
+
unless response.body.empty?
|
10
|
+
File.open("#{ExceptionsApp.config.statics_path}/#{response.status}.html", "w") { |file| file.write(response.body) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
puts "Static pages generated successfully"
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ExceptionsAppTest < TestCase
|
4
|
+
|
5
|
+
def test_configuration_block_correct_object
|
6
|
+
ExceptionsApp.configure do |config|
|
7
|
+
assert_instance_of ExceptionsApp::Config, config
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_overwrite_default_configuration
|
12
|
+
environments = ["prod#1", "prod#2"]
|
13
|
+
errors_path = ["123", "456"]
|
14
|
+
ExceptionsApp.configure do |config|
|
15
|
+
config.envs = environments
|
16
|
+
config.errors_path = errors_path
|
17
|
+
end
|
18
|
+
assert_equal environments, ExceptionsApp.config.envs
|
19
|
+
assert_equal errors_path, ExceptionsApp.config.errors_path
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SimpleResponseTest < TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@env = {}
|
7
|
+
@env["PATH_INFO"] = "/404"
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_initializing_without_headers
|
11
|
+
sr = ExceptionsApp::SimpleResponse.new
|
12
|
+
response = sr.call(@env)
|
13
|
+
assert_equal "404", response[0]
|
14
|
+
assert_equal "pass", response[1]["X-Cascade"]
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_initializing_with_headers
|
18
|
+
sr = ExceptionsApp::SimpleResponse.new({"ContentLength" => 0})
|
19
|
+
response = sr.call(@env)
|
20
|
+
assert_equal 0, response[1]["ContentLength"]
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_empty_body
|
24
|
+
sr = ExceptionsApp::SimpleResponse.new
|
25
|
+
response = sr.call(@env)
|
26
|
+
assert_nil response[3]
|
27
|
+
end
|
28
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: exceptions_app
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Igor Kuznetsov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.9
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.9
|
30
|
+
description: exceptions_app is gem, that provides a simple way of handling exceptions
|
31
|
+
in Rails applications.
|
32
|
+
email:
|
33
|
+
- igkuznetsov@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- exceptions_app.gemspec
|
44
|
+
- features/rake_task_integration.feature
|
45
|
+
- features/step_definitions/rails_steps.rb
|
46
|
+
- features/support/env.rb
|
47
|
+
- lib/exceptions_app.rb
|
48
|
+
- lib/exceptions_app/config.rb
|
49
|
+
- lib/exceptions_app/railtie.rb
|
50
|
+
- lib/exceptions_app/simple_response.rb
|
51
|
+
- lib/exceptions_app/version.rb
|
52
|
+
- lib/tasks/exceptions_app.rake
|
53
|
+
- test/lib/config_test.rb
|
54
|
+
- test/lib/exceptions_app_test.rb
|
55
|
+
- test/lib/simple_response_test.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
homepage: http://github.com/kaize/exceptions_app
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
hash: -4290187780574975558
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: -4290187780574975558
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: exceptions_app provides a simple way of handling exceptions in Rails applications
|
87
|
+
test_files:
|
88
|
+
- features/rake_task_integration.feature
|
89
|
+
- features/step_definitions/rails_steps.rb
|
90
|
+
- features/support/env.rb
|
91
|
+
- test/lib/config_test.rb
|
92
|
+
- test/lib/exceptions_app_test.rb
|
93
|
+
- test/lib/simple_response_test.rb
|
94
|
+
- test/test_helper.rb
|