20724
Rack app error: ActionController::UnknownHttpMethodをrescueするにはどうすればいいのでしょうか？

指定されたHTTP
method以外でアクセスするとこのエラーが出ます。その際に各種のバージョンなどがクライアント側に出力されるのでこれをrescueして、単純なエラーメッセージだけに変更したいと考えています。

midlewareから出されているエラーのようで  
app/controllers/application_controller.rb  
に  
rescue_from ActionController::UnknownHttpMethod, with: :unknown_method  
としても反応しません。

どうすればいいのでしょうか？

エラーログ

```

    2016-01-06 22:22:28 +0800: Rack app error: #<ActionController::UnknownHttpMethod: AAA, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH>
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/http/request.rb:344:in `check_method'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/http/request.rb:105:in `request_method'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/rack/logger.rb:51:in `started_request_message'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/rack/logger.rb:37:in `block in call_app'
    /.rbenv/versions/2.2.3/lib/ruby/2.2.0/logger.rb:372:in `add'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/logger.rb:13:in `block (2 levels) in broadcast'
    /.rbenv/versions/2.2.3/lib/ruby/2.2.0/logger.rb:434:in `info'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/rack/logger.rb:37:in `call_app'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `block in call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:68:in `block in tagged'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:26:in `tagged'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:68:in `tagged'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/request_id.rb:21:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/static.rb:116:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/engine.rb:518:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/application.rb:165:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/puma-2.15.3/lib/puma/server.rb:541:in `handle_request'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/puma-2.15.3/lib/puma/server.rb:388:in `process_client'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/puma-2.15.3/lib/puma/server.rb:270:in `block in run'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/puma-2.15.3/lib/puma/thread_pool.rb:106:in `call'
    /.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/puma-2.15.3/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
    
```

Ruby 2.2.3  
Rails 4.2.4  
puma 2.15.3

「Rails の rescue_from で拾えない例外を exceptions_app で処理する」  
<http://qiita.com/ma2ge/items/938d9f8f4839eb336318>

こちらを参考にしてRails.configuration.exceptions_appを設定しましたが、まったく反応せず上記のエラーログが出てきます。エラーはcurlコマンドを使って

```

    $ curl -X AAA http://localhost:3000/
    
```

などとして例外になるようなHTTPメソッドを使ってコールしています。この時のエラーを捕捉したいのですが、まったくできません。なにがおかしいのでしょうか？
CC BY-SA 3.0
2016-01-06T14:55:38.913
20678
2016-02-01T00:47:55.290
2016-02-01T00:47:55.290
5793
9202
question
ruby-on-rails ruby rack
Rack app error: ActionController::UnknownHttpMethodをrescueする方法
> これをrescueして、単純なエラーメッセージだけに変更したい

というのであれば、微妙に求めている回答とは違うかもしれませんが、こちらのページが参考になるかもしれません。

[Render 405 response for ActionController::UnknownHttpMethod
exceptions](https://gist.github.com/viola/1243572)

試しにこちらでも組み込んでみました。

```

    # lib/http_method_not_allowed.rb
    
    class HttpMethodNotAllowed
      def initialize(app)
        @app = app
      end
    
      def call(env)
        if !ActionDispatch::Request::HTTP_METHODS.include?(env["REQUEST_METHOD"].upcase)
          Rails.logger.info("ActionController::UnknownHttpMethod: #{env.inspect}")
          [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]]
        else
          @status, @headers, @response = @app.call(env)
          [@status, @headers, @response]
        end
      end
    end
    
    # config/application.rb
    require 'http_method_not_allowed'
    config.middleware.insert_before(0, HttpMethodNotAllowed)
    
```

実行結果

```

    $ curl -X AAA http://localhost:3000/              
    Method Not Allowed%
    
```

ログ

```

    ActionController::UnknownHttpMethod: {"GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/", "QUERY_STRING"=>"", "REMOTE_ADDR"=>"::1", "REMOTE_HOST"=>"::1", "REQUEST_METHOD"=>"AAA", "REQUEST_URI"=>"http://localhost:3000/", ...（以下省略）
    
```

コードはこちらに置いてあります。

[JunichiIto/unknown-http-method-
sandbox](https://github.com/JunichiIto/unknown-http-method-sandbox)

ご参考までに。
CC BY-SA 3.0
2016-01-07T23:02:35.697
20724
2016-01-07T23:02:35.697
85
20678
answer