rest-client がステータス 400 の際に投げるエラーメッセージは "RestClient::BadRequest: 400 Bad Request" のみで、デバッグが辛すぎます。
今回は、rest-client の生ログを出しておいて、デバッグを捗らせようという趣旨です。
これとは別に、例外処理の観点から RestClient::Exception を rescue して、適切なエラーメッセージをセットした自前のErrorクラスを raise するのもやるべきと思います。
なお、バージョンは rest-client (1.8.0) です。
rubydoc
Setup the log for RestClient calls. Value should be a logger but can can be stdout, stderr, or a filename. You can also configure logging by the environment variable RESTCLIENT_LOG.
RestClient.log の引数として指定できるものは以下の4種類とのこと。
- logger
- stdout
- stderr
- filename
Railsのlogに出力する
Stackoverflow で見つけた事例です。
# config/initializers/rest-client.rb
# RestClient logs using << which isn't supported by the Rails logger,
# so wrap it up with a little proxy object.
RestClient.log =
Object.new.tap do |proxy|
def proxy.<<(message)
Rails.logger.info message
end
end
引用: Logging RestClient in a Rails app
Railsのログとは別のファイルに出力する
なんでもかんでも production.log に出力したくはなかったので、今回は restclient.log に出力することにしました。
# config/initializers/rest-client.rb
logfile = Rails.root.join('log', 'restclient.log')
RestClient.log = Logger.new(logfile)