rambulance 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 146d016881be5aa21f7d33de3bab927491c06160
4
- data.tar.gz: d0ef5bc64965f81c3834865772875b399161a074
3
+ metadata.gz: 6b34e6b878ba63db8822a5fc882cfe096e0fa24d
4
+ data.tar.gz: be1dafb554fdad7ac32ad9c350ebde70dae19b95
5
5
  SHA512:
6
- metadata.gz: f9459c8e769ae84353aac43085acb710c64fb94f469fe28530fe0797988592ae38eff50f3512e0ad15b36c300940013e441eed8c1f000cc714a046d52a887dfa
7
- data.tar.gz: 0761811510d0c91d4efc7c6c4446dc2bd6e71f95a2f7876d046ec034a4d4ee052148a6ae4ce13f28b688c5ce278da3c75f65a5d666acb5f17338118d90146210
6
+ metadata.gz: 2654f7f3939613f5d17ff0a68b7fc02debc9c23e38b63bd5ce2ebaa6a92fea0184e41a31ec4f82a37a16fce12a4d7d094cb4e8177f0d3bbbf83996582fec3c2c
7
+ data.tar.gz: f930880f7303f539dc2d740efeac7d5f28929c1b3be4928678b1c4988489f7aa76245eec7faf4bc38d8c4898aa501db404ef4449f726850bc9f61702e1b9cecd
@@ -12,6 +12,7 @@ gemfile:
12
12
  - gemfiles/rails_32.gemfile
13
13
  - gemfiles/rails_40.gemfile
14
14
  - gemfiles/rails_41.gemfile
15
+ - gemfiles/rails_42.gemfile
15
16
  - gemfiles/rails_edge.gemfile
16
17
 
17
18
  matrix:
data/Appraisals CHANGED
@@ -16,6 +16,14 @@ appraise "rails_41" do
16
16
  gem "railties", "~> 4.1.0"
17
17
  end
18
18
 
19
+ appraise "rails_42" do
20
+ gem "activesupport", "~> 4.2.0.beta1"
21
+ gem "actionpack", "~> 4.2.0.beta1"
22
+ gem "railties", "~> 4.2.0.beta1"
23
+
24
+ gem 'minitest', '~> 5.3.4'
25
+ end
26
+
19
27
  appraise "rails_edge" do
20
28
  git 'git://github.com/rails/rails.git' do
21
29
  gem "activesupport", require: 'active_support'
@@ -24,5 +32,4 @@ appraise "rails_edge" do
24
32
  end
25
33
 
26
34
  gem 'minitest', '~> 5.3.4'
27
- gem 'rspec-rails', '2.99.0.beta1'
28
35
  end
data/README.md CHANGED
@@ -30,7 +30,7 @@ And then execute:
30
30
  $ rails g rambulance:install
31
31
  ```
32
32
 
33
- Now you can start editing templates like `app/views/errors/not_found.html.erb`. Edit, run `rails server` and open [`localhost:3000/rambulance/not_found`](http://localhost:3000/rambulance/404)!
33
+ Now you can start editing templates like `app/views/errors/not_found.html.erb`. Edit, run `rails server` and open [`localhost:3000/rambulance/not_found`](http://localhost:3000/rambulance/not_found)!
34
34
 
35
35
  ## Setting Pairs of Exceptions and HTTP Statuses
36
36
 
@@ -67,8 +67,8 @@ This page tells all the error pages as well as all the pairs of exceptions/corre
67
67
  ### Open `localhost:3000/rambulance/***` in Your Browser
68
68
 
69
69
  Just go to one of the error pages via Rambulance:
70
- * [`localhost:3000/rambulance/not_found`](http://localhost:3000/rambulance/404) or
71
- * [`localhost:3000/rambulance/internal_server_error`](http://localhost:3000/rambulance/500)
70
+ * [`localhost:3000/rambulance/not_found`](http://localhost:3000/rambulance/not_found) or
71
+ * [`localhost:3000/rambulance/internal_server_error`](http://localhost:3000/rambulance/internal_server_error)
72
72
 
73
73
  This is useful when you want to edit templates without changing Rails configuration.
74
74
 
data/Rakefile CHANGED
@@ -3,9 +3,7 @@ require "bundler/gem_tasks"
3
3
  require "rspec/core"
4
4
  require "rspec/core/rake_task"
5
5
 
6
- RSpec::Core::RakeTask.new(:spec) do |spec|
7
- spec.pattern = FileList['spec/**/*_spec.rb']
8
- end
6
+ RSpec::Core::RakeTask.new(:spec)
9
7
 
10
8
  task default: "spec:all"
11
9
 
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activesupport", "~> 4.2.0.beta1"
6
+ gem "actionpack", "~> 4.2.0.beta1"
7
+ gem "railties", "~> 4.2.0.beta1"
8
+ gem "minitest", "~> 5.3.4"
9
+
10
+ gemspec :path => "../"
@@ -9,6 +9,5 @@ git "git://github.com/rails/rails.git" do
9
9
  end
10
10
 
11
11
  gem "minitest", "~> 5.3.4"
12
- gem "rspec-rails", "2.99.0.beta1"
13
12
 
14
13
  gemspec :path => "../"
@@ -3,7 +3,15 @@ module Rambulance
3
3
  class InstallGenerator < Rails::Generators::Base
4
4
  source_root File.expand_path('../templates', __FILE__)
5
5
 
6
- class_option :template_engine, type: :string, aliases: '-e', desc: 'Template engine for the views. Available options are "erb" and "haml".'
6
+ class_option :template_engine,
7
+ type: :string,
8
+ aliases: '-e',
9
+ desc: 'Template engine for the views. Available options are "erb" and "haml".'
10
+
11
+ class_option :error_layout,
12
+ type: :string,
13
+ aliases: '-l',
14
+ desc: 'Copies app/views/layout/application.html.erb to the specified layout name'
7
15
 
8
16
  def self.banner #:nodoc:
9
17
  <<-BANNER.chomp
@@ -28,13 +36,15 @@ BANNER
28
36
  end
29
37
 
30
38
  def copy_layout #:nodoc:
31
- say "\ncopying app/views/layouts/application.html.#{template_engine} to app/views/layouts/error.html.#{template_engine}:"
32
- copy_file Rails.root.join("app/views/layouts/application.html.#{template_engine}"), "app/views/layouts/error.html.#{template_engine}"
39
+ return if layout_name == "application"
40
+
41
+ say "\n" "copying app/views/layouts/application.html.#{template_engine} to app/views/layouts/#{layout_name}.html.#{template_engine}:"
42
+ copy_file Rails.root.join("app/views/layouts/application.html.#{template_engine}"), "app/views/layouts/#{layout_name}.html.#{template_engine}"
33
43
  end
34
44
 
35
45
  def copy_initializer #:nodoc:
36
- say "\ngenerating initializer:"
37
- copy_file "rambulance.rb", "config/initializers/rambulance.rb"
46
+ say "\n" "generating initializer:"
47
+ template "rambulance.rb", "config/initializers/rambulance.rb"
38
48
  end
39
49
 
40
50
  private
@@ -42,6 +52,14 @@ BANNER
42
52
  def template_engine
43
53
  options[:template_engine].try(:to_s).try(:downcase) || 'erb'
44
54
  end
55
+
56
+ def layout_name
57
+ options[:error_layout].try(:to_s).try(:downcase) || "application"
58
+ end
59
+
60
+ def longest_error_name_size
61
+ ActionDispatch::ExceptionWrapper.rescue_responses.keys.sort_by(&:size).last.size
62
+ end
45
63
  end
46
64
  end
47
65
  end
@@ -3,20 +3,11 @@ Rambulance.setup do |config|
3
3
  # List of pairs of exception/corresponding http status. In Rails, the default
4
4
  # mappings are below:
5
5
  #
6
- # "ActionController::RoutingError" => :not_found,
7
- # "AbstractController::ActionNotFound" => :not_found,
8
- # "ActionController::MethodNotAllowed" => :method_not_allowed,
9
- # "ActionController::UnknownHttpMethod" => :method_not_allowed,
10
- # "ActionController::NotImplemented" => :not_implemented,
11
- # "ActionController::UnknownFormat" => :not_acceptable,
12
- # "ActionController::InvalidAuthenticityToken" => :unprocessable_entity,
13
- # "ActionDispatch::ParamsParser::ParseError" => :bad_request,
14
- # "ActionController::BadRequest" => :bad_request,
15
- # "ActionController::ParameterMissing" => :bad_request,
16
- # "ActiveRecord::RecordNotFound" => :not_found,
17
- # "ActiveRecord::StaleObjectError" => :conflict,
18
- # "ActiveRecord::RecordInvalid" => :unprocessable_entity,
19
- # "ActiveRecord::RecordNotSaved" => :unprocessable_entity
6
+ <%=
7
+ ActionDispatch::ExceptionWrapper.rescue_responses.map do |error_class, status|
8
+ " # #{error_class.ljust(longest_error_name_size)} => :#{status}"
9
+ end.join(",\n")
10
+ %>
20
11
  #
21
12
  # If you add exceptions in this config, Rambulance uses the pairs you defined
22
13
  # here *in addition* to the default maddings. You can also override the default
@@ -31,9 +22,9 @@ Rambulance.setup do |config|
31
22
  }
32
23
 
33
24
  # The template name for the layout of the error pages. The default value is
34
- # 'error'. For exmaple, if this value is set to "error_page", Rambulance uses
25
+ # 'application'. For exmaple, if this value is set to "error_page", Rambulance uses
35
26
  # 'app/views/layout/error_page.html.erb' as a layout for all the error pages.
36
- config.layout_name = "error"
27
+ config.layout_name = "<%= layout_name %>"
37
28
 
38
29
  # The directry name to organize error page templates. The default value is
39
30
  # 'errors'. For exmaple, if this value is set to "error_pages", Rambulance
@@ -9,7 +9,7 @@ module Rambulance
9
9
 
10
10
  # The name of the layout file for the error pages.
11
11
  mattr_accessor :layout_name
12
- @@layout_name = "error"
12
+ @@layout_name = "application"
13
13
 
14
14
  # The directry name to organize error page templates.
15
15
  mattr_accessor :view_path
@@ -23,6 +23,10 @@ module Rambulance
23
23
  action(status_in_words).call(env)
24
24
  end
25
25
 
26
+ def self.local_prefixes
27
+ [Rambulance.view_path]
28
+ end if ActionPack::VERSION::STRING >= "4.2.0"
29
+
26
30
  ERROR_HTTP_STATUSES.values.each do |status_in_words|
27
31
  eval <<-ACTION
28
32
  def #{status_in_words}
@@ -33,6 +37,16 @@ module Rambulance
33
37
 
34
38
  private
35
39
 
40
+ def process_action(*)
41
+ begin
42
+ request.GET
43
+ rescue bad_request_exception
44
+ env["MALFORMED_QUERY_STRING"], env["QUERY_STRING"] = env["QUERY_STRING"], ""
45
+ end
46
+
47
+ super
48
+ end
49
+
36
50
  def send_action(name, *args)
37
51
  @_status = env["PATH_INFO"][1..-1].to_i
38
52
  @_response.status = @_status
@@ -60,5 +74,11 @@ module Rambulance
60
74
  def controller_path
61
75
  Rambulance.view_path
62
76
  end
77
+
78
+ def bad_request_exception
79
+ ActionController::BadRequest
80
+ rescue NameError
81
+ TypeError # Rails 3.2 doesn't know about ActionController::BadRequest
82
+ end
63
83
  end
64
84
  end
@@ -1,3 +1,3 @@
1
1
  module Rambulance
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "tzinfo"
28
28
  spec.add_development_dependency "jbuilder"
29
29
  spec.add_development_dependency "rspec"
30
- spec.add_development_dependency "rspec-rails", '~> 2.14.1'
30
+ spec.add_development_dependency "rspec-rails"
31
31
  spec.add_development_dependency "capybara"
32
32
  spec.add_development_dependency "appraisal"
33
33
  end
@@ -25,7 +25,9 @@ end
25
25
  class CustomException < StandardError; end
26
26
 
27
27
  Rambulance.setup do |config|
28
+ config.layout_name = "error"
28
29
  config.rescue_responses = {
30
+ 'TypeError' => :bad_request,
29
31
  'CustomException' => :not_found
30
32
  }
31
33
  end
@@ -53,6 +55,8 @@ class UsersController < ApplicationController
53
55
  def new
54
56
  raise ActionController::InvalidAuthenticityToken
55
57
  end
58
+
59
+ def create; end
56
60
  end
57
61
 
58
62
  # helpers
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  require 'spec_helper'
3
3
 
4
- feature 'Error json responses', if: !ENV["CUSTOM_EXCEPTIONS_APP"] do
4
+ feature 'Error json response', if: !ENV["CUSTOM_EXCEPTIONS_APP"], type: :request do
5
5
  def json_response
6
6
  JSON.parse(response.body)
7
7
  end
@@ -10,31 +10,31 @@ feature 'Error json responses', if: !ENV["CUSTOM_EXCEPTIONS_APP"] do
10
10
  super(path, nil, "CONTENT_TYPE" => "application/json", "HTTP_ACCEPT" => "application/json")
11
11
  end
12
12
 
13
- scenario 'Unprocessable entity due to ActionController:InvalidAuthenticityToken but without its template' do
13
+ scenario 'returns 422 json due to ActionController:InvalidAuthenticityToken but without its template' do
14
14
  get '/users/new'
15
15
 
16
- response.status.should == 422
17
- json_response['message'].should == "Something went wrong"
16
+ expect(response.status).to eq(422)
17
+ expect(json_response['message']).to eq("Something went wrong")
18
18
  end
19
19
 
20
- scenario 'Internal server error due to RuntimeError' do
20
+ scenario 'returns 500 json due to RuntimeError' do
21
21
  get '/users/1.json'
22
22
 
23
- response.status.should == 500
24
- json_response['message'].should == "Something went wrong"
23
+ expect(response.status).to eq(500)
24
+ expect(json_response['message']).to eq("Something went wrong")
25
25
  end
26
26
 
27
- scenario 'Not found due to CustomException' do
27
+ scenario 'returns 404 json due to CustomException' do
28
28
  get '/users.json'
29
29
 
30
- response.status.should == 404
31
- json_response['message'].should == "Page not found"
30
+ expect(response.status).to eq(404)
31
+ expect(json_response['message']).to eq("Page not found")
32
32
  end
33
33
 
34
- scenario 'Not found due to ActinoController::RoutingError' do
34
+ scenario 'returns 404 json due to ActinoController::RoutingError' do
35
35
  get '/doesnt_exist.json'
36
36
 
37
- response.status.should == 404
38
- json_response['message'].should == "Page not found"
37
+ expect(response.status).to eq(404)
38
+ expect(json_response['message']).to eq("Page not found")
39
39
  end
40
40
  end
@@ -1,42 +1,58 @@
1
1
  # encoding: UTF-8
2
2
  require 'spec_helper'
3
3
 
4
- feature 'Error pages' do
4
+ feature 'Error page' do
5
5
  shared_examples_for "an action that renders 500 page if the template is missing" do
6
- scenario 'Unprocessable entity due to ActionController:InvalidAuthenticityToken but without its template' do
6
+ scenario 'displays 500 page and return 422 Bad Request status' do
7
7
  visit '/users/new'
8
8
 
9
- page.status_code.should == 422
10
- page.body.should have_content "Error page"
11
- page.body.should have_content "Something went wrong."
9
+ expect(page.status_code).to eq(422)
10
+ expect(page.body).to have_content "Error page"
11
+ expect(page.body).to have_content "Something went wrong."
12
12
  end
13
13
  end
14
14
 
15
- context 'with the default exceptions app', if: !ENV["CUSTOM_EXCEPTIONS_APP"] do
15
+ context 'generated by the default exceptions app', if: !ENV["CUSTOM_EXCEPTIONS_APP"] do
16
16
  it_behaves_like "an action that renders 500 page if the template is missing"
17
17
 
18
- scenario 'Internal server error due to RuntimeError' do
18
+ scenario 'displays 500 page due to RuntimeError' do
19
19
  visit '/users/1'
20
20
 
21
- page.status_code.should == 500
22
- page.body.should have_content "Error page"
23
- page.body.should have_content "Something went wrong."
21
+ expect(page.status_code).to eq(500)
22
+ expect(page.body).to have_content "Error page"
23
+ expect(page.body).to have_content "Something went wrong."
24
24
  end
25
25
 
26
- scenario 'Not found due to CustomException' do
26
+ scenario 'displays 404 page due to CustomException' do
27
27
  visit '/users'
28
28
 
29
- page.status_code.should == 404
30
- page.body.should have_content "Error page"
31
- page.body.should have_content "Page not found."
29
+ expect(page.status_code).to eq(404)
30
+ expect(page.body).to have_content "Error page"
31
+ expect(page.body).to have_content "Page not found."
32
32
  end
33
33
 
34
- scenario 'Not found due to ActinoController::RoutingError' do
34
+ scenario 'displays 404 page due to ActionController::RoutingError' do
35
35
  visit '/doesnt_exist'
36
36
 
37
- page.status_code.should == 404
38
- page.body.should have_content "Error page"
39
- page.body.should have_content "Page not found."
37
+ expect(page.status_code).to eq(404)
38
+ expect(page.body).to have_content "Error page"
39
+ expect(page.body).to have_content "Page not found."
40
+ end
41
+
42
+ scenario 'displays 400 page when malformed parameters' do
43
+ page.driver.post '/users?x[y]=1&x[y][][w]=2'
44
+
45
+ expect(page.status_code).to eq(400)
46
+ expect(page.body).to have_content "Error page"
47
+ expect(page.body).to have_content "Bad request."
48
+ end
49
+
50
+ scenario 'displays 404 page for non-existing page when malformed parameters' do
51
+ page.driver.post '/doesnt_exist?x[y]=1&x[y][][w]=2'
52
+
53
+ expect(page.status_code).to eq(404)
54
+ expect(page.body).to have_content "Error page"
55
+ expect(page.body).to have_content "Page not found."
40
56
  end
41
57
 
42
58
  context "with a custom layout name" do
@@ -47,27 +63,27 @@ feature 'Error pages' do
47
63
 
48
64
  scenario 'uses the custom layout' do
49
65
  visit '/doesnt_exist'
50
- page.body.should have_content "Application page"
66
+ expect(page.body).to have_content "Application page"
51
67
  end
52
68
  end
53
69
  end
54
70
 
55
- context "With a custom exception app", if: ENV["CUSTOM_EXCEPTIONS_APP"] do
71
+ context "generated by a custom exception app", if: ENV["CUSTOM_EXCEPTIONS_APP"] do
56
72
  it_behaves_like "an action that renders 500 page if the template is missing"
57
73
 
58
- scenario 'Not found due to ActinoController::RoutingError' do
74
+ scenario 'displays 404 page due to ActinoController::RoutingError' do
59
75
  visit '/doesnt_exist'
60
76
 
61
- page.status_code.should == 404
62
- page.body.should have_content "Error page"
63
- page.body.should have_content "Page not found."
77
+ expect(page.status_code).to eq(404)
78
+ expect(page.body).to have_content "Error page"
79
+ expect(page.body).to have_content "Page not found."
64
80
  end
65
81
 
66
- scenario 'Internal server error due to RuntimeError' do
82
+ scenario 'displays 500 page due to RuntimeError' do
67
83
  visit '/users/1'
68
84
 
69
- page.status_code.should == 500
70
- page.body.should have_content "Custom error page"
85
+ expect(page.status_code).to eq(500)
86
+ expect(page.body).to have_content "Custom error page"
71
87
  end
72
88
  end
73
89
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rambulance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Nishijima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-05 00:00:00.000000000 Z
11
+ date: 2014-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -126,16 +126,16 @@ dependencies:
126
126
  name: rspec-rails
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - "~>"
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: 2.14.1
131
+ version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - "~>"
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 2.14.1
138
+ version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: capybara
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -183,6 +183,7 @@ files:
183
183
  - gemfiles/rails_32.gemfile
184
184
  - gemfiles/rails_40.gemfile
185
185
  - gemfiles/rails_41.gemfile
186
+ - gemfiles/rails_42.gemfile
186
187
  - gemfiles/rails_edge.gemfile
187
188
  - lib/generators/rambulance/exceptions_app_generator.rb
188
189
  - lib/generators/rambulance/install_generator.rb
@@ -208,6 +209,7 @@ files:
208
209
  - lib/rambulance/railtie.rb
209
210
  - lib/rambulance/version.rb
210
211
  - rambulance.gemspec
212
+ - spec/fake_app/app/views/errors/bad_request.html.erb
211
213
  - spec/fake_app/app/views/errors/internal_server_error.html.erb
212
214
  - spec/fake_app/app/views/errors/internal_server_error.json.jbuilder
213
215
  - spec/fake_app/app/views/errors/not_found.html.erb
@@ -244,6 +246,7 @@ signing_key:
244
246
  specification_version: 4
245
247
  summary: Simple and safe way to dynamically generate error pages
246
248
  test_files:
249
+ - spec/fake_app/app/views/errors/bad_request.html.erb
247
250
  - spec/fake_app/app/views/errors/internal_server_error.html.erb
248
251
  - spec/fake_app/app/views/errors/internal_server_error.json.jbuilder
249
252
  - spec/fake_app/app/views/errors/not_found.html.erb