{"id":15801820,"url":"https://github.com/zhandao/business_error","last_synced_at":"2026-01-21T14:01:41.792Z","repository":{"id":62554753,"uuid":"152731246","full_name":"zhandao/business_error","owner":"zhandao","description":"Business Error Management by using OOP","archived":false,"fork":false,"pushed_at":"2023-04-04T08:52:21.000Z","size":14,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-10-07T13:30:29.615Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhandao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-12T10:00:49.000Z","updated_at":"2023-04-04T08:52:27.000Z","dependencies_parsed_at":"2024-10-05T01:41:06.187Z","dependency_job_id":null,"html_url":"https://github.com/zhandao/business_error","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"33bf20e4499f474fb4f31a062b8a6fcd0cd5869e"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/zhandao/business_error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fbusiness_error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fbusiness_error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fbusiness_error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fbusiness_error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhandao","download_url":"https://codeload.github.com/zhandao/business_error/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fbusiness_error/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28634786,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T04:47:28.174Z","status":"ssl_error","status_checked_at":"2026-01-21T04:47:22.943Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T01:40:58.727Z","updated_at":"2026-01-21T14:01:41.777Z","avatar_url":"https://github.com/zhandao.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BusinessError\n\n[![Gem Version](https://badge.fury.io/rb/business_error.svg)](https://badge.fury.io/rb/business_error)\n\nBusiness Error Management by using OOP\n\n```ruby\nError::Api.invalid_token! # raise the error defined\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'business_error'\n```\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\n### 1. Config file\n\ninitializer `business_error.rb`\n\n```ruby\nBusinessError::Config.tap do |config|\n  config.default_http_status = 200\nend\n```\n\n### 2. About `BusinessError::Error`\n\n```ruby\n# new an error\ne = BusinessError::Error.new(\n  name = :invalid_token,\n  msg = 'your token is invalid',\n  code = 1001,\n  http_status = 400 # it is optional\n)\n\ne.info # =\u003e { code: 1001, msg: '...', http: 400 }\ne.message # =\u003e \"{ code: 1001, msg: '...', http: 400 }\"\n```\n\n### 3. Define Error\n\n#### 3.a Recommended Practice\n\n1. Create new directories `_docs/error` in the `/app` directory.\n\n2. Add `config.eager_load_paths \u003c\u003c \"#{Rails.root}/app/_docs\"` to your `application.rb`.\n\n3. Create a base error class, like `api.rb`:\n    ```ruby\n    # app/_docs/error/api.rb\n    class Error::Api\n      extend BusinessError\n   \n      define :invalid_token, 'Your token is invalid', 1001\n    end\n    ```\n    The class method `define` will define an `BusinessError::Error` by the given\n    name, msg and code.\n    \n4. Now you can create more error class inherited from `Error::Api`:\n    ```ruby\n    class Error::Foo \u003c Error::Api\n      define :bar, 'bar', 2002\n    end\n    ```\n    \nAnd then, try it in your console!\n\n```ruby\nError::Api.invalid_token # =\u003e an instance of BusinessError::Error initialized by the given params\nError::Foo.invalid_token # Yes, this error comes from inheritance!\nError::Foo.bar           # This error is defined by itself\n\n# How to raise an error? -- Bang with the same name\nError::Api.invalid_token! # =\u003e will raise an BusinessError::Error with defined message\n\n# Methods for getting all of error definitions\n#   Get the error class's error definitions\nError::Api.print # It will print a YAML for showing it's groups and their error definitions\n#   Get this error class AND it's ancestors and descendants' error definition\nError::Api.tree  # YAML also\n```\n\n#### 3.b Preventing error definition inheritance via grouping them\n\n```ruby\n# Error::Api\ngroup :group_name do \n  define :foo, 'foo', 1\n  define :bar, 'bar', 2\nend\n# Then\nError::Api.foo # ok\nError::Foo.foo # NoMethodError!\n\n# method signature\ngroup group_name = :private, code_start_at = nil, http: 200, \u0026block\n```\n\n#### 3.c Using the same name to define?\n\nNOT supported currently. It leads to method override,\nthe last definition will leave.\n\n#### 3.d Skills\n\n1. Use `mattr_reader` instead of `define` (alias) IF you're using Rubymine.\n\n    It makes Rubymine auto completion more perfect.\n    \n2. Use `define_px` (define an error and group it into the group named that the prefix of error name)\n\n    ```ruby\n    define_px :create_failed, '', -1\n    # the same as below:\n    group :create do\n      define :create_failed, '', -1\n    end\n    # or\n    define :create_failed, '', -1, group: :create # or call `mattr_reader`\n    ```\n    \n3. Passing blank message:\n\n    ```ruby\n    define :create_failed, '', -1\n    # then, the message of this error will be:\n    :create_failed.to_s.humanize\n    ```\n    \n4. `code_start_at`\n\n    ```\n    code_start_at 0\n    define ... # code is 0\n    define ... # code is 1\n    define ... # code is 2\n \n    code_start_at -1\n    define ... # code is -1\n    define ... # code is -2\n    define ... # code is -3\n    ```\n    \n5. `http`\n    \n    ```\n    http 500\n    define ... # http is 500\n    define ... # http is 500\n    \n    http :forbidden\n    define ... # http is :forbidden (403)\n    ```\n\n### 4. Raise Error\n\nJust: error_name + bang!\n\n```ruby\nError::Api.invalid_token! # =\u003e BusinessError::Error! with invalid_token's message\n```\n\n#### 4.a `with!` for error info customization\n\n```ruby\nError::Api.invalid_token.with!(hello: 'world')\n# it will raise an invalid_token error with info:\n#   { code: 1001, msg: '...', http: 400, hello: 'world' }\n```\n\n#### 4.b `format!` in order to be compatible with different info format requirements\n\nerror.info have a hash format defaults to:  \n`{ code: @code, msg: @msg, http: @http_status }`\n\n```ruby\n# Suppose we need a format called \"old\"\n# initializer\nconfig.formats[:old] = %i[ status message http ]\n\n# If:\nError::Api.invalid_token.format!(:old)\n# it will raise an invalid_token error with info:\n#   { only: { status: 1001, message: '...', http: 400 } }\n\nError::Api.invalid_token.format!(:old, hello: 'world') # it's ok\n```\n\nthe key `only` is for [`output`](https://github.com/zhandao/out_put)\n\nMore complex formatting is to be done:\n```ruby\nconfig.formats[:old] = { format: {\n  status: 0,\n  foo: {\n    bar: 'default value',\n    msg: 'success'\n  },\n  http: 200\n}, code: [:status], msg: [:foo, :msg], http: :http }\n```\n\n`format!` has an alias `render!`\n\n### 5. Rescue Error and render response by [`OutPut`](https://github.com/zhandao/out_put)\n\nJust do:\n```ruby\noutput Error::Api.invalid_token\n```\n\n## Development\n\nAfter 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.\n\nTo 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](https://rubygems.org).\n\n## Contributing\n\nBug 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](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the BusinessError project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/business_error/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhandao%2Fbusiness_error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhandao%2Fbusiness_error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhandao%2Fbusiness_error/lists"}