LoginSignup
0
0

More than 5 years have passed since last update.

dns_get_recordを呼び出したときにエラーを吐く場合のとりあえずの対処法

Posted at

dns_get_recordを呼び出した際に、以下のエラーが発生して処理が止まりました。
こちらのバグレポートと同様のものです。

Warning Error: dns_get_record(): A temporary server error occurred.

ここで発生しているのはExceptionではなくエラーなので、dns_get_recordをtry-catchで括ってもキャッチすることはできません。

根本的な修正方法は、エラーハンドラーを正しく設定するなどだと思うのですが...
とりあえずこの箇所だけでもExceptionとしてキャッチすることで処理は継続できます。

  // !!IMPORTANT!! https://bugs.php.net/bug.php?id=73149 の回避のために一時的にエラーハンドラを上書き                                                                                        
  set_error_handler(function($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
  });

  // DNSレコード取得
  try {
    $dnsRecord = dns_get_record($hostname);
  } catch (ErrorException $ee) {
    // 例外処理
  }

  // !!IMPORTANT!! エラーハンドラを元に戻す
  restore_error_handler();

set_error_handlerでエラーをErrorExceptionに変換し、処理が終わったらrestore_error_handlerで戻しています。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0