My-Bic = My Beagle is Cute!

Debugging

If you wish to debug your scripts to see the request/responses just turn on debugging like this:
  1.  
  2. var ajaxObj = new XMLHTTP("mybic_server.php");
  3. ajaxObj.debug=1;

That will show a debug screen similar to the one shown below right on your webpage. I was going to make it draggable but it would have added alot more code and I'm trying to keep this light and fast:



You can also use this in your own scripts! No more alert boxes my friend. Use My-Bic's built in print debugger and you can see your data in a collapsable format just like My-Bic uses.

Let's say you have a function in your page you want to test

  1.  
  2. function testy(status)
  3. {
  4. if(ajaxObj.debug==1) {
  5. ajaxObj.showDebug("new", "Testy Function");
  6. ajaxObj.showDebug("Status is: "+status+"<br>");
  7. }
  8. }

There you have it! now it will show up with all the other debug messages!. Notice how I check to see if debug is turned on first. This is a good idea for performance so you're not calling the showDebug function if debug is off. The function will double check just in case you forget but its a good idea to check yourself.



Error Handling

Currently Error Handling is pretty simple. If your PHP file cannot be found or the user is not authorized the mybic_server page returns false, if there are users errors then you can create an array of errors and pass those back to your script. So MyBIC handles fatal errors by returning false.

Validation / Misc Errors

If you wish to return meaningful error messages, the easiest way to do it is by returning an array of errors. Let's pretend you're letting users submit comments and you want to make sure the name field is at least 3 characters long. Your postComment.php script might look like this:

  1.  
  2. public function return_response()
  3. {
  4. $resp ='';
  5. if(strlen($this->queryVars['name']) < 3) {
  6. $resp['errors'][] = "Name field is too short, dude";
  7. } else {
  8. // save the comment
  9. $resp = "comment submitted";
  10. }
  11. return $resp;
  12. }

As you can see above the if we detect the queryVars element "name"'s string length is less than 3 characters we create an array called "errors" to store our error in. Now in your javascript if you want to detect that error you just need to do this in your callback function

  1.  
  2. function respComment(resp) {
  3. if(resp) {
  4. if(resp.errors) {
  5. showErrors(resp.errors);
  6. } else {
  7. // data is good, load 'er up!
  8. }
  9. }
  10. }

As you can see we first test for an exception with is(resp) and then test for any user errors with if(resp.errors). resp.errors is now a javascript array filled with any errors we created in PHP

Fatal Errors

If you're script comes across a fatal error, mybic will try to log the error in your /logs file if you have a /logs directory and the permissions to write to it.

  1.  
  2. // log no action found
  3. logErrorToFile("No Action Found that matches query string: . This means the server cannot find your PHP class file, check your paths");
  4. echo "ajax_ msg_failed|No Action Found that matches query string: . This means the server cannot find your PHP class file, check your paths";
If there is interest I will expand the error handling so it sends more responsive messages back to the javascript. I return false so that you can have the freedom to do unique error handling for each of your applications that you may be using.