Popularity
0.5
Stable
Activity
5.8
Declining
8
1
1

Description

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: }}'"}} ```

Monthly Downloads: 69
Programming language: Ruby
License: MIT License
Tags: Error Handling     JSON     Rails    

BadJsonRequestHandler alternatives and similar gems

Based on the "Error Handling" category

Do you think we are missing an alternative of BadJsonRequestHandler or a related project?

Add another 'Error Handling' Gem

README

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 following commands to run the tests.

bundle exec appraisal install
bundle exec appraisal rake test

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.


*Note that all licence references and agreements mentioned in the BadJsonRequestHandler README section above are relevant to that project's source code only.