@stdlib/utils-try-function
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published
About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

Try Function

NPM version Build Status Coverage Status

Wrap a function in a try/catch block.

Installation

npm install @stdlib/utils-try-function

Usage

var wrap = require( '@stdlib/utils-try-function' );

wrap( fcn )

Wraps a function in a try/catch block.

function fcn() {
    throw new Error( 'beep boop' );
}

var f = wrap( fcn );

var out = f();
if ( out instanceof Error ) {
    console.error( out.message );
    // => 'beep boop'
}

The returned function has the same signature as the wrapped function.

function fcn( a, b, c, d ) {
    var sum = a + b + c + d;
    if ( sum < 10 ) {
        throw new Error( 'invalid arguments. Arguments must sum to a number greater than or equal to 10.' );
    }
    return sum;
}

var f = wrap( fcn );

var out = f( 5, 6, 7, 8 );
// returns 26

out = f( 1, 2, 3, 1 );
// returns <Error>

If provided an asynchronous function, the returned function only traps errors which occur during the current event loop tick.

function fcn( a, b, clbk ) {
    if ( !a ) {
        throw new Error( 'invalid argument.' );
    }
    setTimeout( onTimeout, 0 );
    function onTimeout() {
        if ( !b ) {
            throw new Error( 'invalid argument.' );
        }
        clbk();
    }
}

function done() {
    console.log( 'beep' );
}

var f = wrap( fcn );

var out = f( null, 5, done );
// returns <Error>

out = f( true, null, done );
// returns undefined

Notes

  • Isolating try/catch blocks as separate wrapped functions prevents a parent scope from permanently entering optimization hell.
  • If a function throws a literal, the literal is serialized as a string and returned as an Error object.

Examples

var wrap = require( '@stdlib/utils-try-function' );

function beep( str ) {
    if ( typeof str !== 'string' ) {
        throw new TypeError( 'invalid argument. Must provide a string. Value: `' + str + '`.' );
    }
    return 'beep ' + str;
}

function boop( str, clbk ) {
    if ( typeof str !== 'string' ) {
        throw new TypeError( 'invalid argument. Must provide a string. Value: `' + str + '`.' );
    }
    setTimeout( done, 1000 );

    function done() {
        if ( str !== 'beep' ) {
            throw new Error( 'invalid argument. String must equal `beep`. Value: `' + str + '`.' );
        }
        clbk( str + ' boop' );
    }
}

function done( str ) {
    if ( str !== 'beep boop' ) {
        throw new Error( 'huh?' );
    }
}

// Synchronous...
var f = wrap( beep );

var out = f( 'boop' );
console.log( out );
// => 'beep boop'

out = f( null );
console.log( out.message );
// => '...'

// Asynchronous...
f = wrap( boop );

out = f( 'beep', done );
console.log( out );
// => undefined

out = f( 'foo', done );
console.log( out );
// => undefined

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.

Package Sidebar

Install

npm i @stdlib/utils-try-function

Homepage

stdlib.io

Weekly Downloads

1

Version

0.2.2

License

Apache-2.0

Unpacked Size

29.7 kB

Total Files

11

Last publish

Collaborators

  • stdlib-bot
  • kgryte
  • planeshifter
  • rreusser