Skip to main content

Distinguish new vs existing exceptions



I am creating a PHP application and I want to display the number of times an error has occurred. The problem that I am trying to figure out is if an error has already been reported or if its new using the following values:





  1. Message (ie: Attempted to divide by zero.)



  2. Stack trace (ie: at componentNETapp.Form1.btnTrackExceptionsUn_Click(Object sender, EventArgs e) ...)



  3. Source (ie: componentNETapp)



  4. Target site (ie: Void btnTrackExceptionsUn_Click(System.Object, System.EventArgs))







Thanks


Comments

  1. First of all Errors or Exceptions? PHP differs between errors and exceptions. Generally the Error Handling and Logging­Docs section of the manual might be of interest.

    For Errors:

    error_get_last­Docs will return the last error with the following information: Type (integer), Message (string), File (string) and Line (integer). I would say this is self explanatory. You might also want to look into set_error_handler­Docs which will - next to these four bits of information - also offer you an error context you could make use of.

    If you need more information about an error, there is also the backtrace in PHP debug_backtrace­Docs which can add even more context.

    For Exceptions:

    Each Exception­Docs has similar members: Message (string), Code (integer), File (string) and Line (integer). It also has a Trace which contains more information and could be used as context. It's also possible to set a previous exception to nest exceptions. Like the the error handler, it's possible to use an exception handler as well: set_exception_handler­Docs.



    With the information given, you should be able to handle and log all errors/exceptions in a manner that you can analyse them later on. Next to that it's also possible to make use of PHP error logging and to store those errors into a database. See Outputting all PHP errors to database not error_log for a simple example. I would say the key point is that you store the error information in a more or less normalized form, so that you can define criteria later on how to count/group them.

    If you're looking for more infrastructure, take a look into the apache logging services project that offers some tools (e.g. a viewer) and a PHP logger exists for it, too (log4php).

    But this is just exemplary, other tools exist as well, some of them are using a MySQL backend and you can not only track PHP but also other errors.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.