nice ass
nice ass

Reputation: 16719

Can I detect if a function was called with the error control operator (@)?

I have custom error handler that is supposed to stop the execution of the application when an uncaught exception is thrown or some error/warning/notice is generated, display a nice error message, then email the developer about it.

Everything works well except that I can't find a way to ignore warnings that were generated by a function that was called with @ (ftp_login)

debug_backtrace() shows the function name, but not @. Is there a way to detect it, or do I have to hard code the function name into the error handler?

Upvotes: 2

Views: 106

Answers (2)

Rodolpho Freire
Rodolpho Freire

Reputation: 210

From PHP Manual: https://www.php.net/manual/en/language.operators.errorcontrol.php

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

If you have set a custom error handler function with set_error_handler() then it will still get called, but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @.

Just use "error_reporting() === 0" in your error handler to know where a function is preceded by a @

Upvotes: 4

AbraCadaver
AbraCadaver

Reputation: 79014

The only way I can think of if you want to include it in an email is to capture the line from the file. Assuming your error handler function has the signature:

bool handler(int $errno, string $errstr [,string $errfile [,int $errline [,array $errcontext]]])

Try:

$code = file($errfile)[$errline];

You can then include this text or strpos for an @ and decide from there.

Upvotes: 3

Related Questions