简体   繁体   English

如何处理 ExtJs 上的错误

[英]How to Handle errors on ExtJs

I want to be able to catch any error thrown on my screen.我希望能够捕捉到我的屏幕上抛出的任何错误。 So far my code is this:到目前为止,我的代码是这样的:

Ext.application({
    errorHandler:function(err){
        alert(err.msg);
        return true;
    },
    requires: [
        'Ext.ux.ajax.JsonSimlet',
        'Ext.ux.ajax.SimManager'
    ],

    views: [
        'Admin.view.something', 
        'Admin.view.somethingController'
    ],

    name: 'Admin',

    launch : function() {Ext.create('Admin.view.something');
    Ext.Error.notify = false;
    Ext.Error.handle = this.errorHandler;
    }
});

The problem is, I have some objects that may be undefined.问题是,我有一些可能未定义的对象。 If I do something like undefined.properties, the error handling doesn't catch it.如果我执行类似 undefined.properties 的操作,错误处理就不会捕获它。

I know about functions like "Ext.isDefined()", but for this case I really need to handle all generic errors in the same way.我知道像“Ext.isDefined()”这样的函数,但对于这种情况,我真的需要以相同的方式处理所有通用错误。

Ext.Error.handle only handles errors raised by Ext.raise according to the documentation . Ext.Error.handle仅根据文档处理由Ext.raise引发的错误。

A solution to handle plain javascript errors is the error event on the browsers window.处理普通 javascript 错误的解决方案是浏览器窗口上的error事件。
You can attach an handler with window.addEventListener('error', function(event) { ... }) to it.您可以使用window.addEventListener('error', function(event) { ... })附加一个处理程序。

The following code should catch all errors in your project.以下代码应捕获项目中的所有错误。

launch : function() {
    Ext.Error.notify = false;
    Ext.Error.handle = this.errorHandler;
    window.addEventListener('error', this.errorJsHandler);
    Ext.create('Admin.view.something');
},
errorJsHandler: function(err) { // err = ErrorEvent Object https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
        alert(err.message);
        return true;  // prevent the firing of the default event handler
},

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

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