【发布时间】:2017-12-17 20:54:36
【问题描述】:
我刚刚将我的 Ionic 2 应用程序迁移到 Ionic Pro 并按照以下链接中的说明设置错误监控:
https://ionicframework.com/docs/pro/monitoring/
但到目前为止我无法弄清楚它是如何工作的,所以这是我的问题:
我在我的应用程序中使用 @angular/http 的 HTTP 模块与我的 API 进行通信,到目前为止我在旧版本中从未需要代理,因为我正在使用 chrome --disable-web-security 功能进行测试并且它曾经可以工作当我部署到旧版 Ionic View 时。但是,在我部署到 Ionic Pro 后,我必须添加代理才能使用 Ionic DevApp,而当我部署到 Ionic View 时,它根本无法使用代理。
所以当我在 Ionic View 中测试时,我希望看到错误和日志,但 Ionic 中的监控页面没有显示任何内容。
这里是app.module.ts中用于错误处理的部分代码:
import { NgModule, Pipe, PipeTransform, ErrorHandler, Injectable, Injector } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { Pro } from '@ionic/pro';
@Injectable()
export class MyErrorHandler implements ErrorHandler {
ionicErrorHandler: IonicErrorHandler;
constructor(injector: Injector) {
try {
this.ionicErrorHandler = injector.get(IonicErrorHandler);
} catch (e) {
// Unable to get the IonicErrorHandler provider, ensure
// IonicErrorHandler has been added to the providers list below
}
}
handleError(err: any): void {
IonicPro.monitoring.handleNewError(err);
// Remove this if you want to disable Ionic's auto exception handling
// in development mode.
this.ionicErrorHandler && this.ionicErrorHandler.handleError(err);
}
}
providers: [
IonicErrorHandler,
[{ provide: ErrorHandler, useClass: MyErrorHandler }]
]
})
我还尝试在我的 login.ts 代码中手动发送错误:
import { Pro } from '@ionic/pro';
err => {
loader.dismissAll();
var errorTitle = this.authService.connectionService.GetNoInternetConnectionErrorTitle();
var errorMessage = this.authService.connectionService.GetNoInternetConnectionErrorMessage();
if (err.status != 0) {
console.log(JSON.parse(err._body));
var errMessageJson = JSON.parse(JSON.parse(err._body));
console.log("Error Code is " + errMessageJson.ErrorCode);
errorTitle = errMessageJson.ErrorTitle;
errorMessage = errMessageJson.ErrorMessage;
}
let alert = this.alertCtrl.create({
title: errorTitle,
subTitle: errorMessage,
buttons: ['OK']
});
// add error monitoring here
Pro.getApp().monitoring.exception(new Error('Could not connect to API. Error Message is ' + errorMessage));
Pro.getApp().monitoring.log('This happens sometimes Could not connect to API. The whole message object is ' + err, { level: 'error' });
alert.present();
},
【问题讨论】:
标签: angular cordova typescript ionic-framework