且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP& WP:引发DB错误时,尝试捕获不起作用

更新时间:2023-02-06 11:52:50

您的代码位于自定义名称空间中:

namespace StatCollector;

这样的代码:

catch (Exception $ex)

正在寻找\StatCollector\Exception的异常类,不是抛出的异常:\Exception是.

由于ErrorException类位于 root 名称空间中,因此需要执行以下操作:

catch (\Exception $ex)

和/或:

catch (\Error $ex)

或者只是:

catch (\Throwable $t)

然后还遵循 Bill Karwin的上述建议.

I'm currently trying to write error handling for the following situation. I want to be able to catch an error when a data base is --read-only and is trying to write.

WordPress database error INSERT command denied to user 'readonly'@

This error is thrown because the database is a --read-only and is trying to write into the database. However, this error should have been caught by my catch conditional.

function drools_request($data, $uid) {
  try {
    $db = _get_db();
    $db->insert("requests", [
      "uid" => $uid,
      "data" => json_encode($data),
    ]);
  }
  catch(Error $e){
    echo 'Error writing to database: ',  $e->getMessage(), "\n";
  }
}

As you see from the above snippet the $db->insert( method correctly breaks because is trying to write into a $db instance that is --read-only. However, the catch(Error $e) did not work, why?

Here is the full class:

    <?php

namespace StatCollector;

function drools_request($data, $uid) {
  try {
    $db = _get_db();
    $db->insert("requests", [
      "uid" => $uid,
      "data" => json_encode($data),
    ]);
  }
  catch(Exception $e)
  {
    echo 'Error writing to database: ',  $e->getMessage(), "\n";
  }
}

function drools_response($response, $uid) {
  try {
    $db = _get_db();
    $db->insert("responses", [
      "uid" => $uid,
      "data" => json_encode($response),
    ]);
  }
  catch(Exception $e)
  {
    echo 'Error writing to database: ',  $e->getMessage(), "\n";
  }
}

function results_sent($type, $to, $uid, $url = null, $message = null) {
  try {
    $db = _get_db();
    $db->insert("messages", [
      "uid" => $uid,
      "msg_type" => strtolower($type),
      "address" => $to,
      "url" => $url,
      "message" => $message
    ]);
  }
  catch(Exception $e)
  {
    echo 'Error writing to database: ',  $e->getMessage(), "\n";
  }
}

function peu_data($staff, $client, $uid) {
  try {
    if (empty($uid)) {
      return;
    }
    $db = _get_db();

    if (! empty($staff)) {
      $db->insert("peu_staff", [
        "uid" => $uid,
        "data" => json_encode($staff)
      ]);
    }
    if (! empty($client)) {
      $db->insert("peu_client", [
        "uid" => $uid,
        "data" => json_encode($client)
      ]);
    }
  }
  catch(Exception $e)
  {
    echo 'Error writing to databse: ', $e->getMessage(), "\n";
  }
}

function response_update() {
  $uid = $_POST['GUID'];
  $url = $_POST['url'];
  $programs = $_POST['programs'];
  if (empty($uid) || empty($url) || empty($programs)) {
    wp_send_json(["status" => "fail","message" => "missing values"]);
    return wp_die();
  }

  try {
    $db = _get_db();
    $db->insert("response_update", [
      "uid" => $uid,
      "url" => $url,
      "program_codes" => $programs
    ]);
    wp_send_json(["status" => "ok"]);
    wp_die();
  }
  catch(Exception $e)
  {
    echo 'Error writing to database: ', $e->getMessage(), "\n";
  }
}

Why is the try and catch not working? What can I do to solve this issue? this post didn't work. PHP try/catch and fatal error

Your code is in a custom namespace:

namespace StatCollector;

so this code:

catch (Exception $ex)

is looking for an exception class of \StatCollector\Exception, which isn't what's thrown: \Exception is.

Since the Error and Exception classes are in the root namespace, you need to do:

catch (\Exception $ex)

and/or:

catch (\Error $ex)

or just:

catch (\Throwable $t)

then also follow Bill Karwin's advice above.