尝试捕获不在PHP中工作?

时间:2022-10-28 13:27:28

I'm working on web-service.

我正在从事网络服务。

I'm trying to catch error. My problematic code is:

我想抓住错误。我有问题的代码是:

    try
    {
    $query = "UPDATE Users
              SET 
                Latitude=?,
                Longitude=?,
                Address=?,
                LocationTimestamp=?
              WHERE Id=?";

        $stmt = $this->link->prepare($query);
        $stmt->bind_param('ddssi', $Latitude, $Longitude, $Address, $LocationTimestamp, $Id);
        $stmt->execute();


        $affected_rows = $stmt->affected_rows;

    }

    catch(Exception $exc)
    {
        file_put_contents("log/error.txt",$exc->getMessage());
    }

I expected, that catch block will catch all errors and PHP will not produce any errors on output. However - on output I see something like this:

我预计,catch块会捕获所有错误,PHP不会在输出上产生任何错误。但是 - 在输出上我看到这样的事情:

Warning: Creating default object from empty value in /srv/webservice/server.php on line 119

警告:在第119行的/srv/webservice/server.php中从空值创建默认对象

I want to avoid any HTML on output, because this is web-service and I have JSON interpreter on client side.

我想避免输出上的任何HTML,因为这是web服务,我在客户端有JSON解释器。

My question is:

我的问题是:

How to debug service like this, when I have no access to PHP output? I would like to redirect all errors, warnings etc. to file.

当我无法访问PHP输出时,如何调试这样的服务?我想将所有错误,警告等重定向到文件。

2 个解决方案

#1


You get an PHP warning, not an Exception. Maybe this helps to save your errors direct:

你得到一个PHP警告,而不是异常。也许这有助于直接保存您的错误:

ini_set("log_errors", true);
ini_set("error_log", "log/error.txt");

This logs all PHP errors and warnings (and notices) into this file.

这会将所有PHP错误和警告(和通知)记录到此文件中。

After the code block, you can disable it, if you want so:

在代码块之后,如果您愿意,可以禁用它:

ini_set("log_errors", false);

#2


A Warning is not an Exception..... you can catch Exceptions, but not Warnings/Notices/Errors/etc. If you want to make warnings catchable you need to convert them to Exceptions with a user defined error handler using set_error_handler()

警告不是例外.....你可以捕获异常,但不能捕获警告/通知/错误/等。如果要使警告可捕获,则需要使用set_error_handler()将它们转换为具有用户定义的错误处理程序的异常

class MyCustomException extends Exception {
    public static function errorHandlerCallback($code, $string, $file, $line, $context) {
        $e = new self($string, $code);
        $e->line = $line;
        $e->file = $file;
        throw $e;
    }
}

set_error_handler(['MyCustomException', 'errorHandlerCallback'], E_ALL);

#1


You get an PHP warning, not an Exception. Maybe this helps to save your errors direct:

你得到一个PHP警告,而不是异常。也许这有助于直接保存您的错误:

ini_set("log_errors", true);
ini_set("error_log", "log/error.txt");

This logs all PHP errors and warnings (and notices) into this file.

这会将所有PHP错误和警告(和通知)记录到此文件中。

After the code block, you can disable it, if you want so:

在代码块之后,如果您愿意,可以禁用它:

ini_set("log_errors", false);

#2


A Warning is not an Exception..... you can catch Exceptions, but not Warnings/Notices/Errors/etc. If you want to make warnings catchable you need to convert them to Exceptions with a user defined error handler using set_error_handler()

警告不是例外.....你可以捕获异常,但不能捕获警告/通知/错误/等。如果要使警告可捕获,则需要使用set_error_handler()将它们转换为具有用户定义的错误处理程序的异常

class MyCustomException extends Exception {
    public static function errorHandlerCallback($code, $string, $file, $line, $context) {
        $e = new self($string, $code);
        $e->line = $line;
        $e->file = $file;
        throw $e;
    }
}

set_error_handler(['MyCustomException', 'errorHandlerCallback'], E_ALL);