gpt4 book ai didi

php - 确定在哪里捕获php/codeigniter错误

转载 作者:行者123 更新时间:2023-12-03 08:58:45 27 4
gpt4 key购买 nike

我试图在我的Web应用程序中捕获所有意外错误消息,并向最终用户显示通用消息。但是截至目前,我的页面上出现了几条php/codeigniter错误消息。

我的 Controller 中具有以下逻辑:

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)”,这是我所期望的。

但是我正在另一台Web服务器上尝试相同的逻辑,现在,我收到了一堆其他错误消息:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: switches::$_password

Filename: core/Model.php

Line Number: 51 A PHP Error was encountered

Severity: Notice

Message: Undefined property: switches::$_userId

Filename: core/Model.php

Line Number: 51 switches_model get_logs e handler A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /myapp/system/core/Exceptions.php:185)

Filename: core/Common.php

Line Number: 438 An Error Was Encountered

Unable to connect to device. Please contact administrator! (1)



我也想知道CI发出的这些消息是什么意思...但是我可以用Google搜索它们。现在,我只是想知道如何防止这些消息显示给用户。我只想让他们登录。
在我用于CI的config.php中,我有$ config ['log_threshold'] = 1;

谢谢。

编辑1

我弄清楚了为什么会出现错误-这是因为我没有正确声明2个变量。但是,如果您可以帮助我解决问题,以防止在GUI上出现意外错误,那将不胜感激!

最佳答案

看起来好像是来自CodeIgniter的,但实际上是来自PHP的。一般而言,您可能应该阅读PHP错误处理,但是喜欢CI的人可以阅读here is a 50000 ft overview。通常,对开发人员的错误报告应尽可能严格,在生产时,错误日志应记录到文件中(如果可能,则永远不要显示给最终用户)。

关于php - 确定在哪里捕获php/codeigniter错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14471002/

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