tags:

views:

32

answers:

1

I have a php application, that, when it encounters an error, will run a function that uses trigger_error() as well as uses the following function to return the error as JSON.

$this->returnError('My error');

In some places in the script, multiple errors occur, but only one gets returned, because the returnError() function will stop the script. So, the errors are not entirely accurate. What I want to do is gather all errors into an array, then at the end of the script send that through JSON so that the use can get nicely formatted JSON of the errors.

So, my question is, what is the best way to go about this? Is this the best idea, or is there a better method of capturing and returning multiple errors?

+1  A: 

you can have a class level array which keeps track of all the errors .. then at the end you only call $this->returnError(); which checks if there are any errors in the array and then returns them.

Sabeen Malik
Ideally this could be incorporated into your standard wrapper for JSON handlers, making it pretty much transparent, just call returnError when there is one and you'll get all the errors the way you'd like.You should of course make sure that you aren't letting bad data slip through by not terminating on error. Maybe by adding a concept of warnings vs. errors.
Tim