Ionic2中騰訊Bugly異常捕獲以及上報

Ionic2混合開發,入坑系列:Ionic2中騰訊Bugly異常捕獲以及上報html

 

一、Ionic2中處理全局異常,直接繼承IonicErrorHandler便可,代碼以下ios

import { IonicErrorHandler } from 'ionic-angular';
import { Bugly } from './../plugins/TX-bugly.plugin'
import { Config } from './../config/config'
/**
 * Ionic2全局異常捕獲
 */
export class GlobalIonicErrorHandler extends IonicErrorHandler {


    /**
     * 異常處理
     * @param err 異常信息
     */
    handleError(err: any): void {
        let _message = err._nativeError || err.message || err._nativeError.message || "";
        let _stack = err._nativeError || err.stack || err._nativeError.stack;
        let params: any = { message: _message, stack: _stack, platform: Config.APP_PLATFORM };
        console.log("錯誤:" + _message);
        console.log("堆棧:" + _stack);
        Bugly.buglySend(JSON.stringify(params));
    }
}

 

二、在AppModule.ts中將自定義的異常擴類添加到providers中bootstrap

import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import './rxjs-extensions';
import { GlobalIonicErrorHandler } from './global-error-handler-ionic.extend'

@NgModule({ declarations: [ MyApp ], bootstrap: [IonicApp], entryComponents: [ MyApp ], imports: [ IonicModule.forRoot(MyApp, { tabsHideOnSubPages: true, iconMode: 'ios', modalEnter: 'modal-slide-in', modalLeave: 'modal-slide-out', tabsPlacement: 'bottom', pageTransition: 'ios' }), ComponentBusinessModule ], providers: [ { provide: ErrorHandler, useClass: GlobalIonicErrorHandler }] }) export class AppModule { }
相關文章
相關標籤/搜索