Skip to content
No description or website provided.
Ruby Shell
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
bin
lib
test
.gitignore
.travis.yml
CHANGELOG.md
CODE_OF_CONDUCT.md
Gemfile
Gemfile.lock
LICENSE.txt
README.md
Rakefile
_config.yml
bad_json_request_handler.gemspec

README.md

BadJsonRequestHandler

Gem Version

You have a Rails API endpoint which expects JSON request payload. But some clients send invalid JSON requests. This typically results in following error.

ActionDispatch::Http::Parameters::ParseError: 767: unexpected token at '{ event: { seconds: }}'

This results in a 500 error from the server. But client does not get an idea that the request payload was invalid. What if you can instead respond with 400 Bad Request response which clearly tells the client that the request is invalid. They may start sending valid requests going forward!

That's where this gem comes into the picture. It captures the Parameters::ParseError and returns a Bad Request response back. Once you use the initialize from this gem, the response will be as follows.

HTTP 400
{"errors"=>{"message"=>"Invalid request payload: 767: unexpected token at '{ event: { seconds: }}'"}}

Installation

Add this line to your application's Gemfile:

gem 'bad_json_request_handler'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install bad_json_request_handler

Usage

This gem automatically includes BadJsonRequestHandler middleware in your Rails application by adding it to the middleware stack as follows.

Rails.application.config.middleware.insert_after ActionDispatch::DebugExceptions, BadJsonRequestHandler::Middleware

Development

After checking out the repo, run rake test to run the tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/prathamesh-sonpatki/bad_json_request_handler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the BadJsonRequestHandler project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

You can’t perform that action at this time.