Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/prathamesh-sonpatki/bad_json_request_handler
Catch bad JSON requests to your application and return valid JSON response back
https://github.com/prathamesh-sonpatki/bad_json_request_handler
error-handling json rails
Last synced: 14 days ago
JSON representation
Catch bad JSON requests to your application and return valid JSON response back
- Host: GitHub
- URL: https://github.com/prathamesh-sonpatki/bad_json_request_handler
- Owner: prathamesh-sonpatki
- License: mit
- Created: 2020-02-29T17:07:03.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-23T07:50:21.000Z (about 4 years ago)
- Last Synced: 2024-10-04T17:24:25.329Z (about 1 month ago)
- Topics: error-handling, json, rails
- Language: Ruby
- Homepage: https://prathamesh.tech
- Size: 43 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# BadJsonRequestHandler
![Gem Version][gem-version-img]
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:
```ruby
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.```rb
Rails.application.config.middleware.insert_after ActionDispatch::DebugExceptions, BadJsonRequestHandler::Middleware
```
## DevelopmentAfter checking out the repo, run following commands to run the tests.
```rb
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](https://github.com/prathamesh-sonpatki/bad_json_request_handler/blob/master/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## 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](https://github.com/prathamesh-sonpatki/bad_json_request_handler/blob/master/CODE_OF_CONDUCT.md).
[gem-version-img]: https://badge.fury.io/rb/bad_json_request_handler.svg