Errawr
A framework for effectively defining and raising localized errors.
Installation
Add this line to your application's Gemfile:
gem 'errawr'
And then execute:
$ bundle
Or install it yourself as:
$ gem install errawr
Usage
Localizations
Errawr uses I18n for easily managing error localizations. Just define a locale using the errawr
key.
en:
errawr:
your_error: Your error message here
Need to add more locale files? Use I18n's standard load_path
.
I18n.load_path += Dir.glob('lib/your_lib/locales/*.{rb,yml}')
Registering Errors
Before you can raise an error you'll need to register it first.
Errawr.register!(:your_error)
Raising Errors
begin
Errawr.error!(:your_error)
rescue => e
puts e. # Localized error message defined in locale file
end
Metadata
It's possible to add additional information to a registered error through metadata. Just specify a metadata
hash when either registering:
Errawr.register!(:your_error, metadata: { http_status: 400 })
begin
Errawr.error!(:your_error)
rescue => e
puts e.[:http_status] # Will return 400
end
or throwing an error:
Errawr.register!(:your_error)
begin
Errawr.error!(:your_error, metadata: { http_status: 400 })
rescue => e
puts e.[:http_status] # Will return 400
end
Managing Errors through Locale Files
It's also possible to manage your errors and their metadata purely through locale files.
en:
errawr:
your_error:
message: My awesome error message
metadata:
http_status: 400
Then just register and raise your exceptions like normal.
Errawr.register!(:your_error)
begin
Errawr.error!(:your_error)
rescue => e
puts e.[:http_status] # Will return 400
end
I18n Interpolation
You can pass in parameter values to your localized error messages.
en
errawr:
your_error:
message: "My awesome error message is: %{error_message}"
Errawr.register!(:your_error, error_message: 'You did it wrong!')
begin
Errawr.error!(:your_error)
rescue => e
puts e. # Will return "My awesome error message is: You did it wrong!"
end
Overrides
Want to override that metadata you registered? That's cool too.
en:
errawr:
your_error:
message: My awesome error message
metadata:
http_status: 400
The #register!
method will override the locale file.
Errawr.register!(:your_error, message: 'Some other error message', metadata: { http_status: 403 })
begin
Errawr.error!(:your_error)
rescue => e
puts e. # => Will return "Some other error message"
puts e.[:http_status] # => Will return 403
end
The #error!
method will override both #register!
and the locale file.
Errawr.register!(:your_error, message: 'Some other error message', metadata: { http_status: 403 })
begin
Errawr.error!(:your_error, message: 'Yet another error message', metadata: { http_status: 404 })
rescue => e
puts e. # => Will return "Yet another error message"
puts e.[:http_status] # => Will return 404
end
Custom Error Classes
Want to write a custom error class? No problem!
class YourError < Errawr::Error
def your_method
...
end
end
# Register your error with your custom class
Errawr.register!(:your_error, base_class: YourError)
# Do something with it
begin
Errawr.error!(:your_error)
rescue => e
e.your_method
end
Contributing
- Fork it
- 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 new Pull Request
Credits
Errawr is maintained and funded by Sticksnleaves
Thanks to all of our contributors