{"id":3237,"date":"2013-10-01T15:17:14","date_gmt":"2013-10-01T06:17:14","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=3237"},"modified":"2018-11-14T16:33:54","modified_gmt":"2018-11-14T07:33:54","slug":"chatty_error%e3%81%a8%e3%81%84%e3%81%86gem%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/10\/01\/chatty_error%e3%81%a8%e3%81%84%e3%81%86gem%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f\/","title":{"rendered":"chatty_error\u3068\u3044\u3046gem\u3092\u4f5c\u3063\u3066\u307f\u305f"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>chatty_error \u3068\u3044\u3046gem\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<h2>chatty_error<\/h2>\n<p>\u72ec\u81ea\u306e\u4f8b\u5916\u30af\u30e9\u30b9\u306e\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u3001\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8a2d\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<br \/>\n\u30a8\u30e9\u30fc\u30af\u30e9\u30b9\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3059\u308b\u6642\u3001\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8a2d\u5b9a\u3059\u308b\u306e\u304c\u9762\u5012\u3060\uff01\u3068\u3044\u3046\u554f\u984c\u3092\u89e3\u6c7a\u3057\u307e\u3059\u3002<\/p>\n<p>chatty\u3068\u3044\u3046\u5358\u8a9e\u306b\u3001<\/p>\n<blockquote cite=\"http:\/\/eow.alc.co.jp\/search?q=chatty&amp;ref=sa\"><p>chatty: \u3010\u5f62\u3011\u8a71\u3057\u597d\u304d\u306a\u3001\u3014\u6587\u7ae0\u3084\u8a71\u3057\u65b9\u304c\u3015\u304f\u3060\u3051\u305f<\/p><\/blockquote>\n<p><a title=\"chatty\" href=\"http:\/\/eow.alc.co.jp\/search?q=chatty&amp;ref=sa\" target=\"_blank\">alc<\/a><\/p>\n<p>\u3068\u3044\u3046\u610f\u5473\u304c\u3042\u308b\u3088\u3046\u306a\u306e\u3067\u3001<code>chatty_error<\/code>\u3068\u540d\u4ed8\u3051\u307e\u3057\u305f\u3002<\/p>\n<h2>\u4f5c\u3063\u305f\u80cc\u666f<\/h2>\n<p>\u3042\u308b\u6848\u4ef6\u3067\u3001\u72ec\u81ea\u306e\u4f8b\u5916\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u4f8b\u5916\u3092raise \u3059\u308b\u3068\u304d\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u3068\u3093\u3067\u3082\u306a\u3044\u3053\u3068\u304c\u8d77\u304d\u305f\r\nraise MyError.new(I18n.t('my_error.tondemonai_kotoga_okita'))<\/pre>\n<p>\u4ed6\u306e\u3068\u3053\u308d\u3067\u306f\u3001\u9055\u3046\u30e1\u30c3\u30bb\u30fc\u30b8\u3092<\/p>\n<pre class=\"lang:ruby decode:true\"># \u4e0d\u6b63\u306a\u30a2\u30af\u30bb\u30b9\u304b\u3082\u3057\u308c\u306a\u3044\r\nraise MyError.new(I18n.t('my_error.huseina_akusesu_kamo_sirenai'))<\/pre>\n<p>\u307e\u305f\u5225\u306e\u3068\u3053\u308d\u3067\u306f\u3001<\/p>\n<pre class=\"lang:ruby decode:true\"># \u51e6\u7406\u306e\u624b\u9806\u304c\u304a\u304b\u3057\u3044\r\nraise MyError.new(I18n.t('my_error.tezyunga_okasii'))<\/pre>\n<p>\u3068\u3044\u3046\u611f\u3058\u3067\u3001\u540c\u3058\u30a8\u30e9\u30fc\u3067\u3082\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u5909\u3048\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3057\u305f\u3002<br \/>\n\u7406\u60f3\u3092\u8a00\u3048\u3070\u7d30\u304b\u304f\u4f8b\u5916\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u305f\u65b9\u304c\u826f\u3055\u305d\u3046\u306a\u306e\u3067\u3059\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u3053\u3093\u306a\u611f\u3058\u3067\u5bfe\u5fdc\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u3002<\/p>\n<p>\u3067\u3001\u8272\u3093\u306a\u3068\u3053\u308d\u3067\u3001<strong>MyError\u3092raise\u3059\u308b\u5ea6\u306b\u3001I18n\u3092\u547c\u3073\u51fa\u3055\u306a\u3044\u3068\u3044\u3051\u307e\u305b\u3093<\/strong>\u3002<\/p>\n<p>\u3053\u308c\u306f\u4f55\u304b\u30e2\u30e4\u30e2\u30e4\u3059\u308b\uff5e\u3068\u3044\u3046\u3053\u3068\u3067\u3001chatty_error \u306e\u767b\u5834\u3067\u3059\u3002<\/p>\n<h2>\u4f7f\u3044\u65b9<\/h2>\n<h3>Gem\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>\u307e\u305a\u306fgem\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002bundler\u306e\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># Gemfile\r\ngem 'chatty_error'<\/pre>\n<p>bundle\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ bundle<\/pre>\n<p>\u3053\u308c\u3067\u3001<code>ChattyError<\/code>\u3068\u3044\u3046\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u539f\u56e0\u3092\u8a2d\u5b9a<\/h3>\n<p>\u3055\u3066\u3001chatty_error \u3067\u306f<strong>\u539f\u56e0\u306b\u5bfe\u3057\u3066\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8a2d\u5b9a\u3059\u308b\u3068\u3044\u3046\u65b9\u91dd<\/strong>\u3092\u63a1\u3063\u3066\u3044\u307e\u3059\u3002\u306a\u306e\u3067\u3001\u72ec\u81ea\u306e\u4f8b\u5916\u30af\u30e9\u30b9\u3067\u306f\u539f\u56e0\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<code>caused_by<\/code> \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>MyError\u306e\u4f8b\u3060\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># my_error.rb\r\nclass MyError &lt; StandardError\r\n  include ChattyError\r\n\r\n  caused_by :terrible_thing_happend, :unauthorized_access, :wrong_procedure\r\nend<\/pre>\n<p>\u300c\u3068\u3093\u3067\u3082\u306a\u3044\u3053\u3068\u304c\u8d77\u304d\u305f\u300d\u300c\u4e0d\u6b63\u306a\u30a2\u30af\u30bb\u30b9\u300d\u300c\u9593\u9055\u3063\u305f\u624b\u9806\u300d\u3068\u3044\u3046\u4f8b\u5916\u304c\u8d77\u304d\u308b\u539f\u56e0\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8a2d\u5b9a<\/h3>\n<p>\u6b21\u306b\u3001\u539f\u56e0\u306b\u5bfe\u5fdc\u3059\u308b\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># ja.yml\r\nja:\r\n  chatty_errors:\r\n    my_error:\r\n      terrible_thing_happend: \"\u3068\u3093\u3067\u3082\u306a\u3044\u3053\u3068\u304c\u8d77\u304d\u305f\uff01\uff01\uff01\"\r\n      unauthorized_access: \"\u4e0d\u6b63\u306a\u30a2\u30af\u30bb\u30b9\u3060\uff01\uff01\uff01\"\r\n      wrong_procedure: \"\u9806\u756a\u9593\u9055\u3063\u3066\u308b\"<\/pre>\n<h3>\u4f8b\u5916\u306e\u547c\u3073\u51fa\u3057\u65b9<\/h3>\n<p>raise\u306e\u6642\u306b\u306f\u3001<\/p>\n<pre class=\"lang:ruby decode:true\">raise MyError.terrible_thing_happend\r\nraise MyError.unauthorized_access\r\nraise MyError.wrong_procedure<\/pre>\n<p>\u3068\u3044\u3046\u611f\u3058\u3067\u3001\u7c21\u5358\u306b\u547c\u3073\u51fa\u305b\u307e\u3059\u3002<\/p>\n<p>MyError\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3068\u3068\u3082\u306b\u3001\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3082\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u3002\u3064\u3044\u3067\u306b\u3001<code>cause<\/code> \u306b\u539f\u56e0\u306b\u76f8\u5f53\u3059\u308b\u30b7\u30f3\u30dc\u30eb\u304c\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">error = MyError.unauthorized_access\r\nerror.message # =&gt; \u4e0d\u6b63\u306a\u30a2\u30af\u30bb\u30b9\u3060\uff01\uff01\uff01\r\nerror.cause   # =&gt; :unauthorized_access<\/pre>\n<h2>\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u66f8\u304d\u65b9<\/h2>\n<p>\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u306b\u8a2d\u5b9a\u3059\u308b\u30ad\u30fc\u306f\u3001\u300cchatty_errors.\u30af\u30e9\u30b9\u968e\u5c64.\u539f\u56e0\u300d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u4eee\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5b9a\u7fa9\u306b\u306a\u3063\u3066\u3044\u305f\u3089\u3001<\/p>\n<pre class=\"lang:ruby decode:true\"># deep.rb\r\nmodule A\r\n  module B\r\n    module C\r\n      class Deep &lt; StandardError\r\n        include ChattyError\r\n\r\n        caused_by :deeper\r\n      end\r\n    end\r\n  end\r\nend<\/pre>\n<p>\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u306f<\/p>\n<pre class=\"lang:ruby decode:true\"># ja.yml\r\nja:\r\n  chatty_errors:\r\n    a:\r\n      b:\r\n        c:\r\n          deep:\r\n            deeper: \"\u968e\u5c64\u6df1\u3044\u2026\"<\/pre>\n<p>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u30c7\u30d5\u30a9\u30eb\u30c8\u8a2d\u5b9a<\/h2>\n<p>\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u4e0a\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u30b9\u30b3\u30fc\u30d7\u3068\u3001\u30ad\u30fc\u304c\u4e00\u81f4\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># my_error.rb\r\nclass MyError &lt; StandardError\r\n  include ChattyError\r\n\r\n  configure do |c|\r\n    c.default_scope = 'techscore'\r\n    c.default_message = '\u30a8\u30e9\u30fc\u3060\u3088'\r\n  end\r\n\r\n  caused_by :terrible_thing_happend, :unauthorized_access, :wrong_procedure\r\nend<\/pre>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u30b9\u30b3\u30fc\u30d7\u3092\u5909\u66f4\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># ja.yml\r\nja:\r\n  techscore:\r\n    my_error:\r\n      terrible_thing_happend: \"\u3068\u3093\u3067\u3082\u306a\u3044\u3053\u3068\u304c\u8d77\u304d\u305f\uff01\uff01\uff01\"\r\n      unauthorized_access: \"\u4e0d\u6b63\u306a\u30a2\u30af\u30bb\u30b9\u3060\uff01\uff01\uff01\"\r\n      wrong_procedure: \"\u9806\u756a\u9593\u9055\u3063\u3066\u308b\"<\/pre>\n<h3>\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u6c7a\u5b9a<\/h3>\n<p>\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u6c7a\u5b9a\u306f\u4ee5\u4e0b\u512a\u5148\u9806\u4f4d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>1. \u30c7\u30d5\u30a9\u30eb\u30c8\u30b9\u30b3\u30fc\u30d7\u3001\u30af\u30e9\u30b9\u968e\u5c64\u3001\u30e1\u30bd\u30c3\u30c9\u540d\u304b\u3089\u6c7a\u5b9a\u3055\u308c\u308b\u30ad\u30fc\u3068\u5bfe\u5fdc\u3059\u308b\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u4e0a\u306e\u30e1\u30c3\u30bb\u30fc\u30b8<br \/>\n2. '\u30c7\u30d5\u30a9\u30eb\u30c8\u30b9\u30b3\u30fc\u30d7.default_message' \u3068\u3044\u3046\u30ad\u30fc\u3068\u5bfe\u5fdc\u3059\u308b\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u4e0a\u306e\u30e1\u30c3\u30bb\u30fc\u30b8<br \/>\n3. configure\u3067\u8a2d\u5b9a\u3057\u305f\u3001default_message \u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u30e1\u30c3\u30bb\u30fc\u30b8<\/p>\n<h2>\u3061\u3087\u3063\u3068\u3057\u305f\u6d3b\u7528\u4f8b<\/h2>\n<p>Rails\u306e\u5834\u5408\u3067\u3059\u304c\u3001ApplicationController\u306e\u4e2d\u3067 rescue_from\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class ApplicationController &lt; ActionController::Base\r\n\r\n  rescue_from MyError, :with =&gt; :my_error_handle\r\n\r\n  def my_error_handle(exception)\r\n    case exception.cause\r\n    when :unauthorized_access\r\n      logger.warn([exception.message].join(\"\\n\"))\r\n    else\r\n      logger.error([exception.message].join(\"\\n\"))\r\n    end\r\n    render 'errors\/500', :status =&gt; '500 Internal Server Error', :layout =&gt; 'errors\/application'\r\n  end\r\nend<\/pre>\n<p>MyError\u306ecause\u306b\u3088\u3063\u3066\u3001\u30ed\u30b0\u30ec\u30d9\u30eb\u3092\u5909\u3048\u308b\u3068\u3044\u3046\u51e6\u7406\u3067\u3059\u3002\u4ed6\u306b\u3082\u8272\u3005\u3068\u4f7f\u3044\u3069\u3053\u308d\u306f\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>\u30ea\u30f3\u30af<\/h2>\n<p><a href=\"http:\/\/rubygems.org\/gems\/chatty_error\">rubygems<\/a> | <a href=\"https:\/\/github.com\/techscore\/chatty_error\">github<\/a><\/p>\n<p>\u4ee5\u4e0a\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>chatty_error \u3068\u3044\u3046gem\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\nchatty_error<br \/>\n\u72ec\u81ea\u306e\u4f8b\u5916\u30af\u30e9\u30b9\u306e\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u3001\u30ed\u30b1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8a2d\u5b9a\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/10\/01\/chatty_error%e3%81%a8%e3%81%84%e3%81%86gem%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[24,95],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3237"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=3237"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3237\/revisions"}],"predecessor-version":[{"id":17910,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/3237\/revisions\/17910"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=3237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=3237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=3237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}