great084 / twitter_tool

ツイッターツールの開発
0 stars 0 forks source link

再投稿のapi失敗時に適切なエラー処理が行われない #64

Closed great084 closed 3 years ago

great084 commented 3 years ago

バグの現象

本来あるべき動作

再投稿を行ってTwitter apiで失敗した際に、エラー内容を画面に表示する

実際の動作

再投稿を行ってTwitter apiで失敗した際に、システムエラー画面となった。

発生時のログ

2020-12-21T13:17:09.032512+00:00 app[web.1]: I, [2020-12-21T13:17:09.032373 #4]  INFO -- : [73294fb0-ac5c-4234-be4e-b9933cb6b1af] Completed 500 Internal Server Error in 91ms (ActiveRecord: 0.8ms | Allocations: 3082)
2020-12-21T13:17:09.033898+00:00 app[web.1]: F, [2020-12-21T13:17:09.033786 #4] FATAL -- : [73294fb0-ac5c-4234-be4e-b9933cb6b1af]
2020-12-21T13:17:09.033899+00:00 app[web.1]: [73294fb0-ac5c-4234-be4e-b9933cb6b1af] ActionController::UrlGenerationError (No route matches {:action=>"show", :controller=>"tweets", :id=>nil}, missing required keys: [:id]):
2020-12-21T13:17:09.033900+00:00 app[web.1]: [73294fb0-ac5c-4234-be4e-b9933cb6b1af]
2020-12-21T13:17:09.033901+00:00 app[web.1]: [73294fb0-ac5c-4234-be4e-b9933cb6b1af] app/controllers/tweets_controller.rb:50:in `rescue in post_create'
2020-12-21T13:17:09.033902+00:00 app[web.1]: [73294fb0-ac5c-4234-be4e-b9933cb6b1af] app/controllers/tweets_controller.rb:42:in `post_create'
2020-12-21T13:17:09.039645+00:00 heroku[router]: at=info method=POST path="/tweets/64/post_create" host=my-tweet-research.herokuapp.com request_id=73294fb0-ac5c-4234-be4e-b9933cb6b1af fwd="106.180.2.72" dyno=web.1 connect=0ms service=109ms status=500 bytes=1827 protocol=https

バグの原因

再投稿がTwitter apiで失敗した場合に、エラーをキャッチしてredirectする設定を入れているが、 redirect先の値を取得するのが、再投稿処理のあとになっており、redirectに失敗したため。

以下は該当のコード resuceで再投稿の処理(@client.update)のエラーを取得しているが、 エラー処理のredeirect_toに設定する引数を設定する処理(@tweet_data_all = Tweet.find(params[:id]))が、再投稿の処理(@client.update)より後にあるため、取得できなかった。

def post_create
    @tweet = Tweet.new(post_params)
    @client.update("#{@tweet.text}\r")
    @tweet_data_all = Tweet.find(params[:id])
    @tweet_data_all.tweet_flag = true
    @tweet_data_all.save
    redirect_to tweet_path(@tweet_data_all), success: "再投稿に成功しました"
  rescue StandardError => e
    redirect_to tweet_path(@tweet_data_all), danger: "再投稿に失敗しました#{e}"
  end

バグの解消方法

引数を設定する処理(@tweet_data_all = Tweet.find(params[:id]))を、再投稿の処理(@client.update)の前で実行する