try-catcher
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

try-catcher

Support class based catchers similar to java / c#.

install

npm install -s try-catcher

Usage

import { tryCatch, tryCatcher, Catch, Finally } from 'try-catcher';

class MyCustomError extends Error {}

// non async example
tryCatch(
    () => { throw new MyCustomError; },
    Catch(MyCustomError, e => { /* do something */ }),
    Catch(Error, e => { /* do something else */ }),
    Finally(() => { /* close db connection maybe? */ })
);

// promise example
const color = await tryCatch(
    http.get('/get/default/color'),
    Catch(Error404, () => { return 'red' }),
    Catch(Error500, () => { return 'black' }),
    Catch(Error, (e) => {
        log(error);
        throw e;
    })
);

// async example
await tryCatch(
    async () => { /* work */ },
    Catch(Error, async (e) => { /* more work */ }),
    Finally(async () => { /* more async work */ }),
)
The try catch will return the result of the function if no error else a catch return else will just throw the error if none exist.

If a promise or async function is used then tryCatch will be awaitable. All async functions will be awaited before try catch returns results.

Finnaly will be run even if no catcher caught the error. In that case after finally runs then the initially error would be thrown and your next line after tryCatch would not be hit (assuming you await or have a synchronous trier)

Readme

Keywords

none

Package Sidebar

Install

npm i try-catcher

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

43.1 kB

Total Files

11

Last publish

Collaborators

  • matthewdaniel