{"id":29287362,"url":"https://github.com/kuboon/restful_error","last_synced_at":"2025-07-06T01:07:12.573Z","repository":{"id":23955919,"uuid":"27337936","full_name":"kuboon/restful_error","owner":"kuboon","description":"Define your error with status code. Raise it and you will get formatted response with i18nized message.","archived":false,"fork":false,"pushed_at":"2025-04-07T04:10:38.000Z","size":50,"stargazers_count":60,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-23T03:46:02.348Z","etag":null,"topics":["rails","ruby"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/restful_error","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/kuboon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"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":"2014-11-30T14:12:34.000Z","updated_at":"2025-06-11T05:46:05.000Z","dependencies_parsed_at":"2025-02-06T03:25:01.010Z","dependency_job_id":"895d29da-efbb-4af2-bdc5-35ca3d55d2cb","html_url":"https://github.com/kuboon/restful_error","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/kuboon/restful_error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuboon%2Frestful_error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuboon%2Frestful_error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuboon%2Frestful_error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuboon%2Frestful_error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kuboon","download_url":"https://codeload.github.com/kuboon/restful_error/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuboon%2Frestful_error/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263833416,"owners_count":23517374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["rails","ruby"],"created_at":"2025-07-06T01:07:06.954Z","updated_at":"2025-07-06T01:07:12.564Z","avatar_url":"https://github.com/kuboon.png","language":"Ruby","readme":"[![Gem Version](https://img.shields.io/gem/v/restful_error)](https://rubygems.org/gems/restful_error)\n\n# RestfulError\n\nDefine your error with status code. Raise it and you will get formatted response with i18nized message.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'restful_error'\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\n### Pure ruby (without Rails)\n#### Predefined errors\n```ruby\nex = RestfulError[404].new\n\nStandardError === ex # =\u003e true # because inherit from StandardError\nRestfulError::BaseError === ex # =\u003e true\n\nRestfulError[404] == RestfulError::NotFound # =\u003e true # same class\n\nex.status_data # returns Data about status code\n# =\u003e #\u003cdata RestfulError::Status\n#      code=404,\n#      reason_phrase=\"Not Found\",\n#      symbol=:not_found,\n#      const_name=\"NotFound\"\u003e\nex.status_data.code # =\u003e 404\n```\n\n#### Custom error by subclassing\n```ruby\nclass ::NoSession \u003c RestfulError[404]; end\n# or\nclass ::NoSession \u003c RestfulError::NotFound; end\n```\n\n#### Custom error with http_status\n```ruby\n# define http_status and include RestfulError::Helper\nclass User::PermissionError \u003c StandardError\n  include RestfulError::Helper\n  def http_status = :unauthorized # or 401\nend\nUser::PermissionError.new.status_data.reason_phrase # =\u003e \"Unauthorized\"\n```\n\n### With I18n\n`#response_message` returns i18nized message.\n```yaml\nja:\n  restful_error:\n    unauthorized: ログインが必要です\n    not_found: ページが存在しません\n    user/permission_error: 権限がありません\n```\n```ruby\n# lookup class name first, then status symbol\nUser::PermissionError.new.response_message # =\u003e \"権限がありません\"\nAnotherPermissionError.new.response_message # =\u003e \"ログインが必要です\"\n```\n\n### With Rails\n`config.exceptions_app` [[guide]](https://guides.rubyonrails.org/configuring.html#config-exceptions-app) will automatically set to RestfulError::ExceptionsApp.\n\nIf you want to disable it, you have two options.\n- `config.restful_error.exceptions_app.enable = false` (will not set exceptions_app)\n- `config.exceptions_app = ActionDispatch::PublicExceptions.new(Rails.public_path)` (set Rails default explicitly, or set your own)\n\n#### Raise me in request handling\n```ruby\nclass PostsController \u003c ApplicationController\n  before_action do\n    raise RestfulError[401] unless current_user\n    # or\n    raise RestfulError::Unauthorized unless current_user\n  end\nend\n```\nIf you want to check the output on development env, you need to set `config.consider_all_requests_local = false` and ensure `show_detailed_exceptions? == false` [[guide]](https://guides.rubyonrails.org/configuring.html#config-consider-all-requests-local)\n\n#### Render response\nDefault view files are in [app/views/restful_error](app/views/restful_error)\n\n`html`, `json` and `xml` are supported.\n\nYou can override them by creating view file `show.{format}.{handler}` under your `app/views/restful_error/` directory.\n\n`@status` `@status_code` `@reason_phrase` `@response_message` are available in the view.\n\nIf you have `layouts/restful_error.*.*`, or `layouts/application.*.*`, it will be used as layout. This is done by inheriting `::ApplicationController`.\n\nTo change superclass,\nset `config.restful_error.exceptions_app.inherit_from = 'AnotherBaseController'`\n\n#### Library defined error\nYou can assign status code to error classes which are not yours. (This is Rails standard)\n\n```ruby\nconfig.action_dispatch.rescue_responses[\"Pundit::NotAuthorizedError\"] = :unauthorized # or 401\n```\n\nRestfulError will use these configurations to lookup status code and\n`@response_message` will be set on `exceptions_app`.\n\n```yaml\nja:\n  restful_error:\n    pundit/not_authorized_error: アクセス権限がありません\n    active_record/record_not_found: 要求されたリソースが存在しません\n```\n\n## Why `response_message`, not `message`?\n`StandardError#message` is used for debugging purpose, not intended to be shown to users.\nRails default behavior does not show `message` in production environment. So I decided to use `response_message` instead.\n\nYou can `def response_message` or set `@resposne_message` in your error class to build dynamic message.\n\n\n## Contributing\n\n1. Fork it ( https://github.com/kuboon/restful_error/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuboon%2Frestful_error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkuboon%2Frestful_error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuboon%2Frestful_error/lists"}