How to trigger an error inside a function and make it come from where

How to trigger an error inside a function and make it come from where

am 14.01.2008 12:08:56 von brunormbarros

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.

Re: How to trigger an error inside a function and make it come from

am 14.01.2008 12:17:27 von flowover

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

On Jan 14, 3:08 am, Bruno Rafael Moreira de Barros
wrote:
> 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.

Re: How to trigger an error inside a function and make it come from

am 14.01.2008 12:29:04 von brunormbarros

On Jan 14, 11:17 am, flowover wrote:
> http://ca.php.net/manual/en/language.exceptions.php
>
> On Jan 14, 3:08 am, Bruno Rafael Moreira de Barros
>
> wrote:
> > 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.

So I would do:

try {

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

} catch...

Is that right?

Re: How to trigger an error inside a function and make it come from

am 14.01.2008 14:56:33 von colin.mckinnon

On 14 Jan, 11:08, Bruno Rafael Moreira de Barros
wrote:
> 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.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.

Re: How to trigger an error inside a function and make it come from

am 15.01.2008 14:51:35 von brunormbarros

On Jan 14, 1:56 pm, "C. (http://symcbean.blogspot.com/)"
wrote:
> On 14 Jan, 11:08, Bruno Rafael Moreira de Barros
>
> wrote:
> > 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.p hp
>
> 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.

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

am 15.01.2008 14:58:01 von luiheidsgoeroe

On Tue, 15 Jan 2008 14:51:35 +0100, Bruno Rafael Moreira de Barros
wrote:

> On Jan 14, 1:56 pm, "C. (http://symcbean.blogspot.com/)"
> wrote:
>> On 14 Jan, 11:08, Bruno Rafael Moreira de Barros
>>
>> wrote:
>> > 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.p hp
>>
>> 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.

> 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