简体   繁体   English

javascript:如何使用抑制回调中的异常的库?

[英]javascript: how to work with libs that suppress exceptions in callbacks?

lots of javascript libs and frameworks accepts functions that are called after some operation is completed. 许多javascript库和框架都接受一些操作完成后调用的函数。 For example: 例如:

chrome.storage.local.get( 'foo', function() { console.log( "foo" ); } );

But i just realized that some API's, including google local storage api mentioned above catch and suppress all exceptions in functions being called. 但是我才意识到,某些API(包括上述的Google本地存储api)会捕获并抑制调用函数中的所有异常。 For example, if i modify a code above to have an error ( ReferenceError that i want to see in console): 例如,如果我修改上面的代码以产生错误(我想在控制台中看到的ReferenceError ):

chrome.storage.local.get( 'foo', function() { a.b(); } );

No error will be raised since api suppress it by catching and ignoring all errors. 由于api通过捕获并忽略所有错误来抑制它,因此不会引发任何错误。 How to debug my code in such situations? 在这种情况下如何调试我的代码? Is it any way to bring errors back or all this api's are just not intended to be used with complex code and i need to debug via console.log manually to find what have failed? 有什么办法可以将错误带回来,或者所有这些api都不打算与复杂的代码一起使用,而我需要通过console.log手动调试以查找失败的地方?

UPDATE 1 更新1

Simply adding my own try-catch to all callback will add complexity to code that is not really welcome. 只需将我自己的try-catch添加到所有回调中,将增加代码的复杂性,这是不受欢迎的。 Also, simple console.log is worse than exception since exception is catched by dev tools, displayed in red, have stack trace attached etc. Of course all this can be emulated with console.log but this will add more complexity. 同样,简单的console.log比异常更糟糕,因为异常被开发工具捕获,以红色显示,附加了堆栈跟踪等。当然,所有这些都可以通过console.log进行仿真,但这会增加复杂性。

UPDATE 2 更新2

Seems it's a ommon practice for js code to ignore errors, so i was forced to implement ugly solution by adding underscore plugin and using it to envelope each callback: 看来js代码忽略错误是一种普遍的做法,因此我被迫通过添加下划线插件并将其用于封装每个回调来实现丑陋的解决方案:

function _safeblock( block )
{
  console.assert( block );
  return function() {
    try {
      block.apply( this, arguments );
    }
    catch( e ) {
      console.log( e.message, e.stack );
    }
  };
}


function _safecall( block )
{
  console.assert( block );
  _safeblock( block )();
}


_.mixin({
  safeblock: _safeblock,
  safecall: _safecall,
});

You can try catch them yourself. 您可以尝试自己捉住它们。

chrome.storage.local.get( 'foo', function() {
    try{
        a.b();
    }catch(e){
        console.log(e);
        //do whatever
    } 
});

You can automate this, either by overriding storage.local.get or by creating a generic function wrapper. 您可以通过覆盖storage.local.get或创建通用函数包装器来storage.local.get自动化。

You can also defer the function manually with setTimeout which would make it impossible (in the browser) for the library to suppress your errors 您还可以使用setTimeout手动推迟该函数,这将使该库无法(在浏览器中)抑制您的错误

chrome.storage.local.get( 'foo', function() {
     setTimeout(function(){
           a.b();
     });
});

This would not give you very meaningful stack traces though (anything outside the function will not show) which makes it less useful. 但是,这不会给您带来非常有意义的堆栈跟踪(该函数以外的任何内容都不会显示),这使它的用处较小。 It's also likely a performance overhead if you use this in performance sensitive situations (and in browsers setTimeout takes at least a few miliseconds, there is a postMessage hack around it though). 如果您在对性能敏感的情况下使用此功能,也可能会带来性能开销(并且在浏览器中,setTimeout至少需要花费几毫秒的时间,但是围绕着postMessage进行了破解)。

You could simply perform a try-catch and use console.error (works in Firebug and Chrome DevTools, not sure about others): 您可以简单地执行try-catch并使用console.error (在Firebug和Chrome DevTools中有效,不确定其他功能):

chrome.storage.local.get('foo', function () 
{   try { a.b(); }
    catch (e) { console.error(e.name + ": " + e.message); }
});

If you didn't need arguments for your callback, you could create a wrapper function: 如果您的回调不需要参数,则可以创建包装函数:

function wrapTryCatch(func)
{   return function ()
    {   try { func.apply(this, arguments); }
        catch (e)
        {   var err = e.name + ': ' + e.message;
            console.error ? console.error(err)
            : console.log ? console.log(err)
            : alert(err);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM