0

I posted the following question PHP & WP: try catch not working when error from DB is thrown. Currently I'm trying to prevent the application from crashing when the database is --read-only.

Currently, I get the following error:

WordPress database error INSERT command denied to user 'readonly'@'10.XXX.XX.XX' for table 'responses' for query INSERT INTO responses (uid, data) VALUES

This happens when a database that has --read-only access is trying to insert data into it. During a cluster fail over out writer database became the reader and therefore this issue happen. I try to write error handling for it without success.

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

The variable $db is a database from an AWS RDS endpoint, which had --read-only accesss. So when $db->insert throws an error I expect the logs on WP engine to have shown 'Error writing to database: ' however, this didn't happen and what I saw was WordPress database error INSERT command denied to user ...

Why isn't the error handling working in this case? Not being able to write into the database shouldn't stop the website from working. 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( is_wp_error( $insertion ) ) {
      echo $return->get_error_message();
    }
  }
  catch(\Exception $e)
  {
    echo 'Error writing to 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 ) ) {
      echo $return->get_error_message();
    }
  }
  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();
    $insertion = $db->insert("messages", [
      "uid" => $uid,
      "msg_type" => strtolower($type),
      "address" => $to,
      "url" => $url,
      "message" => $message
    ]);
    if( is_wp_error( $insertion ) ) {
      echo $return->get_error_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)) {
      $insertion = $db->insert("peu_staff", [
        "uid" => $uid,
        "data" => json_encode($staff)
      ]);
    }
    if( is_wp_error( $insertion ) ) {
      echo $return->get_error_message();
    }
    if (! empty($client)) {
      $insertion = $db->insert("peu_client", [
        "uid" => $uid,
        "data" => json_encode($client)
      ]);
    }
    if( is_wp_error( $insertion ) ) {
      echo $return->get_error_message();
    }
  }
  catch(\Exception $e){
    echo 'Error writing to 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 )) {
      echo $return->get_error_message();
    }
  }
  catch(\Exception $e)
  {
    echo 'Error writing to 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;
}

How can can error handling to work?

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Steven Aguilar
  • 3,107
  • 5
  • 39
  • 89
  • Pls do not post the same question multiple times. Edit the original question with any updates you have. – Shadow Sep 20 '19 at 16:56
  • This is a completely different question. This is not regarding `try` and `catch` as posted in the first post. This is about `wp_error` not raising an error. I strongly disagree with editing the first question, the posted answers to the first post were correct but not a solution to the overall problem. – Steven Aguilar Sep 20 '19 at 18:18
  • Reopened question. – Bill Karwin Sep 20 '19 at 22:19

1 Answers1

0

Because as per the documentation an insert on the wp_db class does not throw an error if it failed.

It just returns false. A try..catch block will catch errors that are thrown.

In this case you should just check if the result of the insert was === false.

jake2389
  • 1,166
  • 8
  • 22