request_exception_handler 0.3 → 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +19 -17
- data/Rakefile +8 -11
- data/lib/request_exception_handler.rb +17 -5
- data/test/test_helper.rb +5 -4
- metadata +61 -49
data/README.md
CHANGED
@@ -1,18 +1,16 @@
|
|
1
|
-
RequestExceptionHandler
|
2
|
-
=======================
|
1
|
+
# RequestExceptionHandler
|
3
2
|
|
4
|
-
Rails is not capable of calling
|
3
|
+
Rails is not capable of calling your exception handlers when an error occurs
|
5
4
|
during the parsing of request parameters (e.g. in case of invalid XML body).
|
6
5
|
|
7
|
-
This will hopefully change someday, but until then I have created this biutiful
|
6
|
+
This will hopefully change someday, but until then I have created this biutiful
|
8
7
|
monkey-patch for request parameter parsing to allow more flexibility when
|
9
8
|
an invalid request body is received.
|
10
9
|
|
11
|
-
|
10
|
+
Tested on 3.0, 3.1 and 3.2 it should still work on Rails 2.3 and 2.2.3 as well.
|
12
11
|
|
13
12
|
|
14
|
-
Install
|
15
|
-
=======
|
13
|
+
## Install
|
16
14
|
|
17
15
|
gem 'request_exception_handler'
|
18
16
|
|
@@ -20,15 +18,14 @@ or as a plain-old rails plugin :
|
|
20
18
|
|
21
19
|
script/plugin install git://github.com/kares/request_exception_handler.git
|
22
20
|
|
23
|
-
Example
|
24
|
-
=======
|
21
|
+
## Example
|
25
22
|
|
26
|
-
The code hooks into parameter parsing and allows a request to be constructed
|
27
|
-
even if the params can not be parsed from the submitted raw content. A before
|
28
|
-
filter is installed that checks for a request exception and re-raises it thus
|
29
|
-
it seems to Rails that the exception comes from the application code and is
|
30
|
-
processed as all other "business" exceptions.
|
31
|
-
|
23
|
+
The code hooks into parameter parsing and allows a request to be constructed
|
24
|
+
even if the params can not be parsed from the submitted raw content. A before
|
25
|
+
filter is installed that checks for a request exception and re-raises it thus
|
26
|
+
it seems to Rails that the exception comes from the application code and is
|
27
|
+
processed as all other "business" exceptions.
|
28
|
+
you might skip this filter and install your own to handle such cases (it's good
|
32
29
|
to make sure the filter gets to the beginning of the chain) :
|
33
30
|
|
34
31
|
class MyController < ApplicationController
|
@@ -51,7 +48,7 @@ to make sure the filter gets to the beginning of the chain) :
|
|
51
48
|
end
|
52
49
|
|
53
50
|
Another option of how to modify the returned 500 status is to use exception
|
54
|
-
handlers the same way
|
51
|
+
handlers the same way you're (hopefully) using them for your own exceptions :
|
55
52
|
|
56
53
|
class ApplicationController < ActionController::Base
|
57
54
|
|
@@ -61,9 +58,14 @@ handlers the same way You're (hopefully) using them for Your own exceptions :
|
|
61
58
|
|
62
59
|
end
|
63
60
|
|
64
|
-
If
|
61
|
+
If you're not using REXML as a parsing backend the exception might vary, e.g.
|
65
62
|
for Nokogiri the rescue block would look something like :
|
66
63
|
|
67
64
|
rescue_from 'Nokogiri::XML::SyntaxError' do |exception|
|
68
65
|
render :text => exception.to_s, :status => 422
|
69
66
|
end
|
67
|
+
|
68
|
+
## Copyright
|
69
|
+
|
70
|
+
Copyright (c) 2009-2012 [Karol Bucek](https://github.com/kares).
|
71
|
+
See LICENSE (http://www.apache.org/licenses/LICENSE-2.0) for details.
|
data/Rakefile
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
|
2
|
-
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
1
|
+
#!/usr/bin/env rake
|
4
2
|
|
5
3
|
desc 'Default: run unit tests.'
|
6
4
|
task :default => :test
|
7
5
|
|
6
|
+
require 'rake/testtask'
|
8
7
|
desc 'Test the request_exception_handler plugin.'
|
9
8
|
Rake::TestTask.new(:test) do |t|
|
10
9
|
t.libs << 'lib'
|
@@ -12,11 +11,9 @@ Rake::TestTask.new(:test) do |t|
|
|
12
11
|
t.verbose = true
|
13
12
|
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
-
end
|
14
|
+
begin
|
15
|
+
require 'bundler/gem_helper'
|
16
|
+
Bundler::GemHelper.class_eval { def version_tag; "#{version}"; end }
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
rescue LoadError
|
19
|
+
end
|
@@ -1,8 +1,11 @@
|
|
1
|
-
|
1
|
+
# The mixin that provides the +request_exception+
|
2
|
+
# by default included into +ActionController::Base+
|
2
3
|
module RequestExceptionHandler
|
3
4
|
|
5
|
+
THREAD_LOCAL_NAME = :_request_exception
|
6
|
+
|
4
7
|
@@parse_request_parameters_exception_handler = lambda do |request, exception|
|
5
|
-
Thread.current[
|
8
|
+
Thread.current[THREAD_LOCAL_NAME] = exception
|
6
9
|
request_body = request.respond_to?(:body) ? request.body : request.raw_post
|
7
10
|
|
8
11
|
logger = RequestExceptionHandler.logger
|
@@ -25,12 +28,19 @@ module RequestExceptionHandler
|
|
25
28
|
{ "body" => request_body, "content_type" => content_type, "content_length" => request.content_length }
|
26
29
|
end
|
27
30
|
|
28
|
-
|
31
|
+
begin
|
32
|
+
mattr_accessor :parse_request_parameters_exception_handler
|
33
|
+
rescue NoMethodError => e
|
34
|
+
require('active_support/core_ext/module/attribute_accessors') && retry
|
35
|
+
raise e
|
36
|
+
end
|
29
37
|
|
38
|
+
# Resets the current +request_exception+ (to nil).
|
30
39
|
def self.reset_request_exception
|
31
|
-
Thread.current[
|
40
|
+
Thread.current[THREAD_LOCAL_NAME] = nil
|
32
41
|
end
|
33
42
|
|
43
|
+
# Retrieves the Rails logger.
|
34
44
|
def self.logger
|
35
45
|
defined?(Rails.logger) ? Rails.logger :
|
36
46
|
defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER :
|
@@ -41,14 +51,16 @@ module RequestExceptionHandler
|
|
41
51
|
base.prepend_before_filter :check_request_exception
|
42
52
|
end
|
43
53
|
|
54
|
+
# Checks and raises a +request_exception+ (gets prepended as a before filter).
|
44
55
|
def check_request_exception
|
45
56
|
e = request_exception
|
46
57
|
raise e if e && e.is_a?(Exception)
|
47
58
|
end
|
48
59
|
|
60
|
+
# Retrieves and keeps track of the current request exception if any.
|
49
61
|
def request_exception
|
50
62
|
return @_request_exception if defined? @_request_exception
|
51
|
-
@_request_exception = Thread.current[
|
63
|
+
@_request_exception = Thread.current[THREAD_LOCAL_NAME]
|
52
64
|
RequestExceptionHandler.reset_request_exception
|
53
65
|
@_request_exception
|
54
66
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
gem 'test-unit' rescue nil
|
2
3
|
require 'test/unit'
|
3
4
|
|
4
5
|
# enable testing with different version of rails via argv :
|
@@ -15,15 +16,15 @@ version =
|
|
15
16
|
if version
|
16
17
|
RAILS_VERSION = version
|
17
18
|
gem 'activesupport', "= #{RAILS_VERSION}"
|
18
|
-
gem 'activerecord', "= #{RAILS_VERSION}"
|
19
|
+
#gem 'activerecord', "= #{RAILS_VERSION}"
|
19
20
|
gem 'actionpack', "= #{RAILS_VERSION}"
|
20
|
-
gem 'actionmailer', "= #{RAILS_VERSION}"
|
21
|
+
#gem 'actionmailer', "= #{RAILS_VERSION}"
|
21
22
|
gem 'rails', "= #{RAILS_VERSION}"
|
22
23
|
else
|
23
24
|
gem 'activesupport'
|
24
|
-
gem 'activerecord'
|
25
|
+
#gem 'activerecord'
|
25
26
|
gem 'actionpack'
|
26
|
-
gem 'actionmailer'
|
27
|
+
#gem 'actionmailer'
|
27
28
|
gem 'rails'
|
28
29
|
end
|
29
30
|
|
metadata
CHANGED
@@ -1,54 +1,66 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_exception_handler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 13
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 0
|
8
|
-
- 3
|
9
|
-
version: "0.3"
|
5
|
+
version: "0.4"
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
|
-
- Karol Bucek
|
8
|
+
- Karol Bucek
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
18
|
-
default_executable:
|
13
|
+
date: 2012-11-15 00:00:00 Z
|
19
14
|
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "2.1"
|
23
|
+
requirement: *id001
|
24
|
+
prerelease: false
|
25
|
+
type: :runtime
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
requirement: *id002
|
35
|
+
prerelease: false
|
36
|
+
type: :development
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: test-unit
|
39
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "2.4"
|
45
|
+
requirement: *id003
|
46
|
+
prerelease: false
|
47
|
+
type: :development
|
35
48
|
description: a rails hook that allows one to handle request parameter parsing exceptions (invalid XML, JSON) with a rescue block
|
36
49
|
email:
|
37
|
-
- self@kares.org
|
50
|
+
- self@kares.org
|
38
51
|
executables: []
|
39
52
|
|
40
53
|
extensions: []
|
41
54
|
|
42
55
|
extra_rdoc_files:
|
43
|
-
- README.md
|
56
|
+
- README.md
|
44
57
|
files:
|
45
|
-
- lib/request_exception_handler.rb
|
46
|
-
- LICENSE
|
47
|
-
- README.md
|
48
|
-
- Rakefile
|
49
|
-
- test/
|
50
|
-
- test/
|
51
|
-
has_rdoc: true
|
58
|
+
- lib/request_exception_handler.rb
|
59
|
+
- LICENSE
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- test/test_helper.rb
|
63
|
+
- test/request_exception_handler_test.rb
|
52
64
|
homepage: http://github.com/kares/request_exception_handler
|
53
65
|
licenses: []
|
54
66
|
|
@@ -56,32 +68,32 @@ post_install_message:
|
|
56
68
|
rdoc_options: []
|
57
69
|
|
58
70
|
require_paths:
|
59
|
-
- lib
|
71
|
+
- lib
|
60
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
73
|
none: false
|
62
74
|
requirements:
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 2
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
69
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
82
|
none: false
|
71
83
|
requirements:
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 2
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
78
90
|
requirements: []
|
79
91
|
|
80
92
|
rubyforge_project: "[none]"
|
81
|
-
rubygems_version: 1.
|
93
|
+
rubygems_version: 1.8.24
|
82
94
|
signing_key:
|
83
95
|
specification_version: 3
|
84
|
-
summary: handler for all
|
96
|
+
summary: handler for all request (parsing) related exceptions
|
85
97
|
test_files:
|
86
|
-
- test/
|
87
|
-
- test/
|
98
|
+
- test/test_helper.rb
|
99
|
+
- test/request_exception_handler_test.rb
|