如何在Worker API中检测无效URL的错误 [英] How to detect errors of invalid URL in Worker API

查看:60
本文介绍了如何在Worker API中检测无效URL的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行以下命令(至少在Chromium上运行):

If I run the following (at least on Chromium):

var w = null;
try {
    w = new Worker( 'NONEXISTENT_URL' );
}
catch ( e ) {
    console.log( 'CAUGHT: ' + e );
}
console.log( w );

...即使 Worker 构造函数未指向到有效的文件名。此外,控制台显示 Worker {} ,指示 w 现在包含<$的(非功能性)实例。 c $ c> Worker 。

...no exception is caught, even though the argument to the Worker constructor does not point to a valid filename. Moreover, the console shows Worker {}, indicating that w now holds a (non-functional) instance of Worker.

其他错误情况也是可能的。例如,构造函数的参数可能可用并且可读,但其中不包含有效的JavaScript。

Other error conditions are also possible. For example, argument to the constructor may be available and readable, but not contain valid JavaScript.

我还能如何检测到此类错误情况?

How else can I detect such error conditions?

(顺便说一句,请假定浏览器支持 Worker API。)

(BTW: Please assume that the browser supports the Worker API.)

推荐答案

使用 onerror 事件处理程序。

You can get error using onerror event handler.

var worker = new Worker('aURL');

worker.onerror = function () {
    console.log( 'error' );
};




如果URL的语法无效或同源策略违反了抛出SECURITY_ERR类型的 DOMException

这篇关于如何在Worker API中检测无效URL的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆