首页 > 解决方案 > PHP:将 WP_ERROR 转换为异常

问题描述

我目前正在努力为以下情况添加错误处理。当数据库在有权访问$db时尝试写入时会引发错误。--read-only这会在 WP Engine 上导致以下错误。

WordPress database error INSERT command denied to user 'readonly'@'xx.xxx.xx.xx' for table 'responses' for query INSERT INTO `responses`

我不希望这个错误不会破坏应用程序,所以我正在尝试添加错误处理。但是,当我提出异常时,并没有被捕获。我知道这WP_ERROR是不同的,那么如何将一个转换WP_ERROR为异常?

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

这是我迄今为止尝试但没有成功的方法。在这里我检查is_wp_error()这个条件是否为真我抛出一个异常。然而,这并没有奏效。我认为这就是处理 a 的WP_ERROR方式,但我想知道是否有另一种方法来处理这种类型的错误。这是完整的课程:

<?php

namespace StatCollector;

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

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

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

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

    if (! empty($staff)) {
      $insertion = $db->insert("peu_staff", [
        "uid" => $uid,
        "data" => json_encode($staff)
      ]);
    }
    if( is_wp_error( $insertion ) ) {
      throw new \Exception('Error writing to the database:');
    }
    if (! empty($client)) {
      $insertion = $db->insert("peu_client", [
        "uid" => $uid,
        "data" => json_encode($client)
      ]);
    }
    if( is_wp_error($insertion) ) {
      throw new \Exception('Error writing to the database:');
    }
  }
  catch(\Exception $e){
    echo 'Error writing to the database: ',  $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();
    $insertion = $db->insert("response_update", [
      "uid" => $uid,
      "url" => $url,
      "program_codes" => $programs
    ]);
    wp_send_json(["status" => "ok"]);
    wp_die();
    if( is_wp_error($insertion) ) {
      throw new \Exception('Error writing to the database.');
    }
  }
  catch(\Exception $e)
  {
    echo 'Error writing to the database: ', $e->getMessage(), "\n";
  }
}

标签: phpwordpress

解决方案


wpdb::insert出错时不返回 WP_Error。出错时返回 boolean false。错误打印是在wpdb::query自身内部完成的,但您可以通过设置suppress_errors为 true 来禁用它,然后使用该last_error属性优雅地获取上一个错误。

$db->suppress_errors(true);
//Note: If you still want to log the errors to your server log
//use $db->hide_errors(); instead.
$insertion = $db->insert("requests", [
  "uid" => $uid,
  "data" => json_encode($data),
]);
if( $insertion === false ) {
  throw new \Exception('Error writing to the database: ' . $db->last_error);
}

推荐阅读