如何在Angular应用程序中获取所有控制台错误消息 [英] How to get all console error messages in Angular application

查看:30
本文介绍了如何在Angular应用程序中获取所有控制台错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个有角度的应用程序,问题是当我想在某些智能电视上调试它时,没有调试模式,并且在后台我有一些错误导致应用程序崩溃,所以,有什么办法吗获取所有错误消息并将其打印在我的应用程序页面的一部分中?

I'm working on a angular application, the problem is when I want to debug it on some smart TVs there is no debug mode and in the background I have some errors that cause application to crash, so, is there any way to get all error messages and print it in a part of the page in my application?

实际上,我想在自己的应用程序中使用某种虚拟控制台.

Actually I want to have some kind of virtual console in my own application.

非常感谢您

推荐答案

有几种调试方法:

  • Console.log()-这会将您的消息输出到控制台.不确定是否可以在电视上看到它.
  • 按照 Angular文档
  • 中的说明,创建全局错误处理程序
  • Console.log() - this outputs your message to console. Not sure if you can see it on tv though.
  • Create a global error handler, as provided in Angular documentation

如果上述方法不起作用,您可以尝试使用此解决方案,我不久前还在Internet上的某个地方通过使用

If above does not work, you can try to use this solution, which I also found out somewhere on the internet a while ago by using a snackbar component, which displays errors as a popup:

error.service.ts:

error.service.ts:

import { Injectable } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';

@Injectable({
    providedIn: 'root'
})
export class ErrorService {

    checkOnline()
    {
        if (!navigator.onLine) {
            return 'No Internet Connection';
        }
    }

    getClientMessage(error: Error): string {
        return error.message ? error.message : error.toString();
    }

    getClientStack(error: Error): string {
        return error.stack;
    }

    getServerMessage(error: HttpErrorResponse): string {
        var msg = error.error.Message;
        if (!!msg)
            return msg + " : " + error.error.ExceptionMessage;
        return "Application can not execute because API hasn\'t been started";
    }

    getServerStack(error: HttpErrorResponse): string {
        return error.error.StackTrace;
    }
}

global-error-handler-component.ts:

global-error-handler-component.ts:

import { ErrorHandler, Injectable, Injector } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorService } from './services/error.service';
import { MatSnackBar } from '@angular/material';

@Injectable()
export class GlobalErrorHandler implements ErrorHandler
{
    constructor(private injector: Injector, public snackBar: MatSnackBar) { }

    handleError(error: Error | HttpErrorResponse)
    {
        const errorService = this.injector.get(ErrorService);
        let message;
        let stackTrace = errorService.getClientStack(error);

        if (error instanceof HttpErrorResponse) // Server Error
            message = errorService.getServerMessage(error);
        else // Client Error
            message = errorService.getClientMessage(error);

        this.snackBar.open(message, 'X', { panelClass: ['error'] });
    }
}

app.module.ts:

app.module.ts:

import { GlobalErrorHandler } from './global-error-handler.component';
import { MatSnackBarModule } from '@angular/material/snack-bar';
...
  imports: [
...
    MatSnackBarModule,
...
  ],
  providers: [
...
    { provide: ErrorHandler, useClass: GlobalErrorHandler },
...

这篇关于如何在Angular应用程序中获取所有控制台错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆