Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Created July 9, 2023 16:33
Show Gist options
  • Save arleighdickerson/c5f67df8698519ace118d2c6564d0cea to your computer and use it in GitHub Desktop.
Save arleighdickerson/c5f67df8698519ace118d2c6564d0cea to your computer and use it in GitHub Desktop.
safeInvoke(obj, path[], ...args[])
import _ from 'lodash';
const invoke = new Proxy(_.invoke, {
apply(target, thisArg, argArray: any[]): any {
try {
return Reflect.apply(target, thisArg, argArray);
} catch (e) {
if (process.env.NODE_ENV === 'development') {
console.debug('[safe-invoke] discarding exception', e, ...argArray);
}
}
},
});
export default function safeInvoke(object: any, path: string[], ...args: any[]): unknown {
return invoke(object, path, ...args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment