我遇到了尝试向PHP添加错误处理的障碍。当前,当$ db试图try
写入catch
的数据库中时,我可以尝试使用insert->
和--read-only
来处理错误。我收到以下错误:
PHP致命错误:未捕获的错误:没有类'StatCollector \ Exception' 在wp-content / mu-plugins / stat-collector / StatCollectorFunctions.php中找到
<?php
namespace StatCollector;
function drools_request($data, $uid) {
try {
$db = _get_db();
$insertion = $db->insert("requests", [
"uid" => $uid,
"data" => json_encode($data),
]);
if( !$insertion ) {
throw new Exception('Error writing to the database:');
}
}
catch(\Exception $e)
{
echo 'Error writing to the database: ', $e->getMessage(), "\n";
}
}
从上面的片段中可以看到,我有一个catch(\Exception $e)
,应该捕获在throw new Exception
上生成的异常。但是事实并非如此,在应用程序无法正常工作的情况下,WP Engine出现错误。
在这种情况下,为什么尝试捕获功能不起作用? 这是完整的课程:
<?php
namespace StatCollector;
function drools_request($data, $uid) {
try {
$db = _get_db();
$insertion = $db->insert("requests", [
"uid" => $uid,
"data" => json_encode($data),
]);
if( !$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( !$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( $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( $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( $insertion ) {
throw new Exception('Error writing to the database.');
}
}
catch(\Exception $e)
{
echo 'Error writing to the 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;
}
答案 0 :(得分:4)
您在名称空间中
namespace StatCollector;
因此,您在执行此操作时会得到一个\StatCollector\Exception
对象:
throw new Exception()
您只需在实例化异常类时将其锚定到根名称空间:
throw new \Exception();
答案 1 :(得分:1)
就在
之后 namespace StatCollector;
添加
use \Exception;
或在抛出异常时将其置于绝对路径
throw new \Exception();
两个都可以。