我试图在我的网络应用程序中捕获所有意外错误消息并向最终用户显示一般消息。但截至目前,我的页面上出现了几条 php / codeigniter 错误消息。
我的控制器中有以下逻辑:
public function logs()
{
try {
$data['logcontents'] = $this->switches_model->get_logs($this->uri->segment(7), $this->uri->segment(4));
$data['main_content']='logs';
$this->load->view('includes/template', $data);
}
catch (Exception $e) {
show_error($e->getMessage());
}
}// end logs
我的模型看起来像这样:
public function get_logs($dnsName, $switchname)
{
try {
if ( $switch_obj->connect() ) {
//do something
return $data;
}
else {
throw new Exception('Connection');
}//end if
}// end try
catch (Exception $e) {
print 'switches_model get_logs e handler<BR>';
$this->error_handler($e->getMessage());
return false;
}
最后,这就是我模型中的 error_handler() 方法的样子:
private function error_handler($emess)
{
switch ($emess)
{
case 'Connection':
$userfriendlyemess = "Unable to connect to device. Please contact administrator! (1)";
break;
case 'Empty Data Set':
$userfriendlyemess = "Device connected. Unable to get data. Please contact administrator! (3)";
break;
case 'Failure':
$userfriendlyemess = "Oops! Unable to complete command. Please contact administrator! (4)";
break;
//handling unknown errors
default:
$userfriendlyemess = "Oops! Something big just happened. (99)";
log_message('error', $emess); //write to ci logs
break;
}
throw new Exception($userfriendlyemess);
}
我已经在本地测试了这个逻辑,当我模拟连接失败时,我收到一条错误消息,显示消息“无法连接到设备。请联系管理员!(1)”,这是我所期望的。
但我在不同的网络服务器上尝试同样的逻辑,现在,我收到了一堆额外的错误消息:
遇到 PHP 错误
严重性:通知
消息:未定义的属性:switches::$_password
文件名:core/Model.php
行号:51 遇到 PHP 错误
严重性:通知
消息:未定义的属性:switches::$_userId
文件名:core/Model.php
行号:51 switch_model get_logs e handler 遇到 PHP 错误
严重性:警告
消息:无法修改标头信息 - 标头已发送(输出开始于 /myapp/system/core/Exceptions.php:185)
文件名:core/Common.php
行号:438 遇到错误
无法连接到设备。请联系管理员!(1)
我也想知道来自 CI 的这些消息是什么意思……但我可以用谷歌搜索它们。现在,我只是想知道如何防止这些消息出现在用户面前。我只想让他们记录下来。在我的 CI config.php 中,我有 $config['log_threshold'] = 1;
谢谢。
编辑 1
我弄清楚了为什么会出现错误-这是因为我没有正确声明 2 个变量。但是,如果您能帮助我解决问题以防止 GUI 上出现意外错误,那将不胜感激!