В настоящее время я пытаюсь добавить обработку ошибок для следующего случая.Возникает ошибка, когда база данных $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()
, выполняется ли это условие, я выкидываю исключение.Однако это не сработало.Я думал, что так можно поступить с 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";
}
}