为什么 Strope.js 没有抛出错误?

发布于 2024-12-18 00:39:33 字数 583 浏览 5 评论 0原文

示例代码:

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);
});

它不会在 Chrome 控制台中抛出错误。

您有解决这个问题的想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白况 2024-12-25 00:39:33

是的,Strope 经常自行捕获错误,并且目前不提供任何获取连接错误信息的功能。虽然错误捕获是可以的,但是自己无法捕获错误并不是很好。但您可以使用以下代码修复它:

$().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);
});

其中 myErrorHandler 是您的自定义连接错误处理程序。

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.

抹茶夏天i‖ 2024-12-25 00:39:33

是的,营养不良会吞噬错误。更糟糕的是;抛出错误后,回调不会按应有的方式返回 true,并且 strope 将删除处理程序。一旦发生错误,回调将不会再次被调用。

我发现当前答案中的代码有点难以使用。在内部,我们对每个回调使用以下包装器;

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;
};
}

该包装器将在问题的代码中按以下方式使用;

connection.connect('admin@localhost', 'admin', callback(onConnect));

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));
花开半夏魅人心 2024-12-25 00:39:33

我已经使用 Strope 一段时间了,我必须修改它的默认错误处理例程以满足我们的需求

  • Strope.js - log 函数 - 默认情况下不包含任何内容 - 我添加了对我的服务器的调用level === ERROR 和 level === FATAL
  • Strope.js 的侧面日志记录服务 - run 函数 - 错误的默认行为是删除处理程序并重新抛出错误 - 因为我已经记录了错误服务器端我不会重新抛出错误并决定保留处理程序(即使失败)。这种行为可能有意义(或没有),具体取决于您自己的实现 - 因为我使用自定义消息并且有一个相当复杂的消息处理例程,所以我不希望客户端仅仅因为发送时消息格式不正确而停止,所以我想要保留处理程序,无论是否有错误。我将 run 函数中的 throw e 行替换为 result = true;
  • Strope.js _hitError - 正如我所提到的,我不希望客户端断开连接,所以我重写默认行为以永远不会断开连接(无论错误计数器有多高)

希望这些想法对其他人有帮助 - 如果您有问题/想要详细信息,请发表评论。

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.

年少掌心 2024-12-25 00:39:33

我遇到了类似的问题,我使用上面 tsds 给出的方法修复了该问题。然而,只需进行最小程度的修改。我创建了两种连接方法,一种为 connect,另一种为 connect_bak 我将脚本放置

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

在 connectHandler 函数以及 connect 函数中。这样该函数始终绑定在连接上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文