All files / hof guarded.js

100% Statements 11/11
66.66% Branches 4/6
100% Functions 5/5
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x   3x       2x 2x 1x 1x               4x         4x 4x     2x 2x        
const DEFAULT_CONFIG = {
	context: null,
	log: (...args) => console.error(...args)
};
 
function logError(log, err) {
	log(err.stack || err);
	for (const key in err) {
		Eif (key !== 'stack') {
			log(`${key} = ${err[key]}`);
		}
	}
}
 
// returns a guarded version of a given async function.
// Errors are logged during synchronous or asynchronous execution
export default function guarded(asyncFn, config) {
	config = {
		...DEFAULT_CONFIG,
		...(config || {})
	};
 
	return function guardedFn(...args) {
		return asyncFn
			.apply(config.context, args)
			.catch(err => {
				logError(config.log, err);
				throw err;
			});
	};
}