Thursday, September 10th, 2009

Formaldehyde: PHP debug info for the client side

Category: Debugging, PHP

<>p>

Andrea Giammarchi has released Formaldehyde, a new Ajax and PHP error debugger.

Simply grab the project and throw in an inclusion:

  1. < ?php require_once 'formaldehyde.php'; ?>

You are off to the races.

Want to do a touch more?

javascript
< view plain text >
  1. // *optional* custom ServerError constructor
  2. function ServerError(e){
  3.     for(var key in e)
  4.         this[key] = e[key]
  5.     ;
  6. };
  7. // make Firebug the best friend ever
  8. (ServerError.prototype = new Error).name = "ServerError";
  9.  
  10. // same call
  11. var xhr = new XMLHttpRequest;
  12. xhr.open("get", "test.php", true);
  13. xhr.onreadystatechange = function(){
  14.     if(xhr.readyState === 4){
  15.         if(199 < xhr.status && xhr.status < 400){
  16.             // do something without failures
  17.             eval(xhr.responseText);
  18.         }
  19.        
  20.         // if Formaldehyde managed the call
  21.         else if(xhr.status === 500 && xhr.getResponseHeader("X-Formaldehyde") != null) {
  22.        
  23.             // evaluate safely the response
  24.             // generating a proper error
  25.             console.log(new ServerError(eval("(" + xhr.responseText + ")")));
  26.  
  27.         } else {
  28.             // 404 or other cases
  29.             console.log(new Error(xhr.responseText));
  30.         }
  31.     }
  32. };
  33. xhr.send(null);

We have known about the great FirePHP for quite some time, but this is different as explained. Check it out!

Related Content:

3 Comments »

Comments feed TrackBack URI

Thanks Dion, just as extra note: this evening I am planning to create a cross-browser JavaScript file to include if necessary in order to make responses management automatic and possibly compatible with every library as well without changing a single piece of code ( it’s a XMLHttpRequest / ActiveXObject wrapper )
That should make this little project complete so please stay tuned

Comment by WebReflection — September 10, 2009

Ok, I’m impressed.

Comment by Darkimmortal — September 10, 2009

As promised, Formaldehyde JS cross-browser, same logic (include as first file in the page that will perform Ajax calls, and that’s it, do not require any change in your existent JS code)
http://code.google.com/p/formaldehyde/

I am writing down some documentation about.
Regards

Comment by WebReflection — September 10, 2009

Leave a comment

You must be logged in to post a comment.