Exceptions
This small lib will play with many kinds of exceptions and return then as json with all needed informations to build awesome errors responses
needs ActiveModel
as dependency if you aren't working with rails.
Installation
Add this line to your application's Gemfile:
# direct from github
gem 'exceptions-resource', github: 'xdougx/exceptions-resource'
# from rubygems
gem 'exceptions-resource'
And then execute:
$ bundle
Or install it yourself as:
$ gem install exceptions-resource
Usage
Its simple to use for a model
redaction = Redaction.new
redaction.valid?
exception = Exceptions::Model.build(redaction)
ap exception.error # awesome print to help us
Result
{
"error": {
"model": "Redaction",
"field": "redaction[subject]",
"attribute": "subject",
"message": "can't be blank",
"full_message": "Subject can't be blank"
}
}
If you need to build a exception just with a message
exception = Exceptions::Simple.build(field: "Name", message: "is needed to be filled")
ap exception.error
Result
{
"error": {
"message": "is needed to be filled",
"full_message": "Name is needed to be filled",
"field": "Name"
}
}
Need an application exception?
exception = Exceptions::.build(message: "You have no permission to access this API")
ap exception.error
Result
{
"error": {
"message": "You have no permission to access this API"
}
}
Build your exception
class Exceptions::MyAppException
def error
{
error: {
error_code: self.error_code,
message: self.,
}
}
end
def
self.object.
end
def error_code
if self.object.error_code
end
def status
:unprocessable_entity
end
end
Contributing
- Fork it ( https://github.com/[my-github-username]/exceptions/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request