From c2e7491890a6ac0deb360e90e6c075f77fdee659 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 12 Aug 2022 15:58:36 +0200 Subject: [PATCH] backend-app-api: Improve error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patrik Oldsberg Co-authored-by: Fredrik Adelöw Signed-off-by: Johan Haals --- .../src/wiring/BackendInitializer.ts | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/backend-app-api/src/wiring/BackendInitializer.ts b/packages/backend-app-api/src/wiring/BackendInitializer.ts index c1b7089827..afb5250504 100644 --- a/packages/backend-app-api/src/wiring/BackendInitializer.ts +++ b/packages/backend-app-api/src/wiring/BackendInitializer.ts @@ -40,17 +40,35 @@ export class BackendInitializer { deps: { [name: string]: ServiceOrExtensionPoint }, pluginId: string, ) { - return Object.fromEntries( - await Promise.all( - Object.entries(deps).map(async ([name, ref]) => [ - name, - this.#extensionPoints.get(ref as ExtensionPoint) || - (await this.#serviceHolder.get(ref as ServiceRef)!( - pluginId, - )), - ]), - ), - ); + const result = new Map(); + const missingRefs = new Set(); + + for (const [name, ref] of Object.entries(deps)) { + const extensionPoint = this.#extensionPoints.get( + ref as ExtensionPoint, + ); + if (extensionPoint) { + result.set(name, extensionPoint); + } else { + const factory = await this.#serviceHolder.get( + ref as ServiceRef, + ); + if (factory) { + result.set(name, await factory(pluginId)); + } else { + missingRefs.add(ref); + } + } + } + + if (missingRefs.size > 0) { + const missing = Array.from(missingRefs).join(', '); + throw new Error( + `No extension point or service available for the following ref(s): ${missing}`, + ); + } + + return Object.fromEntries(result); } add(feature: BackendFeature, options?: TOptions) { @@ -61,7 +79,6 @@ export class BackendInitializer { } async start(): Promise { - console.log(`Starting backend`); if (this.#started) { throw new Error('Backend has already started'); } @@ -72,7 +89,6 @@ export class BackendInitializer { let registerInit: BackendRegisterInit | undefined = undefined; - console.log('Registering', feature.id); feature.register({ registerExtensionPoint: (extensionPointRef, impl) => { if (registerInit) { @@ -107,8 +123,6 @@ export class BackendInitializer { this.#registerInits.push(registerInit); } - this.validateSetup(); - const orderedRegisterResults = this.#resolveInitOrder(this.#registerInits); for (const registerInit of orderedRegisterResults) { @@ -117,8 +131,6 @@ export class BackendInitializer { } } - private validateSetup() {} - #resolveInitOrder(registerInits: Array) { let registerInitsToOrder = registerInits.slice(); const orderedRegisterInits = new Array(); @@ -142,7 +154,6 @@ export class BackendInitializer { } if (unInitializedDependents.length === 0) { - console.log(`DEBUG: pushed ${registerInit.id} to results`); orderedRegisterInits.push(registerInit); toRemove.add(registerInit); }