簡體   English   中英

PHP:將 WP_ERROR 轉換為異常

[英]PHP: Converting WP_ERROR to Exception

我目前正在努力為以下情況添加錯誤處理。 $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()如果這個條件為真我拋出一個異常。 然而,這並沒有奏效。 我認為這就是 go 關於處理WP_ERROR的方式,但我想知道是否有另一種方法來處理此類錯誤。 這是完整的 class:

<?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";
  }
}

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM