time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

wrap.js (2117B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isFunction = require( '@stdlib/assert/is-function' );
     24 var isError = require( '@stdlib/assert/is-error' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Wraps a function in a try/catch block.
     31 *
     32 * @param {Function} fcn - function to wrap
     33 * @param {*} [thisArg] - function context
     34 * @throws {TypeError} must provide a function
     35 * @returns {Function} wrapped function
     36 *
     37 * @example
     38 * function fcn() {
     39 *     throw new Error( 'beep boop' );
     40 * }
     41 *
     42 * var f = wrap( fcn );
     43 *
     44 * var out = f();
     45 * if ( out instanceof Error ) {
     46 *     console.error( out.message );
     47 *     // => 'beep boop'
     48 * }
     49 */
     50 function wrap( fcn, thisArg ) {
     51 	var ctx;
     52 	if ( !isFunction( fcn ) ) {
     53 		throw new TypeError( 'invalid argument. Must provide a function. Value: `' + fcn + '`.' );
     54 	}
     55 	if ( arguments.length > 1 ) {
     56 		ctx = thisArg;
     57 	} else {
     58 		ctx = null;
     59 	}
     60 	return wrapped;
     61 
     62 	/**
     63 	* Wrapped function.
     64 	*
     65 	* @private
     66 	* @param {...*} [args] - function arguments
     67 	* @returns {*|Error} returned value or an error object
     68 	*/
     69 	function wrapped() {
     70 		var args;
     71 		var len;
     72 		var i;
     73 
     74 		len = arguments.length;
     75 		args = new Array( len );
     76 		for ( i = 0; i < len; i++ ) {
     77 			args[ i ] = arguments[ i ];
     78 		}
     79 		try {
     80 			return fcn.apply( ctx, args );
     81 		} catch ( error ) {
     82 			if ( isError( error ) ) {
     83 				return error;
     84 			}
     85 			// Handle thrown literals...
     86 			if ( typeof error === 'object' ) {
     87 				return new Error( JSON.stringify( error ) );
     88 			}
     89 			return new Error( error.toString() );
     90 		}
     91 	}
     92 }
     93 
     94 
     95 // EXPORTS //
     96 
     97 module.exports = wrap;