Connecting Tech Pros Worldwide Forums | Help | Site Map

How to trigger an error inside a function and make it come from whereit was called?

Bruno Rafael Moreira de Barros
Guest
 
Posts: n/a
#1: Jan 14 '08
function test1() {
trigger_error('My error');
}

application.php

//code...
test1();
//code...

I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.

PHP does it, when you call any PHP function and it errors, it says it
came from the file that called it. In my own functions, it says it
came from the declaration... which is kind of useless because that is
what I already know... but when in the application it happened...
that's what's hard to find out.

flowover
Guest
 
Posts: n/a
#2: Jan 14 '08

re: How to trigger an error inside a function and make it come from whereit was called?


http://ca.php.net/manual/en/language.exceptions.php

On Jan 14, 3:08 am, Bruno Rafael Moreira de Barros
<brunormbar...@gmail.comwrote:
Quote:
function test1() {
trigger_error('My error');
>
}
>
application.php
>
//code...
test1();
//code...
>
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.
>
PHP does it, when you call any PHP function and it errors, it says it
came from the file that called it. In my own functions, it says it
came from the declaration... which is kind of useless because that is
what I already know... but when in the application it happened...
that's what's hard to find out.
Bruno Rafael Moreira de Barros
Guest
 
Posts: n/a
#3: Jan 14 '08

re: How to trigger an error inside a function and make it come from whereit was called?


On Jan 14, 11:17 am, flowover <flowover...@gmail.comwrote:
Quote:
http://ca.php.net/manual/en/language.exceptions.php
>
On Jan 14, 3:08 am, Bruno Rafael Moreira de Barros
>
<brunormbar...@gmail.comwrote:
Quote:
function test1() {
trigger_error('My error');
>
Quote:
}
>
Quote:
application.php
>
Quote:
//code...
test1();
//code...
>
Quote:
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.
>
Quote:
PHP does it, when you call any PHP function and it errors, it says it
came from the file that called it. In my own functions, it says it
came from the declaration... which is kind of useless because that is
what I already know... but when in the application it happened...
that's what's hard to find out.
So I would do:

try {

//code...
test1();
//code...

} catch...

Is that right?
C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
#4: Jan 14 '08

re: How to trigger an error inside a function and make it come from whereit was called?


On 14 Jan, 11:08, Bruno Rafael Moreira de Barros
<brunormbar...@gmail.comwrote:
Quote:
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.
>
These *are* the parameters to the customer error handler.

from http://uk.php.net/manual/en/function...or-handler.php

The third parameter is optional, errfile , which contains the
filename that the error was raised in, as a string.
The fourth parameter is optional, errline , which contains the line
number the error was raised at, as an integer.

See also:
http://uk.php.net/debug_backtrace

C.
Bruno Rafael Moreira de Barros
Guest
 
Posts: n/a
#5: Jan 15 '08

re: How to trigger an error inside a function and make it come from whereit was called?


On Jan 14, 1:56 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
Quote:
On 14 Jan, 11:08, Bruno Rafael Moreira de Barros
>
<brunormbar...@gmail.comwrote:
Quote:
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.
>
These *are* the parameters to the customer error handler.
>
fromhttp://uk.php.net/manual/en/function.set-error-handler.php
>
The third parameter is optional, errfile , which contains the
filename that the error was raised in, as a string.
The fourth parameter is optional, errline , which contains the line
number the error was raised at, as an integer.
>
See also:http://uk.php.net/debug_backtrace
>
C.
Yes, but the problem is, the error line and file equal to the ones
that lead you to the declaration of the function...

So if you have:

test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error
test1(); // trigger the error

And you check the error log, all the errors will have come from the
same place, from inside test1(). But I tested try...catch and it
worked, and it even solved further problems I had.
Rik Wasmus
Guest
 
Posts: n/a
#6: Jan 15 '08

re: How to trigger an error inside a function and make it come from whereit was called?


On Tue, 15 Jan 2008 14:51:35 +0100, Bruno Rafael Moreira de Barros
<brunormbarros@gmail.comwrote:
Quote:
On Jan 14, 1:56 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
Quote:
>On 14 Jan, 11:08, Bruno Rafael Moreira de Barros
>>
><brunormbar...@gmail.comwrote:
Quote:
I want the trigger_error to say the error came from application.php
and the line where it came from. I have a custom error handler, so if
some change on the error handler (or test1(), for that matter) is
needed, I will gladly do it.
>>
>These *are* the parameters to the customer error handler.
>>
>fromhttp://uk.php.net/manual/en/function.set-error-handler.php
>>
> The third parameter is optional, errfile , which contains the
>filename that the error was raised in, as a string.
> The fourth parameter is optional, errline , which contains the line
>number the error was raised at, as an integer.
>>
>See also:http://uk.php.net/debug_backtrace
>
Yes, but the problem is, the error line and file equal to the ones
that lead you to the declaration of the function...
Check the link provided to you, the custom handler could examine the
contents of debug_backtrace(), and optionally provide a whole trace, or it
could 'skip' the last step in the backrace based on the function name.
Quote:
And you check the error log, all the errors will have come from the
same place, from inside test1(). But I tested try...catch and it
worked, and it even solved further problems I had.
Hmmm, I like Exceptions indeed :).
--
Rik Wasmus
Closed Thread