wrap-up v2.0.0
wrap-up
Create a wrapper to redirect exceptions to an error handler.
The library wrap-up allow to create a wrapper which redirects exceptions that occurred in a given function to an error handler.
Installation
With npm installed, run :
$ npm install wrap-up
Or if you use yarn, run:
$ yarn add wrap-up
You can use wrap-up from your JavaScript project.
Usage
To use this library, you must use wrapper
or wrapperAsync
functions:
import { wrapper } from "wrap-up"; // for synchronous function.
import { wrapperAsync } from "wrap-up"; // for asynchronous function.
and next, give your function as the following example:
// Functions that must be wrapped:
// without exception thrown.
const httpHandler = (value) => {
return { payload: value, status: "success" };
};
// with exception thrown.
const httpHandlerError = () => {
throw { error: "Unknown error" };
};
let wrapped;
wrapped = wrapper({ callback: httpHandler });
const value = wrapped("hello, world"); // return { payload: "hello, world", status: "success" } object
wrapped = wrapper({ callback: httpHandlerError });
const value = wrapped(); // return { error: "Unknown error" } object
Test
To execute the unit tests, execute following command in your favorite terminal , with npm:
$ npm test
or yarn:
$ yarn test
If you wish execute the unit tests with coverage, with npm:
$ npm run test:coverage
or yarn:
$ yarn test:coverage
License
The MIT License (MIT)
Copyright (c) 2017 Cyril Schumacher.fr
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnishet to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.