簡體   English   中英

PHP 致命錯誤:未捕獲錯誤:Class 未找到異常

[英]PHP Fatal error: Uncaught Error: Class Exception not found

我在嘗試向 PHP 添加錯誤處理時遇到了障礙。 目前,當 $db 嘗試insert->寫入catch --read-only的數據庫時,我嘗試並try處理錯誤。 我收到以下錯誤:

PHP 致命錯誤:未捕獲的錯誤:Class 'StatCollector\Exception' 未在 wp-content/mu-plugins/stat-collector/StatCollectorFunctions.ZE1BFD762321E409CEE4ZAC0B6E8 中找到

<?php

namespace StatCollector;

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

正如您從上面的代碼片段中看到的,我有一個catch(\Exception $e)應該捕獲在throw new Exception 但是,情況並非如此,我在 WP Engine 上收到錯誤,而應用程序沒有工作。

為什么在這種情況下,try and catch 不起作用? 這是完整的 class:

<?php

namespace StatCollector;

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

function _get_db() {
  $host = get_option('statc_host');
  $database = get_option('statc_database');
  $user = get_option('statc_user');
  $password = get_option('statc_password');
  $bootstrapped = get_option('statc_bootstrapped');

  $host = (!empty($host)) ? $host : $_ENV['STATC_HOST'];
  $database = (!empty($database)) ? $database : $_ENV['STATC_DATABASE'];
  $user = (!empty($user)) ? $user : $_ENV['STATC_USER'];
  $password = (!empty($password)) ? $password : $_ENV['STATC_PASSWORD'];
  $bootstrapped = (!empty($bootstrapped)) ? $bootstrapped : $_ENV['STATC_BOOTSTRAPPED'];

  if (empty($host) || empty($database) || empty($user) || empty($password)) {
    error_log('StatCollector is missing database connection information. Cannot log');
    return new MockDatabase();
  }

  $db = new \wpdb($user, $password, $database, $host);
  $db->show_errors();

  if ($bootstrapped !== '5') {
    __bootstrap($db);
  }
  return $db;
}

你在一個命名空間中:

namespace StatCollector;

因此,當您執行此操作時,您會得到一個\StatCollector\Exception object:

throw new Exception()

您只需要在實例化時將異常 class 錨定到根命名空間:

throw new \Exception();

剛過

namespace StatCollector; 添加

use \Exception;

或在拋出異常時放置異常的絕對路徑

throw new \Exception();

兩者都會起作用。

暫無
暫無

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

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