uffbasse
Bouncing async/await wrapper for smart error handling
Introduction
uffbasse is an enhanced async/await wrapper for smart error handling and is based on the articles Learning to Throw Again and
How to write async await without try-catch blocks in Javascript. So it returns a [err, result]
array quite similar to the error-first callbacks. Like described in the articles using async/await both run-time and developer errors are merged into one single channel. uffbasse enables to differ between types of errors and behave based on this distinction. There are basically three behaviours:
- promise resolves
returns[null, result]
- promise rejects with a matching error
returns[err, undefined]
- promise rejects with a non-matching error
returns[null, defaultResult]
and logs the error
The modules standard
and ava
are used to grant a high quality implementation.
uffbasse is the Swabian translation for take care.
Installation
For installation use the Node Package Manager ⇗:
$ npm install --save uffbasse
or clone the repository:
$ git clone https://github.com/felixheck/uffbasse
Example
With uffbasse
const to = ;const succeeded = Promise;const failedMatched = Promise;const failedNonMatched = Promise;async {await ;// returns: [null, { test: 123 }]await ;// returns: ['SyntaxError: foobar', undefined]const err res = await ;if err throw err;// returns: ['SyntaxError: foobar', undefined]// throws: 'SyntaxError: foobar'await ;// logs: foobar// returns: [null, undefined]await ;// logs: foobar// returns: [null, {}]};
Without uffbasse
const bounce = ;const succeeded = Promise;const failedMatched = Promise;const failedNonMatched = Promise;async {let res;tryres = await succeeded;catcherrbounce;console;tryres = await failedMatched;catcherrbounce;console;tryres = await failedNonMatched;catcherrbounce;console;res = {}};
API
uffbasse(promise, [options])
: Returns a [err, result]
array.
-
promise {Promise}
: The promise to be handled
Required. -
options {Object}
: Additional options to customize the behaviour.
Optional.defaults {*}
: Default value to be returned if it is a non-matching error.
Optional. Default:undefined
.log {null|Function}
: Function utilized to log non-matching errors. Ifnull
, logging is disabled. Optional. Default:console.error
.is {Function}
: Function utilized to distinct between error types.
Optional. Default:bounce.isSystem
.
By default it just returns run-time errors.
Errors due to developers are logged with console.error
.
Developing and Testing
First you have to install all dependencies:
$ npm install
To execute all unit tests once, use:
$ npm test
or to run tests based on file watcher, use:
$ npm start
To get information about the test coverage, use:
$ npm run coverage
Contribution
Fork this repository and push in your ideas.
Do not forget to add corresponding tests to keep up 100% test coverage.
For further information read the contributing guideline.