BusinessError
Business Error Management by using OOP
Error::Api.invalid_token! # raise the error definedInstallation
Add this line to your application's Gemfile:
gem 'business_error'And then execute:
$ bundle
Usage
1. Config file
initializer business_error.rb
BusinessError::Config.tap do |config|
config.default_http_status = 200
end2. About BusinessError::Error
# new an error
e = BusinessError::Error.new(
name = :invalid_token,
msg = 'your token is invalid',
code = 1001,
http_status = 400 # it is optional
)
e.info # => { code: 1001, msg: '...', http: 400 }
e.message # => "{ code: 1001, msg: '...', http: 400 }"3. Define Error
3.a Recommended Practice
-
Create new directories
_docs/errorin the/appdirectory. -
Add
config.eager_load_paths << "#{Rails.root}/app/_docs"to yourapplication.rb. -
Create a base error class, like
api.rb:# app/_docs/error/api.rb class Error::Api extend BusinessError define :invalid_token, 'Your token is invalid', 1001 end
The class method
definewill define anBusinessError::Errorby the given name, msg and code. -
Now you can create more error class inherited from
Error::Api:class Error::Foo < Error::Api define :bar, 'bar', 2002 end
And then, try it in your console!
Error::Api.invalid_token # => an instance of BusinessError::Error initialized by the given params
Error::Foo.invalid_token # Yes, this error comes from inheritance!
Error::Foo.bar # This error is defined by itself
# How to raise an error? -- Bang with the same name
Error::Api.invalid_token! # => will raise an BusinessError::Error with defined message
# Methods for getting all of error definitions
# Get the error class's error definitions
Error::Api.print # It will print a YAML for showing it's groups and their error definitions
# Get this error class AND it's ancestors and descendants' error definition
Error::Api.tree # YAML also3.b Preventing error definition inheritance via grouping them
# Error::Api
group :group_name do
define :foo, 'foo', 1
define :bar, 'bar', 2
end
# Then
Error::Api.foo # ok
Error::Foo.foo # NoMethodError!
# method signature
group group_name = :private, code_start_at = nil, http: 200, &block3.c Using the same name to define?
NOT supported currently. It leads to method override, the last definition will leave.
3.d Skills
-
Use
mattr_readerinstead ofdefine(alias) IF you're using Rubymine.It makes Rubymine auto completion more perfect.
-
Use
define_px(define an error and group it into the group named that the prefix of error name)define_px :create_failed, '', -1 # the same as below: group :create do define :create_failed, '', -1 end # or define :create_failed, '', -1, group: :create # or call `mattr_reader`
-
Passing blank message:
define :create_failed, '', -1 # then, the message of this error will be: :create_failed.to_s.humanize
-
code_start_atcode_start_at 0 define ... # code is 0 define ... # code is 1 define ... # code is 2 code_start_at -1 define ... # code is -1 define ... # code is -2 define ... # code is -3 -
httphttp 500 define ... # http is 500 define ... # http is 500 http :forbidden define ... # http is :forbidden (403)
4. Raise Error
Just: error_name + bang!
Error::Api.invalid_token! # => BusinessError::Error! with invalid_token's message4.a with! for error info customization
Error::Api.invalid_token.with!(hello: 'world')
# it will raise an invalid_token error with info:
# { code: 1001, msg: '...', http: 400, hello: 'world' }4.b format! in order to be compatible with different info format requirements
error.info have a hash format defaults to:
{ code: @code, msg: @msg, http: @http_status }
# Suppose we need a format called "old"
# initializer
config.formats[:old] = %i[ status message http ]
# If:
Error::Api.invalid_token.format!(:old)
# it will raise an invalid_token error with info:
# { only: { status: 1001, message: '...', http: 400 } }
Error::Api.invalid_token.format!(:old, hello: 'world') # it's okthe key only is for output
More complex formatting is to be done:
config.formats[:old] = { format: {
status: 0,
foo: {
bar: 'default value',
msg: 'success'
},
http: 200
}, code: [:status], msg: [:foo, :msg], http: :http }format! has an alias render!
5. Rescue Error and render response by OutPut
Just do:
output Error::Api.invalid_tokenDevelopment
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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/business_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 BusinessError project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.