Throwing errors from its “correct” source

I hope the title is not too confusing, I will try to explain below.

Suppose I have a function in a separate file, functions.php:

function divide($num1, $num2) { if ($num1 == 0 || $num2 == 0) { trigger_error("Cannot divide by 0", E_USER_ERROR); } else { return ($num1 / $num2); } } 

And another file that calls it:

 include "functions.php"; echo divide(10, 0); 

My mistake

Fatal error: cannot divide by 0 in C: \ Users \ Derek \ Desktop \ projects \ functions.php on line 5

My question is: how can I make this error, instead I will indicate the location of the error in the main code, so I get instead:

Fatal error: cannot divide by 0 in C: \ Users \ Derek \ Desktop \ projects \ main.php on line 3

The specific reason I want this is because I have a load_class function that simply finds the PHP file and creates an instance of the object inside, but if it is given the wrong file name, it reports an error from within load_class, which is technically correct but this is not particularly useful if I don’t remember where I called load_class in the first place. I would like the error to point to a file called load_class.

In addition, I would like to write the error () function (something like below) that, when a message is provided as a parameter, more "meaningful" error messages will be generated, but when this is done, the error always says that it comes from error (), not where the error came from!

For example, in the error.php file:

 /** * error() * * Throws an error of a certain type * * @param string $type The type of error. "Fatal", "warning," or "notice" * @param string $message A description of the error * @return void */ function error($type, $message) { switch (strtolower($type)) { case 'fatal': trigger_error($message, E_USER_ERROR); break; case 'notice': trigger_error($message, E_USER_NOTICE); default: trigger_error($message, E_USER_WARNING); break; } } 

And in index.php

 error("fatal", "A sample warning!"); 

My mistake:

Fatal error: selective warning! in C: \ Users \ Derek \ Desktop \ projects \ summary \ sys \ Error.php on line 45

But the error did not happen in error.php, it happened in index.php! How can I show where it came from?

+4
source share
5 answers

The debug_backtrace function allows you to get the stack as an array. You can select the source location.

Next to this, you need to slip into the error message to make it look like. Example:

 function divide($num1, $num2) { if ($num1 == 0 || $num2 == 0) { trigger_error_original("Cannot divide by 0", E_USER_ERROR); } else { return ($num1 / $num2); } } function trigger_error_original($message, $type) { $trace = debug_backtrace(FALSE); list($location) = array_slice($trace, 1, 1) + array('file' => 'unknown', 'line' => 'unknown'); $message .= sprintf(" in %s on line %d\nTriggered", $location['file'], $location['line']); trigger_error($message, $type); } divide(1, 0); 

An error message than shown something like this:

 > php test-pad.php Fatal error: Cannot divide by 0 in test-pad.php on line 18 Triggered in test-pad.php on line 15 

The disadvantage of this is that you need to change your code in order to have this “function”. If you need this to debug your own code, it is much better to include inclusions in your logs. the Xdebug extension does this for you, or you can write your own error handler that takes care of this.

See also related question Caller Function in PHP 5? . I used array_slice so that you can create an extra parameter to determine the number of steps you want up in the backtrace.

+2
source

Use debug_backtrace() and debug_print_backtrace() for a full call stack. They are especially effective when using Xdebug , which will override the function for coloring the output.

0
source

I have the same problem...

# 1 : while 10/0 = ERROR, 0/10 = 0 is completely legal, you should not have an exception for this.

# 2 : when you include a file, it becomes part of this new file, so you may have to play a bit with things like __FILE__ and see if you can make it point it to the file before it is included in another file.

0
source

You can use xdebug - it will show you the stack, or you can register your own error descriptor and display the stack. Just check the example in php.net for set_error_handler ().

0
source

Maybe exceptions best used in your case. You get the full stack and can find where the function was called without relying on some tricky code :)

0
source

Source: https://habr.com/ru/post/1399662/


All Articles