From a201e9cff13b58210bff50d3353462ff040fee41 Mon Sep 17 00:00:00 2001 From: Will Martin Date: Tue, 2 Apr 2024 10:21:01 -0400 Subject: [PATCH] [PM-7229] Fix circular dependency in ErrorHandler (#8573) * call injector explicitly in ErrorHandler * Fallback to `super` on early error Co-authored-by: Matt Gibson --------- Co-authored-by: Matt Gibson , --- .../platform/services/logging-error-handler.ts | 18 +++++++++++++----- .../src/services/jslib-services.module.ts | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libs/angular/src/platform/services/logging-error-handler.ts b/libs/angular/src/platform/services/logging-error-handler.ts index 81cd537e7f..522412dd28 100644 --- a/libs/angular/src/platform/services/logging-error-handler.ts +++ b/libs/angular/src/platform/services/logging-error-handler.ts @@ -1,14 +1,22 @@ -import { ErrorHandler, Injectable } from "@angular/core"; +import { ErrorHandler, Injectable, Injector, inject } from "@angular/core"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @Injectable() export class LoggingErrorHandler extends ErrorHandler { - constructor(private readonly logService: LogService) { - super(); - } + /** + * When injecting services into an `ErrorHandler`, we must use the `Injector` manually to avoid circular dependency errors. + * + * https://stackoverflow.com/a/57115053 + */ + private injector = inject(Injector); override handleError(error: any): void { - this.logService.error(error); + try { + const logService = this.injector.get(LogService, null); + logService.error(error); + } catch { + super.handleError(error); + } } } diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index 114c60c193..6378eed755 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -1075,7 +1075,7 @@ const safeProviders: SafeProvider[] = [ safeProvider({ provide: ErrorHandler, useClass: LoggingErrorHandler, - deps: [LogService], + deps: [], }), ];