SmartError
Simple gem that can handle Exceptions, ActiveRecord Errors and Custom Errors and return readable message and error number/code as usable Hash.
Table of Contents generated with DocToc
- SmartError
- Installation
- Usage
- Handle integer as CustomError
- Handle ApplicationRecord as ModelError
- Handle ActiveRecord child as ModelError
- handle Exception as ExceptionError
- Override the message with the provided message
- Merge
extra
array to message - Merge
extra
string to message - Merge
extra
with custom message - Show First Model Errors by default
- Show Model Error details by invalid keys and messages and convert messages to sentence
- Contributing
- License
- Code of Conduct
Installation
Add this line to your application’s Gemfile:
gem 'smart_error'
And then execute:
$ bundle
Or install it yourself as:
$ gem install smart_error
Usage
First of all will need to have predefined list of error messages Like: errors
Handle integer as CustomError
SmartError.handle(1001).to_h
# {:message=>"Unauthorized", :details=>{}, :error_url=>"1001", :error_code=>1001}
SmartError.handle(1001).to_json
# "{\"message\":\"Unauthorized\",\"details\":{},\"error_url\":\"1001\",\"error_code\":1001}"
SmartError.handle(1001, options).as_json
# {"message"=>"Unauthorized", "details"=>{}, "error_url"=>"1001", "error_code"=>1001}
Handle ApplicationRecord as ModelError
SmartError.handle(Employee.new).to_h
# {:message=>"", :details=>{}, :error_url=>"1010", :error_code=>1010}
Handle ActiveRecord child as ModelError
SmartError.handle(ApplicationRecord.new).to_h
handle Exception as ExceptionError
SmartError.handle(NoMethodError.new).to_h
# {:message=>"NoMethodError", :details=>{}, :error_url=>"1000", :error_code=>1000}
Override the message with the provided message
message = 'Custom Message'
SmartError.handle(1001, message: message).to_h
# {:message=>"Custom Message", :details=>{}, :error_url=>"1001", :error_code=>1001}
Merge extra
array to message
extra = %i[username password country]
error = SmartError.handle(1002, extra: extra)
error.to_h
# {:message=>"Not Found: username, password, and country", :details=>{}, :error_url=>"1002", :error_code=>1002}
Merge extra
string to message
extra = 'you are noy authorized for this action'
error = SmartError.handle(1001, extra: extra)
error.to_h
# {:message=>"Unauthorized: you are noy authorized for this action", :details=>{}, :error_url=>"1001", :error_code=>1001}
Merge extra
with custom message
extra = %i[username password country]
message = 'Missing Parameters'
error = SmartError.handle(1001, message: message, extra: extra)
error.to_h
# {:message=>"Missing Parameters: username, password, and country", :details=>{}, :error_url=>"1001", :error_code=>1001}
Show First Model Errors by default
SmartError.handle(ExampleModel.new).to_h
Show Model Error details by invalid keys and messages and convert messages to sentence
error = SmartError.handle(ExampleModel.new)
error.to_h[:details] # 'can\'t be blank'
error.to_h[:details] # 'can\'t be blank and is invalid'
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ali-sheiba/smart_error. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant 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 SmartError project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.