From ac0e281977dcc0abec9ef9f3e162aa4c5cd08f48 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 10 Oct 2023 13:01:43 +0200 Subject: [PATCH] frontend-app-api: tweak error messaging to include more information about where undeclared inputs are from Signed-off-by: Patrik Oldsberg --- .../wiring/createExtensionInstance.test.ts | 45 ++++++++++++++++++- .../src/wiring/createExtensionInstance.ts | 15 +++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/packages/frontend-app-api/src/wiring/createExtensionInstance.test.ts b/packages/frontend-app-api/src/wiring/createExtensionInstance.test.ts index 70f70368d3..6e69e99cb7 100644 --- a/packages/frontend-app-api/src/wiring/createExtensionInstance.test.ts +++ b/packages/frontend-app-api/src/wiring/createExtensionInstance.test.ts @@ -289,7 +289,50 @@ describe('createExtensionInstance', () => { }), }), ).toThrow( - "Failed to instantiate extension 'core.test', received undeclared input(s) 'undeclared'", + "Failed to instantiate extension 'core.test', received undeclared input 'undeclared' from extension 'core.test'", + ); + }); + + it('should refuse to create an instance with multiple undeclared inputs', () => { + expect(() => + createExtensionInstance({ + attachments: new Map([ + [ + 'undeclared1', + [ + createExtensionInstance({ + attachments: new Map(), + config: { output: 'many1' }, + extension: simpleExtension, + }), + ], + ], + [ + 'undeclared2', + [ + createExtensionInstance({ + attachments: new Map(), + config: { output: 'many1' }, + extension: simpleExtension, + }), + createExtensionInstance({ + attachments: new Map(), + config: { output: 'many1' }, + extension: simpleExtension, + }), + ], + ], + ]), + config: undefined, + extension: createExtension({ + id: 'core.test', + attachTo: { id: 'ignored', input: 'ignored' }, + output: {}, + factory() {}, + }), + }), + ).toThrow( + "Failed to instantiate extension 'core.test', received undeclared inputs 'undeclared1' from extension 'core.test' and 'undeclared2' from extensions 'core.test', 'core.test'", ); }); diff --git a/packages/frontend-app-api/src/wiring/createExtensionInstance.ts b/packages/frontend-app-api/src/wiring/createExtensionInstance.ts index ad3b858a60..5a325dc0a0 100644 --- a/packages/frontend-app-api/src/wiring/createExtensionInstance.ts +++ b/packages/frontend-app-api/src/wiring/createExtensionInstance.ts @@ -60,12 +60,21 @@ function resolveInputs( inputMap: AnyExtensionInputMap, attachments: Map, ) { - const undeclaredAttachments = Array.from(attachments.keys()).filter( - name => inputMap[name] === undefined, + const undeclaredAttachments = Array.from(attachments.entries()).filter( + ([inputName]) => inputMap[inputName] === undefined, ); if (undeclaredAttachments.length > 0) { throw new Error( - `received undeclared input(s) '${undeclaredAttachments.join("', '")}'`, + `received undeclared input${ + undeclaredAttachments.length > 1 ? 's' : '' + } ${undeclaredAttachments + .map( + ([k, exts]) => + `'${k}' from extension${exts.length > 1 ? 's' : ''} '${exts + .map(e => e.id) + .join("', '")}'`, + ) + .join(' and ')}`, ); }