Async Error Handler
Simple function for handling errors when working with async and await. Instead of wrapping
async functions in try and catch
block all the time to handle possible errors,
async-error-handler
provides you a reusable function that conveniently handles the errors.
installation:
npm install async-error-handler --save
Usage:
Normally, you would handle errors in your async function like this:
const getUsersByLocationAndGender = async { try const result = await db; return result; catch err // do something with err when exception is thrown. }; ;
For expressJS route, you would have to do something like this:
app;
While this is a good approach to catch exceptions in your function, there are drawbacks when you have to deal with many async functions. Some are listed below:
- You would have to be writing try and catch all the time. Thereby, not respecting the DRY
(don't repeat yourself)
principle. - Your code becomes ugly which can affect readability.
- You would have compromised SRP
(Single Responsibility Principle)
as your function now has to do its primary work and also handle errors by itself.
async-error-handler
solves this problem. The various ways you can use it are shown below:
With function that has no parameter
const handleAsync = ; const getUsers = ; ;
With function that has parameters
When the function is called and exception is thrown, any supplied arguments will also be passed to the error callback
as the next arguments
after the err
argument. Something like: (err, arg1, arg2, ...) => {}
. Check example below.
const handleAsync = ; const getUsersByLocationAndGender = ; ;
With expressJS
When used in expressJS route callback
, if exception is thrown, all arguments express does pass to route callback will also be available to the error callback
immediately after the err
argument.
See example below:
const handleAsync = ; app;
With ReactJS
async error handler
can also be used with arrow function in ReactJS. Since ES6
has not yet supported arrow function for declaring method in a class,
you might have to make use of babel plugin called babel-plugin-transform-class-properties
or any other tools that can transpile the arrow function.
;;; { superprops; thisstate = userDetails: null error: null; } handleClick = ; { const userDetails error = thisstate; return !error ? <div> <h2>Async Error Handler with ReactJS Demo</h2> userDetails ? <p>`Welcome , you are years old`</p> : null <button onClick=thishandleClick>Show my Details</button> </div> : <div>errormessage</div>; }
Note:
- This handler should be used with functions that use async and await.
- It is advisable to always pass the second argument
(the error callback)
to the handler to avoid any uncaught error.
async-error-handler Parameters
params | description |
---|---|
asyncFunc | The async function to be handled |
errorCallback | The callback to be called when the asyncFunc throws an exception. Any arguments passed to the asyncFunc will be available to the errorCallback as next arguments after the err argument.Something like (err, ...args) where ...args are arguments passed to asyncFunc |
Testing
npm run test
Contributing
The project is open to contributions. Simply follow this guide:
- Fork the repo
- Make your contribution
- Test your work and ensure it passes.
- Create PR against the develop branch.
License
MIT