Olá, estou com este problema onde os erros da minha aplicação não estão sendo capturados pelo meu handleError não sei se esqueci de algo na implementação. Alguém poderia ajudar?
Segue código:
global-error-handler.ts
import { ErrorHandler, Injectable, Injector } from "@angular/core";
import * as StackTrace from 'stacktrace-js';
import { LocationStrategy, PathLocationStrategy } from "@angular/common";
import { Router } from "@angular/router";
import { environment } from "../../../environments/environment"
import { UserService } from "../../core/user/user.service";
import { ServerLogService } from "./server-log.service";
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor(private injector: Injector) {}
handleError(error: any): void {
console.log("Passou pelo handler");
const location = this.injector.get(LocationStrategy);
const userService = this.injector.get(UserService);
const serverLogService = this.injector.get(ServerLogService);
const router = this.injector.get(Router);
const url = location instanceof PathLocationStrategy
? location.path()
: '';
const message = error.message ? error.message : error.toString();
if(environment.production) router.navigate(['/error']);
StackTrace
.fromError(error)
.then(stackFrames => {
const stackAsString = stackFrames
.map(sf => sf.toString())
.join('\n')
console.log(message);
console.log(stackAsString);
console.log("oque será enviado para o servidor:");
serverLogService
.log({
message,
url,
userName: userService.getUserName(),
stack: stackAsString}
).subscribe(
() => console.log("Error logged on server"),
err => {
console.log(err);
console.log("Fail to send error log to server");
}
);
});
}
}
errors.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { NotFoundComponent } from './not-found/not-found.component';
import { GlobalErrorHandler } from './global-error-handler/global-error-handler';
import { GlobalErrorComponent } from './global-error/global-error.component';
@NgModule({
imports: [
CommonModule,
RouterModule
],
declarations: [
NotFoundComponent,
GlobalErrorComponent
],
providers: [
{
provide: ErrorHandler,
useClass: GlobalErrorHandler
}
]
})
export class ErrorsModule { }
app.module.ts
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing.module';
import { HttpClientModule } from '@angular/common/http';
import { ErrorsModule } from './errors/errors.module';
import { CoreModule } from './core/core.module';
import { ShowIfLoggedModule } from './shared/directives/show-if-logged/show-if-logged.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {MAT_MOMENT_DATE_FORMATS, MomentDateAdapter} from '@angular/material-moment-adapter';
import { MAT_DATE_LOCALE, DateAdapter, MAT_DATE_FORMATS } from '@angular/material';
@NgModule({
declarations: [
AppComponent
],
imports: [
ErrorsModule,
CoreModule,
HttpClientModule,
ShowIfLoggedModule,
AppRoutingModule,
BrowserAnimationsModule
],
providers: [
{provide: MAT_DATE_LOCALE, useValue: 'pt-BR'},
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS}
],
bootstrap: [AppComponent]
})
export class AppModule { }