Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
CustomErrroHandler no maneja la excepción personalizada lanzada en Angular

Ejecuto la aplicación Angular 12 y quiero lograr un comportamiento para crear un controlador de errores personalizado para manejar errores globalmente.

Cada vez que recibo una notificación de error del backend, la suscribo en ToolService usando this.notificationService.registerToServerCalls(......) . Desde aquí, lanzo la CustomException que debería ir a CustomErrorHandler para manejar el error. Pero, una vez que se lanza la CustomException, el código no va al CustomErrorHandler

herramienta.servicio.ts

 @Injectable() export class ToolService { constructor( private notificationService: NotificationService, @Inject(ErrorHandler) private errorHandler: CustomErrorHandler) { this.errorHandler.onUnhandledException$.subscribe({ next: (error: ErrorDetail[]) => { this.dialogService.open(error); } }); } this.notificationRService.registerToServerCalls<ErrorDetail[]>('Error', (errorDetail: ErrorDetail[]) => { this.onError.next(errorDetail); throw new CustomException(errorDetail); });

notificación.servicio.ts

 public registerToServerCalls<T>(clientMethod: string, callback: (data: T) => void): void { this.hubConnection.on(clientMethod, callback); }

custom-exception.model.ts

 export class CustomException extends Error { error: ErrorDetail[]; constructor(error?: ErrorDetail[]) { super(); this.error = error; } }

controlador de errores personalizado.ts

 @Injectable() export class CustomErrorHandler implements ErrorHandler { private onUnhandledException = new Subject(); onUnhandledException$ = this.onUnhandledException.asObservable(); isErrorHandled = false; /** * Override the base class error handler to handle the error. */ handleError(errorObj) { if (this.isErrorHandled) { return; } errorObj = errorObj.rejection ? errorObj.rejection : errorObj; // .rejection is required in case of promise rejections. let errorDetails: ErrorDetail[]; errorDetails = errorObj.error; this.onUnhandledException.next(errorDetails); this.isErrorHandled = true; } }
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Hay una explicación muy simple para esto, aunque no estoy seguro de por qué sucede. Tuve un poco el mismo problema recientemente

El problema es que Angular usa una instancia diferente de CustomErrorHandler que la que inyecta en su servicio.

Esto podría resolverse usando lo siguiente en su controlador de errores y servicios, pero dudo mucho que eso funcione.

 @Injectable({ providedIn: 'root' })

En mi caso utilicé un segundo servicio. Algo como esto:

 export class ErrorMessageService { private onUnhandledException = new Subject(); onUnhandledException$ = this.onUnhandledException.asObservable(); setMessage(message: string){ onUnhandledException.next(message); } } export class CustomErrorHandler implements ErrorHandler { constructor(private errorMessageService: ErrorMessageService) handleError(errorObj) { this.errorMessageService.setMessage(...) ... ... } } export class ToolService { constructor( private notificationService: NotificationService, private errorMessageService: ErrorMessageService) { this.errorMessageService.onUnhandledException$.subscribe({ next: (error: ErrorDetail[]) => { this.dialogService.open(error); } }); } }

Alternativamente, puede fusionar ErrorMessageService y Toolservice e inyectar toolservice en su controlador de errores.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda