gpt4 book ai didi

PHP & 可湿性粉剂 : is_wp_error not working on try and catch

转载 作者:行者123 更新时间:2023-11-29 15:37:58 24 4
gpt4 key购买 nike

我发布了以下问题PHP & WP: try catch not working when error from DB is thrown 。目前,我正在尝试防止应用程序在数据库为 --read-only 时崩溃。

目前,我收到以下错误:

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

当具有--read-only访问权限的数据库尝试向其中插入数据时,就会发生这种情况。在集群故障转移期间,写入器数据库成为读取器,因此会发生此问题。我尝试为其编写错误处理,但没有成功。

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

变量$db是来自AWS RDS端点的数据库,它具有--read-only访问权限。因此,当 $db->insert 抛出错误时,我希望 WP 引擎上的日志显示 'Errorwriting to database: ' 但是,这并没有发生,什么我看到 WordPress 数据库错误 INSERT 命令被拒绝用户...

为什么在这种情况下错误处理不起作用?无法写入数据库不应阻止网站运行。这是完整的类(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;
}

错误处理如何发挥作用?

最佳答案

因为如per the documentation如果失败,wp_db 类上的插入不会引发错误

它只是返回 false。 try..catch block 将捕获抛出的错误。

在这种情况下,您只需检查插入的结果是否为=== false

关于PHP & 可湿性粉剂 : is_wp_error not working on try and catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58031237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com