Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
jsonapi_errors_handler
Advanced tools
A convienient way to serialize errors in Jsonapi standard
Add this line to your application's Gemfile:
gem 'jsonapi_errors_handler'
And then execute:
$ bundle
Or install it yourself as:
$ gem install jsonapi_errors_handler
In your controller:
include JsonapiErrorsHandler
rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
From this point you'll have default html errors being serialized. JsonapiErrorsHandler offers 4 predefined errors:
If you rise any of errors above in any place of your application, client gets the nicely formatted error message instead of 500
If you want to handle all the errors in your API application to deliver nicely formatted JSON response about 500 instead crashing the server, add this when your application loads:
require 'jsonapi_errors_handler'
JsonapiErrorsHandler.configure do |config|
config.handle_unexpected = true
end
If you want to change the response content type you can do it through the configuration setting content_type
by default it is application/vnd.api+json
require 'jsonapi_errors_handler'
JsonapiErrorsHandler.configure do |config|
config.content_type = 'application/json'
end
If you want your custom errors being handled by default, just add them to the mapper
include JsonapiErrorsHandler
ErrorMapper.map_errors!({
'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound'
})
rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
To handle validation errors from ActiveRecord or ActiveModel, you need to write custom error handler:
rescue_from ActiveRecord::RecordInvalid, with: lambda { |e| handle_validation_error(e) }
rescue_from ActiveModel::ValidationError, with: lambda { |e| handle_validation_error(e) }
def handle_validation_error(error)
error_model = error.try(:model) || error.try(:record)
messages = error_model.errors.messages
mapped = JsonapiErrorsHandler::Errors::Invalid.new(errors: messages)
render_error(mapped)
end
When you'll include the jsonapi_errors_handler
to your controller, all errors will be handled and delivered to the client in the nice, formatted
way.
However, you'd probably like to have a way to log the risen error on your own to send notifications to developers that something unexpected happened.
To do so, just implement the log_error
method in your controller, that accepts the risen error as an argument.
def log_error(error)
#do the fancy logging here
end
By default, we deliver hardcoded responses. You can check out the defined error classes for details
If you want to have custom error responses being delivered, just create your own Api::Errors
that inherits from JsonapiErrorsHandler::StandardError
If you want to localize your responses, just create a class:
module Api::Errors
class Forbidden < ::JsonapiErrorsHandler::Errors::StandardError
def initialize(*)
super(
title: I18n.t('api.errors.forbidden.title'),
status: 403,
detail: I18n.t('api.errors.forbidden.detail'),
source: { pointer: '/request/headers/authorization' }
)
end
end
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/driggl/jsonapi_errors_handler.
How to contribute:
Fork repository
Install Rubocop - make sure you run it before commiting changes
Commit changes
Create Pull Request
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that jsonapi_errors_handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.