【发布时间】:2020-01-21 16:21:57
【问题描述】:
我发布了以下问题PHP & WP: try catch not working when error from DB is thrown。目前我正在尝试防止应用程序在数据库为--read-only 时崩溃。
目前,我收到以下错误:
WordPress 数据库错误 INSERT 命令拒绝用户 'readonly'@'10.XXX.XX.XX' for table 'responses' for query INSERT INTO
responses(uid,data) 值
当具有--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 引擎上的日志显示 'Error writing to database: ' 但是,这并没有发生,我看到的是 WordPress database error INSERT command denied to user ...
为什么在这种情况下错误处理不起作用?无法写入数据库不应阻止网站工作。这是完整的课程。
<?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;
}
错误处理如何工作?
【问题讨论】:
-
请不要多次发布同一个问题。使用您的任何更新编辑原始问题。
-
这是一个完全不同的问题。这与第一篇文章中发布的
try和catch无关。这是关于wp_error没有引发错误。我强烈反对编辑第一个问题,第一个帖子的发布答案是正确的,但不能解决整体问题。 -
重新打开的问题。