[PM-7229] Fix circular dependency in ErrorHandler (#8573)

* call injector explicitly in ErrorHandler

* Fallback to `super` on early error

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>

---------

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>,
This commit is contained in:
Will Martin 2024-04-02 10:21:01 -04:00 committed by GitHub
parent 22cca018f8
commit a201e9cff1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 6 deletions

View File

@ -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);
}
}
}

View File

@ -1075,7 +1075,7 @@ const safeProviders: SafeProvider[] = [
safeProvider({
provide: ErrorHandler,
useClass: LoggingErrorHandler,
deps: [LogService],
deps: [],
}),
];