Skip to content

fix: remove default fallback when target function does not exist #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function getUserFunction(
return registeredFunction;
}

let userFunction = functionTarget
const userFunction = functionTarget
.split('.')
.reduce((code, functionTargetPart) => {
if (typeof code === 'undefined') {
Expand All @@ -131,18 +131,12 @@ export async function getUserFunction(
}
}, functionModule);

// TODO: do we want 'function' fallback?
if (typeof userFunction === 'undefined') {
// eslint-disable-next-line no-prototype-builtins
if (functionModule.hasOwnProperty('function')) {
userFunction = functionModule['function'];
} else {
console.error(
`Function '${functionTarget}' is not defined in the provided ` +
'module.\nDid you specify the correct target function to execute?'
);
return null;
}
console.error(
`Function '${functionTarget}' is not defined in the provided ` +
'module.\nDid you specify the correct target function to execute?'
);
return null;
}

if (typeof userFunction !== 'function') {
Expand Down
14 changes: 14 additions & 0 deletions test/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ describe('loading function', () => {
}
}

it('fails to load a function that does not exist', async () => {
FunctionRegistry.http('function', () => {
return 'PASS';
});

const loadedFunction = await loader.getUserFunction(
process.cwd() + '/test/data/with_main',
'functionDoesNotExist',
'http'
);

assert.strictEqual(loadedFunction, null);
});

it('loads a declaratively registered function', async () => {
FunctionRegistry.http('registeredFunction', () => {
return 'PASS';
Expand Down