I did it… I killed NetConnection.Call.BadVersion!

Posted on Saturday 18 June 2005

NetConnection.Call.BadVersion is the error you see in the NetConnection debugger whenever your Remoting service runs into a fatal error; it's THE major pain when working with AMFPHP. The issue is that when there's an error PHP throws garbage into the output stream and Flash chokes on it instead of showing a meaningful error. In writing AMFPHP 1.0 I looked at all the types of errors that cause the dreaded NetConnection.Call.BadVersion error and eliminated a good half using one hack after another. But there are instances where fatal errors are issued by PHP and there's nothing to do about it.

NetConnection.Call.BadVersion is the code for an error that is generated by NetConnection and sent through its onStatus handler. mx.remoting.Connection extends NetConnection, and every instance of mx.remoting.Service has a connection property... So the first step is to catch the error, for example:


var service = new Service(location, null, serviceName, null, null);
service.connection.onStatus = Delegate.create(this, handleConnectionStatus);
function handleConnectionStatus(info)
{
    if(info.code == 'NetConnection.Call.BadVersion')
    {
        trace('Now attempting to cry');
    }
}
 

The second step is to redirect PHP errors in some place where they can be read. In php.ini there is a setting called log_errors (line 305 here) which you will need to set to On. This will copy the errors into the Apache error log (provided you are using Apache of course).

Third, you need to set up a new service that can read the last few lines of the Apache log and send them back to Flash. Here's what I have:


<?php
define("APACHE_LOG", "C:/Program Files/Apache Group/Apache/logs/error.log");
define("NUM_LINES", 10);
class DebuggingService
{
    function DebuggingService()
    {
        $this->methodTable = array(
            "getLastError" => array(
                "description" => "Gets last XX lines from Apache error log file",
                "access" => "remote"
            )
        );
    }
   
    function getLastError()
    {
        $lines = file(APACHE_LOG);
        $str = "";
        $max = min(NUM_LINES, count($lines));
        for($i = 0; $i < $max; $i++)
        {
            $str .= $lines[count($lines) - 1 - $i];
        }
        return $str;
    }
}
?>
 

You can see where this is going right? Simply call this service and method right after a NetConnection.Call.BadVersion error and you'll receive the last couple of errors in plain text in the NetConnection debugger, for example:


import mx.remoting.*;
import mx.remoting.debug.NetDebug;
import mx.utils.Delegate;

NetDebug.initialize();
var service = new Service(location, null, serviceName, null, null);
service.connection.onStatus = Delegate.create(this, handleConnectionStatus);
function handleConnectionStatus(info)
{
    if(info.code == 'NetConnection.Call.BadVersion')
    {
        NetDebug.trace("NetConnection.Call.BadVersion:: attempting to recover");
        var debugService = new Service(location, null, 'DebuggingService', null, null);
        debugService.getLastError();
    }
}
 

And here are the results:

NetConnection.Call.BadVersion

Here I've also used the Log feature (that second parameter in mx.remoting.Service nobody ever uses) and redirected it to the LuminicBox trace panel. By default the error includes the line number and the called file with a short description. If you want, you can install the Xdebug extension to see the call stack in addition.

I'm currently creating a wrapper around mx.remoting.Service that includes this functionality by default and will eventually post it here. Somehow I'm starting to dislike RelayResponder so I'll be doing something which eliminates its use completely and works with EventDispatcher.


WordPress database error: [Can't open file: 'wp_comments.MYD'. (errno: 145)]
SELECT * FROM wp_comments WHERE comment_post_ID = '114' AND comment_approved = '1' ORDER BY comment_date

No comments have been added to this post yet.

Leave a comment




Your e-mail address is never displayed. If you run into issues with SpamKarma blocking you, email me at $patrick->5etdemi(com)


RSS feed for comments on this post | TrackBack URI