douwanc63652 2012-08-13 13:16
浏览 59

Php:如何捕获错误报告?

I am working on a php script that that is invoked by ajax request, and is supposed to echo some response code to be parsed.

For example:

die('#1:200|Success|'.$data);

Or another one:

die('#0:100|Access Denied|');

Now, I want also to include any errors or warnings that might have occurred during the execution of the script - BUT appended at the end of the message.

So, what would be an elegant way of capturing all the errors into some variable?

EDIT

Well It isn't easy to understand how it meant to be used, the manual isn't clear on many things.

But ok, I will try to make an example of how I understand it, then please point it out if I'm getting it wrong :-)

//So guess first I off error reporting that would naturally occur.
error_reporting(0);

//Then I will define array to stuff the errors in.
$errors=array();

//Then I make my handler function.
function handler($errno,$errstr){
    global $errors;
    $errors[]=$errno.': '.$errstr;    //Stuff it into array.
}

//Then I define handler.
set_error_handler('handler',E_ALL);

Is this correct usage?

It also says:

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

And also question, why it wouldn't capture strict errors?

  • 写回答

1条回答 默认 最新

  • dongtan1009 2012-08-13 13:34
    关注

    I'm always require this to capture errors while ajaxing:

    header('content-type: application/json; charset=utf-8');
    error_reporting(E_ALL);ob_start();
    
    function error($msg,$do = false)
    {
        //personal error message
        if(!isset($_SESSION))session_start();       
        trigger_error($msg."
    ".(isset($_SESSION)?"[".$_SESSION['id']."|".$_SESSION['name']."]":"")."-----------");
    
        ob_clean(); 
        die(json_encode(array($msg,$do)));
    }
    function ob_error($msg = "Error!",$do = "ob_error")
    {
        if($s = ob_get_clean())
        error("$msg
    Details:
    $s",$do);
    }
    

    usage:

    //require the php above
    
    //do something
    
    //call error("acces denied") if there is an fatal error
    
    //do anything else
    
    //call ob_error() at the end: if there was anything outputted like warning/notice it will shown
    
    //call die(json_encode(array(1, ...anything you need*...))); - this will run only if there was nothing displayed
    

    client site usage:

    $.post('/_ajax/... .php',{
                'param1':...
                },function(a){
                    if(a[0]=="1") //OK
                    {                   
                        //* do something with a[n]                  
                    }else{ //onError
                        alert(a[0]); 
                        //what to do client site after the error
                        if(a[1]=="refresh") 
                            location.href=location.href;
                    }
                },"json").error(function() {alert("postError"));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)