From 35ac3f955a00f6ef2250a35daeb589fa9d10c964 Mon Sep 17 00:00:00 2001 From: Gareth George Date: Mon, 22 May 2023 21:01:41 +0000 Subject: [PATCH 1/2] fix: module resolution returns early and accurate error messages on failure --- src/loader.ts | 19 ++++++++++--------- test/loader.ts | 14 ++++++++++---- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/loader.ts b/src/loader.ts index 33a9525a..9abc41dd 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -174,16 +174,17 @@ export async function getUserFunction( * @return Resolved path or null. */ function getFunctionModulePath(codeLocation: string): string | null { - let path: string | null = null; try { - path = require.resolve(codeLocation); + return require.resolve(codeLocation); } catch (ex) { - try { - // TODO: Decide if we want to keep this fallback. - path = require.resolve(codeLocation + '/function.js'); - } catch (ex) { - return path; - } + // Ignore exception, this means the function was not found here. + } + + try { + return require.resolve(codeLocation + '/function.js'); + } catch (ex) { + // Ignore exception, this means the function was not found here. } - return path; + + return null; } diff --git a/test/loader.ts b/test/loader.ts index 1a720f6e..1fd2968b 100644 --- a/test/loader.ts +++ b/test/loader.ts @@ -93,11 +93,17 @@ describe('loading function', () => { } } - it('fails to load a function that does not exist', async () => { - FunctionRegistry.http('function', () => { - return 'PASS'; - }); + it('fails to load a module that does not exist', async () => { + const loadedFunction = await loader.getUserFunction( + process.cwd() + '/test/data/does_not_exist', + 'functionDoesNotExist', + 'http' + ); + assert.strictEqual(loadedFunction, null); + }); + + it('fails to load a function that does not exist', async () => { const loadedFunction = await loader.getUserFunction( process.cwd() + '/test/data/with_main', 'functionDoesNotExist', From b0fe6a48a8f651659b4120b1ffa70cf605587ecc Mon Sep 17 00:00:00 2001 From: Gareth George Date: Tue, 23 May 2023 00:13:39 +0000 Subject: [PATCH 2/2] Improve error message --- src/loader.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/loader.ts b/src/loader.ts index 9abc41dd..b1a2d6cc 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -93,7 +93,11 @@ export async function getUserFunction( try { const functionModulePath = getFunctionModulePath(codeLocation); if (functionModulePath === null) { - console.error('Provided code is not a loadable module.'); + console.error( + `Provided code location '${codeLocation}' is not a loadable module.` + + '\nDid you specify the correct location for the module defining ' + + 'your function?' + ); return null; }