Skip to content

Commit

Permalink
fix: Return undefined instead of throw an exception
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Now "not found module" will return undefined value instead of throw an exception
  • Loading branch information
edgardmessias committed Jun 14, 2022
1 parent 8163c5b commit 817592b
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/whatsapp/exportModule.ts
Expand Up @@ -61,7 +61,10 @@ export function exportModule(
const moduleId = webpack.searchId(condition);

if (!moduleId) {
throw `Module ${name} not found with ${condition.toString()}`;
console.error(
`Module ${name} was not found with ${condition.toString()}`
);
return undefined;
}

const module = webpack.webpackRequire(moduleId);
Expand All @@ -75,15 +78,23 @@ export function exportModule(
}
}
if (!valueFn()) {
throw `Property ${property} not found in module ${name}`;
console.error(
`Property ${property.join(
' or '
)} was not found for ${name} in module ${moduleId}`
);
return undefined;
}
} else {
switch (typeof property) {
case 'string':
valueFn = () =>
property.split('.').reduce((a, b) => a?.[b], module);
if (!valueFn()) {
throw `Property ${property} not found in module ${name}`;
console.error(
`Property ${property} was not found for ${name} in module ${moduleId}`
);
return undefined;
}
functionPath = property;
break;
Expand Down Expand Up @@ -165,29 +176,33 @@ export function wrapModuleFunction<TFunc extends (...args: any[]) => any>(
callback: (func: TFunc, ...args: InferArgs<TFunc>) => InferReturn<TFunc>
) {
if (typeof func !== 'function') {
throw new TypeError('func is not a function');
console.error('func is not a function');
return;
}

const moduleId = _moduleIdMap.get(func);

if (!moduleId) {
throw new TypeError('func is not an exported function');
console.error('func is not an exported function');
return;
}

const module = webpack.webpackRequire(moduleId);

const functionPath = functionPathMap.get(func);

if (!functionPath) {
throw new TypeError('function path not found');
console.error('function path was not found');
return;
}

const parts = functionPath.split('.');

const functionName = parts.pop();

if (!functionName) {
throw new TypeError(`function not found in the module ${moduleId}`);
console.error(`function was not found in the module ${moduleId}`);
return;
}

const baseModule = parts.reduce((a, b) => a?.[b], module);
Expand Down

0 comments on commit 817592b

Please sign in to comment.