6

Example code:

var connection = null;

function onConnect(status) {
    im_a_big_error.log('wtf');
    // Why it doesn't throw me an error here ??                                                                                                                                                                                                
}

$().ready(function() {
    connection = new Strophe.Connection('http://localhost:8080/http-bind');
    connection.connect('admin@localhost', 'admin', onConnect);
});

It doesn't throw me an error in my Chrome console.

Do you have an idea to resolve this issue?

Mat
  • 202,337
  • 40
  • 393
  • 406
Unitech
  • 5,781
  • 5
  • 40
  • 47
  • Is `onConnect` called? It should throw `undefined is not a function` or similar. – alex Nov 27 '11 at 10:57
  • `connection.connect('admin@localhost', 'admin', onConnect);` is called here – Unitech Nov 27 '11 at 11:00
  • 1
    Fyi, `$().ready(..)` registers the `ready` event on an empty jQuery set. you should either use `$(document).ready(..)` or its shorter equivalent `$(..);` - `..` is your callback function. – ThiefMaster Nov 27 '11 at 11:13
  • Thanks for your answer. Using `$(document).ready(..)` doesn't throw an error anyway :( – Unitech Nov 27 '11 at 11:41

4 Answers4

8

Yes, Strophe often catch errors by itself and currently doesn't provide any ability to get connection error information. While error catching is ok, the impossibility of catching errors by yourself is not very good. But you can fix it with the following code:

$().ready(function() {
    connection = new Strophe.Connection('http://localhost:8080/http-bind');
    connection._hitError = function (reqStatus) {
        this.errors++;
        Strophe.warn("request errored, status: " + reqStatus + ", 
                number of errors: " + this.errors);
        if (this.errors > 4) this._onDisconnectTimeout();
        myErrorHandler(reqStatus, this.errors);
    };
    connection.connect('admin@localhost', 'admin', onConnect);
});

where myErrorHandler is your custom connection error handler.

tsds
  • 8,700
  • 12
  • 62
  • 83
  • 2
    I think it should be connection._proto._hitError, since _hitError is a method of Bosh. Maybe that changed since your answer, 2.5 years ago. – Gilad Beeri Jul 31 '14 at 16:14
4

Yes, strophe swallows errors. Worse; After an error is thrown, the callback won't return true as it should, and strophe will remove the handler. As soon as an error occurs, the callback will never be called again.

I found the code from the current answer a bit hard to use. Internally, we use the following wrapper for every callback;

function callback(cb) {
// Callback wrapper with
// (1) proper error reporting (Strophe swallows errors)
// (2) always returns true to keep the handler installed
return function() {
    try {
        cb.apply(this, arguments);
    } catch (e){
        console.log('ERROR: ' + (e.stack ? e.stack : e));
    }

    // Return true to keep calling the callback.
    return true;
};
}

This wrapper would be used as following in the code of the question;

connection.connect('admin@localhost', 'admin', callback(onConnect));
TinkerTank
  • 5,685
  • 2
  • 32
  • 41
  • My apologies; The previous version of this code had a bug that would corrupt the arguments if the callback was called with more than one argument. Please revise if you've used the old code. – TinkerTank May 05 '13 at 23:15
0

I've been playing with Strophe for a while now and I had to modify its default error handling routine to fit our needs

  • Strophe.js - log function - by default contains nothing - I added calls to my server side logging service for level === ERROR and level === FATAL
  • Strophe.js - run function - the default behavior for error is to remove the handler and to rethrow the error - since I already log the error server side I don't rethrow the error and decided to keep the handler (even if it failed). This behavior could make sense (or not) depending on your own implementation - since I use custom messages and have a rather complicated message processing routine I don't want the client to stop just because a message was not properly formatted when sent so I want to keep the handler, error or not. I replace the throw e line inside the run function with result = true;
  • Strope.js _hitError - as I mentioned, I don't want the client to ever disconnect so I rewrote the default behavior to never disconnect (no matter how high the error counter)

Hope these thoughts are of help to others - leave a comment if you have questions/want details.

Ando
  • 11,199
  • 2
  • 30
  • 46
0

I had a similar problem which I fixed using the approach given by tsds above. However with minimal modification. I created two connect methods one as connect and the other as connect_bak I placed the script

this.connection._hitError=function (reqStatus) {
client.connect_bak();
};

in my connectHandler function as well as the connect function. Such that the function is always binded on connect.