我有一个Ember.js 3.8应用程序,在其中我希望应用程序级错误处理。我读过似乎是文件的有关部分的内容,但它不像我预期的那样有效。
我有一个组件来测试这一点:
exception-creator.hbs
<div class="panel panel-info">
<div class="panel-heading">Development Only - Exception Creator</div>
<div class="panel-body">
<p>Press the button below to create an exception</p>
<button {{action 'throw' }}>Throw an Exception</button>
</div>
</div>
{{yield}}exception-creator.js
import Component from '@ember/component';
export default Component.extend({
actions: {
throw(){
alert("About to throw an exception");
throw new Error('Whoops!');
}
}
});并且(根据我对doco的阅读),我创建了一个路由application-error
application-error.hbs
<h1>Error Handler</h1>
<p>This is the error handler</p>
{{outlet}}application-error.js
import Route from '@ember/routing/route';
export default Route.extend({
});当按下按钮时,我希望(在警报之后)被重新路由到application-error模板.但我不是。所发生的事情是调试控制台显示“未察觉的错误:喔!”。
谁能告诉我哪里出了问题吗?
编辑
在@stevenelberger给出了正确的路径之后,我现在有了一个工作版本,在这里我将把它放在这里,以便其他人将来可以从中受益(这不是说它有很大的好处,但它可能对某人有帮助)。
我创建了一个新的路由exception-testing-route
exception-testing-route.js
import Route from '@ember/routing/route';
export default Route.extend({
model() {
throw new Error('Whoops - something bad happened at exception-testing-route');
},
actions: {
error(error, transition) {
console.log("The action 'error' is firing in the route exception-testing-route.");
console.log("About to return true to bubble the error.");
return true;
}
},
});exception-testing-route.hbs
{{outlet}}让error操作返回true将导致出现冒泡错误,在本例中为application-error路由,如下所示
application-error.js
import Route from '@ember/routing/route';
export default Route.extend({
setupController(controller, error) {
console.log("application-error setupController firing");
console.log(error.message);
this._super(...arguments);
}
});application-error.hbs
<h1>Error Handler</h1>
<p>This is the error handler</p>
{{outlet}}调用异常测试路由将导致以下处理顺序。

发布于 2019-10-11 00:39:22
我相信你可能错过了文档的这一部分:
与加载子状态一样,对抛出的错误或拒绝的承诺从.路由的模型钩子(或beforeModel或afterModel) Ember将查找错误模板或路由.
增加了需要澄清的重点。您的代码当前正在从一个操作中抛出一个错误,但只有当从路由的model、beforeModel或afterModel挂钩抛出/返回错误或拒绝的承诺时,Ember才会路由到错误状态(而且可能也来自转换,尽管文档在这方面有点模糊,我现在没有时间测试)。
因此,要进入应用程序错误路由,需要在某些路由的model、beforeModel或afterModel钩子中抛出错误或返回拒绝的承诺。
https://stackoverflow.com/questions/58332719
复制相似问题