Angular 我必须在Ionic 4中集成自定义错误处理
我必须创建一个服务来记录应用程序中发生的任何运行时错误 我已经创建了一个名为“ErrorHandlingService”的服务,下面给出了代码Angular 我必须在Ionic 4中集成自定义错误处理,angular,cordova,ionic-framework,ionic4,Angular,Cordova,Ionic Framework,Ionic4,我必须创建一个服务来记录应用程序中发生的任何运行时错误 我已经创建了一个名为“ErrorHandlingService”的服务,下面给出了代码 import { ErrorHandler } from '@angular/core'; export class ErrorhandlingService implements ErrorHandler { handleError(error: any): void { console.error('Some erro
import { ErrorHandler } from '@angular/core';
export class ErrorhandlingService implements ErrorHandler {
handleError(error: any): void {
console.error('Some error occured: ', error);
}
}
在app.module.ts中,我编写了以下代码
@NgModule({
declarations: [AppComponent],
entryComponents: [],
providers: [StatusBar, SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
{ provide: ErrorHandler, useClass: ErrorhandlingService },
bootstrap: [AppComponent],
})
但是仍然显示默认错误您是如何测试显示的默认错误的 我用你写的代码试过了,效果很好。 尝试使用
抛出新错误(“错误字符串”)
这是stackblitz->
注意:您不需要注释中建议的
@Injectable()
,因为您是在useClass
中提供类的。您是如何测试显示的默认错误的
我用你写的代码试过了,效果很好。
尝试使用
抛出新错误(“错误字符串”)
这是stackblitz->
注意:您不需要一条注释中建议的
@Injectable()
,因为您是在useClass
中提供类的,我已通过错误处理代码更新如下:
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorHandler, Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ErrorhandlingService implements ErrorHandler {
handleError(error: any): void {
const Dt = new Date().toISOString();
if (error instanceof HttpErrorResponse) {
console.log(Dt, '\r\nHTTP error: ', error.message, '\r\nStatus code:', (<HttpErrorResponse>error).status);
}
else if (error instanceof TypeError) {
console.log(Dt, '\r\nType Error: ', error.message);
}
else if (error instanceof Error) {
console.log(Dt, '\r\nGeneral Error: ', error.message);
}
else {
console.log(Dt, '\r\nAnonymous Error: ', error);
}
}
}
从'@angular/common/http'导入{HttpErrorResponse};
从'@angular/core'导入{ErrorHandler,Injectable};
@注射的({
providedIn:'根'
})
导出类ErrorhandlingService实现ErrorHandler{
handleError(错误:任意):无效{
const Dt=新日期().toISOString();
if(HttpErrorResponse的错误实例){
console.log(Dt,'\r\nHTTP error:',error.message,'\r\n状态代码:',(error.status);
}
else if(错误实例of TypeError){
console.log(Dt,“\r\n键入错误:”,Error.message);
}
else if(错误实例of error){
console.log(Dt,'\r\n一般错误:',错误消息);
}
否则{
console.log(Dt,'\r\n同名错误:',错误);
}
}
}
我已通过错误处理代码更新如下:
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorHandler, Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ErrorhandlingService implements ErrorHandler {
handleError(error: any): void {
const Dt = new Date().toISOString();
if (error instanceof HttpErrorResponse) {
console.log(Dt, '\r\nHTTP error: ', error.message, '\r\nStatus code:', (<HttpErrorResponse>error).status);
}
else if (error instanceof TypeError) {
console.log(Dt, '\r\nType Error: ', error.message);
}
else if (error instanceof Error) {
console.log(Dt, '\r\nGeneral Error: ', error.message);
}
else {
console.log(Dt, '\r\nAnonymous Error: ', error);
}
}
}
从'@angular/common/http'导入{HttpErrorResponse};
从'@angular/core'导入{ErrorHandler,Injectable};
@注射的({
providedIn:'根'
})
导出类ErrorhandlingService实现ErrorHandler{
handleError(错误:任意):无效{
const Dt=新日期().toISOString();
if(HttpErrorResponse的错误实例){
console.log(Dt,'\r\nHTTP error:',error.message,'\r\n状态代码:',(error.status);
}
else if(错误实例of TypeError){
console.log(Dt,“\r\n键入错误:”,Error.message);
}
else if(错误实例of error){
console.log(Dt,'\r\n一般错误:',错误消息);
}
否则{
console.log(Dt,'\r\n同名错误:',错误);
}
}
}
您似乎没有将其设置为服务@Injectable()
我已获得解决方案。我刚刚更新了我的代码@Injectable({providedIn:'root'})导出类ErrorhandlingService实现了ErrorHandler{您似乎错过了将其设置为服务@Injectable()
我用解决方案得到的。我刚刚更新了我的代码@Injectable({providedIn:'root'})导出类ErrorhandlingService实现ErrorHandler{