0.0.8 • Published 1 year ago

@randock/try v0.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Typed try/catch

Tries to mimick behaviour of other languages (like PHP), which allow you to have multiple catch blocks with different error types.

Basic usage is as follows:

// an example implementation could be as follows
import { Try } from '@randock/try';

await Try.to<void>(async () => {
  // do something that might throw an exception
})
  .catch(SpecificError, error => {
  // if "SpecificError" is thrown, this catch block is called
})
  .catch(AnotherSpecificError, async error => {
  // if "AnotherSpecificError" is thrown, this catch block is called
})
  .catch([FirstError, SecondError], async error => {
  // if "FirstError" or "SecondError" is thrown, this catch block is called
})
  .catch(async error => {
  // This is the catch-all, if none other matched.
  // If the original thrown thing is not an instance of Error, 
  // it will be converted to an instance of ObjectError.
})
  .finally<void>(async () => {
    // finally
});

If you don't use the "finally" block, you need to call .run();

await Try.to<void>(async () => {
}).catch(async error => {})
  .run();
0.0.8

1 year ago

0.0.7

2 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago