errou
Error handling without try
/catch
blocks.
Installing
yarn add errou # If you're using NPM: # npm install errou
Why?
Have you ever written some code like:
; ;try catch error console.log`Contents: `;
With errou
, no more! It abstracts those ugly use cases of try
/catch
blocks for you. It supports both synchronous and asynchronous functions.
Usage
;; ; if call.ok if !call.ok
In the previous example, we passed an arrow function, which will be invoked by errou
. You can also pass a function reference:
; // call.ok// call.data// call.error
TypeScript will complain if errou
last arguments does not match with the first function's argument types.
If the function has some overloads, you must pass a lambda as done in the fs.readFile
example.
error
type
The By default, call.error
has the unknown
type. You may assert that type to your desired Error
instance or pass the correct Error
type to the second generic that errou
accepts.
Asserting the error
type
; if !call.ok
Passing the correct error
type as the second errou
generic
; if !call.ok
Note that you should pass the type of the function as the first generic.
Authors and License
lffg and contributors.
MIT License, see the included MIT file.