【发布时间】:2014-11-06 10:58:59
【问题描述】:
我有一个类似<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 异常?
我当然尝试用try/catch 或window.onError.listen((e)...); 换行icon = null;,但没有成功。
【问题讨论】:
-
你必须从通话开始就尝试/捕捉。这就是为什么 try/catch 围绕
icon = null没有帮助的原因。你为什么不使用:<template if="{{icon!=null}}"><core-icon icon="{{icon}}"></core-icon></template>。我不认为这是一个错误,为什么您希望图标为空? -
不清楚您所说的
the beginning of the call是什么意思。我可以在绑定或我的代码中检查null,但我仍然认为core-icon不应该在icon变为null时抛出。我故意/没有将其设置为null,但我偶然发现了这一点,因为在某些情况下,模型或模型icon的字段必然会变为null。上面的代码被简化为一个允许重现问题的小例子。 -
也许有办法注册一个全局错误处理程序来允许处理此类异常?
-
即使区域也不会捕获错误。我试过
runZoned(() { icon = null; }, onError: (e) { print('onError'); });没有成功。 -
感谢您的调查!
标签: dart dart-js-interop