invalid_json_error_middleware 0.0.2
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.
- checksums.yaml +7 -0
- data/README.md +42 -0
- data/lib/invalid_json_error_middleware.rb +44 -0
- data/lib/invalid_json_error_middleware/version.rb +3 -0
- data/spec/invalid_json_error_middleware_spec.rb +78 -0
- data/spec/spec_helper.rb +19 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d64ab061dbd3ab808d0780de3364a890196190f78c591cc432aeb53981ec0665
|
4
|
+
data.tar.gz: a4a4ffed8c403d819324e219b4f4d4e4ec341d0849ea578b74467b6f9284bf6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 683a3d700a8bfd527acbd677e7c32ded9a6ec87d553d0a1a942081b1fdff5bf0698e68534de47d571cb70f08cb2b5bca84b3d3d9d4467d76064fd88f91fd0f0e
|
7
|
+
data.tar.gz: d62ab2792d51c46919d48e12ea96d880c2db71982bb18c1418f74c08398783dd5c3ce38b7179a31f1910b7f7094acffb8020fd82fec77720c87f34b68b751827
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Rails rack middleware to catch JSON parse errors
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'invalid_json_error_middleware'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install invalid_json_error_middleware
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
config/application.rb
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Application < Rails::Application
|
25
|
+
config.middleware.use InvalidJsonErrorMiddleware
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
For using additional filter checks or/and custom error message:
|
30
|
+
```ruby
|
31
|
+
class Application < Rails::Application
|
32
|
+
config.middleware.use InvalidJsonErrorMiddleware, filter_handler: ->(env) { env['HTTP_ACCEPT'] =~ /json/ }, error_handler: ->(error) { "Custom error message: #{error}" }
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Snick555/invalid_json_error_middleware. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class InvalidJsonErrorMiddleware
|
2
|
+
def initialize(app, filter_handler: ->(_) { true }, error_handler: ->(error) { error_message(error) })
|
3
|
+
@app = app
|
4
|
+
@filter_handler = filter_handler
|
5
|
+
@error_handler = error_handler
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
begin
|
10
|
+
@app.call(env)
|
11
|
+
rescue parse_error_class => error
|
12
|
+
if raise_custom_error?(env)
|
13
|
+
return [
|
14
|
+
400, { 'Content-Type' => 'application/json' },
|
15
|
+
[{ status: 400, error: @error_handler.call(error) }.to_json]
|
16
|
+
]
|
17
|
+
else
|
18
|
+
raise error
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def parse_error_class
|
26
|
+
if defined?(ActionDispatch::Http::Parameters::ParseError)
|
27
|
+
ActionDispatch::Http::Parameters::ParseError
|
28
|
+
else
|
29
|
+
ActionDispatch::ParamsParser::ParseError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def raise_custom_error?(env)
|
34
|
+
@filter_handler.call(env) && content_type?(env)
|
35
|
+
end
|
36
|
+
|
37
|
+
def content_type?(env)
|
38
|
+
env['CONTENT_TYPE'] =~ /application\/json/
|
39
|
+
end
|
40
|
+
|
41
|
+
def error_message(error)
|
42
|
+
"There was a problem in the JSON you submitted: #{error}"
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'InvalidJsonErrorMiddleware' do
|
4
|
+
let(:test_app) { double('Test Rack App') }
|
5
|
+
let(:error_message) { 'some error message' }
|
6
|
+
|
7
|
+
before do
|
8
|
+
allow(test_app).to receive(:call).and_raise(ActionDispatch::Http::Parameters::ParseError.new(error_message))
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'app without additional params' do
|
12
|
+
let(:app) { InvalidJsonErrorMiddleware.new(test_app) }
|
13
|
+
|
14
|
+
it 'raises default error when CONTENT_TYPE header is not application/json' do
|
15
|
+
expect {
|
16
|
+
post '/', {}, { 'CONTENT_TYPE' => 'text/html' }
|
17
|
+
}.to raise_error(ActionDispatch::Http::Parameters::ParseError, error_message)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'raises custom error when CONTENT_TYPE header is application/json' do
|
21
|
+
post '/', {}, { 'CONTENT_TYPE' => 'application/json' }
|
22
|
+
|
23
|
+
expect(last_response.status).to eql 400
|
24
|
+
expect(last_response.content_type).to eql 'application/json'
|
25
|
+
expect(last_response.body).to match error_message
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'raises custom error when undefined ActionDispatch::Http::Parameters::ParseError' do
|
29
|
+
allow(test_app).to receive(:call).and_raise(ActionDispatch::ParamsParser::ParseError.new(error_message))
|
30
|
+
allow(app).to receive(:parse_error_class).and_return(ActionDispatch::ParamsParser::ParseError)
|
31
|
+
|
32
|
+
post '/', {}, { 'CONTENT_TYPE' => 'application/json' }
|
33
|
+
|
34
|
+
expect(last_response.status).to eql 400
|
35
|
+
expect(last_response.content_type).to eql 'application/json'
|
36
|
+
expect(last_response.body).to match error_message
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'app with additional filter param' do
|
41
|
+
let(:app) { InvalidJsonErrorMiddleware.new(test_app, filter_handler: filter_handler) }
|
42
|
+
|
43
|
+
context 'when additional filter param true' do
|
44
|
+
let(:filter_handler) { ->(_) { true } }
|
45
|
+
|
46
|
+
it 'raises custom error' do
|
47
|
+
post '/', {}, { 'CONTENT_TYPE' => 'application/json' }
|
48
|
+
|
49
|
+
expect(last_response.status).to eql 400
|
50
|
+
expect(last_response.content_type).to eql 'application/json'
|
51
|
+
expect(last_response.body).to match error_message
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when additional filter param false' do
|
56
|
+
let(:filter_handler) { ->(_) { false } }
|
57
|
+
|
58
|
+
it 'raises default error' do
|
59
|
+
expect {
|
60
|
+
post '/', {}, { 'CONTENT_TYPE' => 'application/json' }
|
61
|
+
}.to raise_error(ActionDispatch::Http::Parameters::ParseError, error_message)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'app with additional error param' do
|
67
|
+
let(:custom_error_message) { 'Some custom error message' }
|
68
|
+
let(:app) { InvalidJsonErrorMiddleware.new(test_app, error_handler: ->(_) { custom_error_message }) }
|
69
|
+
|
70
|
+
it 'raises custom error' do
|
71
|
+
post '/', {}, { 'CONTENT_TYPE' => 'application/json' }
|
72
|
+
|
73
|
+
expect(last_response.status).to eql 400
|
74
|
+
expect(last_response.content_type).to eql 'application/json'
|
75
|
+
expect(last_response.body).to match custom_error_message
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rack/test'
|
2
|
+
require 'json'
|
3
|
+
require 'invalid_json_error_middleware'
|
4
|
+
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
module ActionDispatch
|
8
|
+
module ParamsParser
|
9
|
+
class ParseError < Exception
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Http
|
14
|
+
module Parameters
|
15
|
+
class ParseError < Exception
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: invalid_json_error_middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ihor Voloshyn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
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: rack-test
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Rails rack middleware to catch JSON parse errors
|
56
|
+
email: ivoloshy@cisco.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README.md
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- lib/invalid_json_error_middleware.rb
|
64
|
+
- lib/invalid_json_error_middleware/version.rb
|
65
|
+
- spec/invalid_json_error_middleware_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
homepage: https://github.com/Cisco-AMP/invalid_json_error_middleware
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.7.8
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Invalid Json Error Middleware
|
91
|
+
test_files: []
|