Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fisker/swallow-errors
Swallow function errors.
https://github.com/fisker/swallow-errors
Last synced: 6 days ago
JSON representation
Swallow function errors.
- Host: GitHub
- URL: https://github.com/fisker/swallow-errors
- Owner: fisker
- License: mit
- Created: 2021-11-26T04:46:07.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T16:42:59.000Z (18 days ago)
- Last Synced: 2024-11-01T05:04:45.907Z (15 days ago)
- Language: JavaScript
- Size: 73.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 30
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# swallow-errors
[![Build Status][github_actions_badge]][github_actions_link]
[![Coverage][coveralls_badge]][coveralls_link]
[![Npm Version][package_version_badge]][package_link]
[![MIT License][license_badge]][license_link][github_actions_badge]: https://img.shields.io/github/workflow/status/fisker/swallow-errors/CI/main?style=flat-square
[github_actions_link]: https://github.com/fisker/swallow-errors/actions?query=branch%3Amain
[coveralls_badge]: https://img.shields.io/coveralls/github/fisker/swallow-errors/main?style=flat-square
[coveralls_link]: https://coveralls.io/github/fisker/swallow-errors?branch=main
[license_badge]: https://img.shields.io/npm/l/prettier-format.svg?style=flat-square
[license_link]: https://github.com/fisker/swallow-errors/blob/main/license
[package_version_badge]: https://img.shields.io/npm/v/swallow-errors.svg?style=flat-square
[package_link]: https://www.npmjs.com/package/swallow-errors> Ignore function errors.
## Install
```bash
yarn add swallow-errors
```## Usage
```js
import {wrap, execute} from 'swallow-errors'const foo = wrap(() => {
throw new Error('oops!')
})
// Returns a function will never throw errorsexecute(() => {
throw new Error('oops!')
})
// Execute the function and ignore possible errors
```## API
### `wrap(originalFunction, ignore?)`
Type: `function`
Returns a function that will ignore errors passed `ignore` test.
#### `originalFunction`
Type: `function`
The function to wrap
#### `ignore`
Type: `function`
The error test function, if it's omitted, all errors will be ignored.
To ignore specific errors, return `true`
```js
const foo = wrap(
function () {
throw new Error('foo')
},
(error) => error?.message === 'bar',
)foo()
// Throws a error with message `foo`
```### `execute(originalFunction, ignore?)`
Execute the wrapped function **without arguments**.