Steven Aguilar
Steven Aguilar

Reputation: 3047

PHP Fatal error: Uncaught Error: Class Exception not found

I'm getting roadblocks trying to add error handling to PHP. Currently I have a try and try and catch to handle an error when a $db is trying to insert-> write into a DB that is --read-only. I get the following error:

PHP Fatal error: Uncaught Error: Class 'StatCollector\Exception' not found in wp-content/mu-plugins/stat-collector/StatCollectorFunctions.php

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

As you see from the above snippet I have a catch(\Exception $e) that should be catching the Exception generated on the throw new Exception. However this is not the case and I get an error on WP Engine, without the application working.

Why is this the try and catch not working in this case? Here is the full 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;
}

Upvotes: 1

Views: 3337

Answers (2)

Robert
Robert

Reputation: 20286

Just after

namespace StatCollector; add

use \Exception;

or put the absolute path to exception when you throw it

throw new \Exception();

Both will work.

Upvotes: 2

Alex Howansky
Alex Howansky

Reputation: 53646

You're in a namespace:

namespace StatCollector;

So you get a \StatCollector\Exception object when you do this:

throw new Exception()

You just need to anchor the exception class to the root namespace when you instantiate it:

throw new \Exception();

Upvotes: 7

Related Questions