有没有办法在Dart中捕获JavaScript异常?

时间:2014-11-06 10:58:59

标签: dart dart-js-interop

我有一个<core-icon>元素,如

<polymer-element name="app-element">
  <template>
    <core-icon icon="{{icon}}"></core-icon>
  </template>
  <script ...>
</polymer-element>
@CustomTag('app-element')
class AppElement extends PolymerElement {
  AppElement.created() : super.created();

  @observable
  String icon = 'menu';

  clickHandler(e) {
    icon = null;
  }
} 

这会导致此异常

Exception caught during observer callback: TypeError: Cannot read property 'split' of null
    at core-icon.Polymer.updateAlt (http://localhost:63342/core-elements/packages/core_elements/src/core-icon/core-icon.html:188:50)
    at core-icon.Polymer.updateIcon (http://localhost:63342/core-elements/packages/core_elements/src/core-icon/core-icon.html:147:14)
    at core-icon.g.invokeMethod (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:13:25932)
    at core-icon.g.notifyPropertyChanges (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:13:24037)
    at Object.x.report_ (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:12:18274)
    at Object.S.check_ (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:12:22612)
    at c (http://localhost:63342/core-elements/packages/polymer/src/js/polymer/polymer.js:12:12181) polymer.concat.js:4861x.report_ polymer.concat.js:4861S.check_ polymer.concat.js:5264c

在我看来这是一个但是在<core-icon>并且我要创建一个问题,但无论如何我的问题是:

有没有办法在Dart中捕获这样的JavaScript异常?

我当然试图将行icon = null;try/catchwindow.onError.listen((e)...);包裹起来但没有成功。

1 个答案:

答案 0 :(得分:0)

我还没有尝试过,但我想这是解决方案:

来源:https://github.com/dart-lang/core-elements/issues/148#issuecomment-62574668

看起来实际问题是在js端捕获异常,然后只是打印到控制台,这就是为什么我们无法访问它。他们确实将Observer._errorThrownDuringCallback设置为true,因此您可以在测试中添加以下内容并且它将适当地失败:

expect(context['Observer']['_errorThrownDuringCallback'], false,
    reason: 'Setting icon to null shouldn\'t throw an error.');