Skip to content

fix: log actual errors instead of just strings with console.error so … #563

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 12 additions & 10 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import {HandlerFunction} from './functions';
import {SignatureType} from './types';
import {getRegisteredFunction} from './function_registry';

export class LoaderError extends Error {
constructor(message) {
super('Could not load the function: ' + message)
this.name = 'LoaderError'
}
}

// Dynamic import function required to load user code packaged as an
// ES module is only available on Node.js v13.2.0 and up.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility
Expand Down Expand Up @@ -93,23 +100,21 @@ export async function getUserFunction(
try {
const functionModulePath = getFunctionModulePath(codeLocation);
if (functionModulePath === null) {
console.error(
throw new LoaderError(
`Provided code location '${codeLocation}' is not a loadable module.` +
'\nDid you specify the correct location for the module defining ' +
'your function?'
);
return null;
}

let functionModule;
const esModule = await isEsModule(functionModulePath);
if (esModule) {
if (semver.lt(process.version, MIN_NODE_VERSION_ESMODULES)) {
console.error(
throw new LoaderError(
`Cannot load ES Module on Node.js ${process.version}. ` +
`Please upgrade to Node.js v${MIN_NODE_VERSION_ESMODULES} and up.`
);
return null;
}
// Resolve module path to file:// URL. Required for windows support.
const fpath = pathToFileURL(functionModulePath);
Expand All @@ -136,19 +141,17 @@ export async function getUserFunction(
}, functionModule);

if (typeof userFunction === 'undefined') {
console.error(
throw new LoaderError(
`Function '${functionTarget}' is not defined in the provided ` +
'module.\nDid you specify the correct target function to execute?'
);
return null;
}

if (typeof userFunction !== 'function') {
console.error(
throw new LoaderError(
`'${functionTarget}' needs to be of type function. Got: ` +
`${typeof userFunction}`
);
return null;
}

return {userFunction: userFunction as HandlerFunction, signatureType};
Expand All @@ -163,11 +166,10 @@ export async function getUserFunction(
} else {
additionalHint = 'Is there a syntax error in your code?\n';
}
console.error(
throw new LoaderError(
`Provided module can't be loaded.\n${additionalHint}` +
`Detailed stack trace: ${err.stack}`
);
return null;
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ export const main = async () => {
options.target,
options.signatureType
);
if (!loadedFunction) {
console.error('Could not load the function, shutting down.');
// eslint-disable-next-line no-process-exit
process.exit(1);
}
const {userFunction, signatureType} = loadedFunction;
const server = getServer(userFunction!, signatureType);
const errorHandler = new ErrorHandler(server);
Expand All @@ -58,8 +53,8 @@ export const main = async () => {
})
.setTimeout(0); // Disable automatic timeout on incoming connections.
} catch (e) {
if (e instanceof OptionsError) {
console.error(e.message);
if (e instanceof OptionsError || e instanceof LoaderError) {
console.error(e);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
Expand Down
12 changes: 4 additions & 8 deletions test/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,19 @@ describe('loading function', () => {
}

it('fails to load a module that does not exist', async () => {
const loadedFunction = await loader.getUserFunction(
expect(await loader.getUserFunction(
process.cwd() + '/test/data/does_not_exist',
'functionDoesNotExist',
'http'
);

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

it('fails to load a function that does not exist', async () => {
const loadedFunction = await loader.getUserFunction(
expect(await loader.getUserFunction(
process.cwd() + '/test/data/with_main',
'functionDoesNotExist',
'http'
);

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

it('loads a declaratively registered function', async () => {
Expand Down