Doorkeeperでログイン中にエラーが発生すると、Doorkeeper::TokensControllerの中でエラーが起きるので、ApplicationController < ActionController::Baseの中でrescue_fromを書いてもうまくハンドリングできなかった。これをrescueするための方法。エッセンスは以下の通り。
- use_doorkeeperの中でdoorkeeperの処理に使うcontrollerを指定できる
- Doorkeeper::TokensControllerはActionController::Metalを継承しているので、renderを実行するのに必要なmoduleをincludeする必要がある
/config/routes.rb
MyAppServer::Application.routes.draw do use_doorkeeper do controllers tokens: 'tokens' end resources :books resources :users end
/app/controllers/tokens_controller.rb
class TokensController < Doorkeeper::TokensController include ActionController::Rescue include ActionController::Rendering include ActionController::Renderers::All rescue_from Exception do |e| render json: {error: 'something occurred.'} end end