php - PHP:将 WP_ERROR 转换为异常
问题描述
我目前正在努力为以下情况添加错误处理。当数据库在有权访问$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()
这个条件是否为真我抛出一个异常。然而,这并没有奏效。我认为这就是处理 a 的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";
}
}
解决方案
wpdb::insert
出错时不返回 WP_Error。出错时返回 boolean false
。错误打印是在wpdb::query
自身内部完成的,但您可以通过设置suppress_errors
为 true 来禁用它,然后使用该last_error
属性优雅地获取上一个错误。
$db->suppress_errors(true);
//Note: If you still want to log the errors to your server log
//use $db->hide_errors(); instead.
$insertion = $db->insert("requests", [
"uid" => $uid,
"data" => json_encode($data),
]);
if( $insertion === false ) {
throw new \Exception('Error writing to the database: ' . $db->last_error);
}
推荐阅读
- excel - Excel:准备直方图数据
- angular - Angular 6单选按钮无法使用名称对其进行分组
- python - 无法导入 pymc3:AttributeError:模块“theano”没有属性“gof”
- ios - Swift JSONDecoder decode(type:from) 无法识别我的数据
- javascript - 将圆环图修改为“进度圆环图”
- airflow - 气流任务因“调度程序心跳出现异常”错误而被杀死
- ios - Swift iOS - 将数据作为包含自定义模型的自定义模型添加到 Firestore
- node.js - 命令插入需要身份验证
- matlab - 在耦合非线性 ode 中为恢复动态添加噪声
- c++ - 无法通过 std::vector 同步 std::thread