Socket
Socket
Sign inDemoInstall

bail

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bail

Throw a given error


Version published
Weekly downloads
8.3M
increased by2.68%
Maintainers
1
Install size
7.13 kB
Created
Weekly downloads
 

Package description

What is bail?

The `bail` npm package is designed to provide a simple way to handle errors in asynchronous and synchronous code. It allows developers to quickly exit a function or process when an error is encountered, simplifying error handling and control flow in Node.js applications.

What are bail's main functionalities?

Error handling in asynchronous functions

This feature demonstrates how `bail` can be used in asynchronous functions to handle errors. If an error occurs during the `someAsyncOperation` call, `bail` is called with the error, effectively throwing the error and allowing it to be caught by higher-level error handlers or causing the process to exit if uncaught.

const bail = require('bail');

async function fetchData() {
  try {
    const data = await someAsyncOperation();
    return data;
  } catch (err) {
    bail(err);
  }
}

fetchData();

Error handling in synchronous functions

This feature shows how `bail` can be used in synchronous functions to handle errors. If an error occurs while reading or parsing the config file, `bail` is invoked with the error, which then either gets caught by a higher-level error handler or causes the process to exit if it remains uncaught.

const bail = require('bail');

function readConfigFile() {
  try {
    const config = fs.readFileSync('/path/to/config.json', 'utf8');
    return JSON.parse(config);
  } catch (err) {
    bail(err);
  }
}

readConfigFile();

Other packages similar to bail

Readme

Source

bail

Build Coverage Downloads Size

Throw if given an error.

Contents

What is this?

This package throws a given error.

When should I use this?

Use this package if you’re building some scripts that might theoretically get errors but frequently don’t and you keep writing if (error) throw error over and over again and you’re just really done with that.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install bail

In Deno with Skypack:

import {bail} from 'https://cdn.skypack.dev/bail@2?dts'

In browsers with Skypack:

<script type="module">
  import {bail} from 'https://cdn.skypack.dev/bail@2?min'
</script>

Use

import {bail} from 'bail'

bail()

bail(new Error('failure'))
// Error: failure
//     at repl:1:6
//     at REPLServer.defaultEval (repl.js:154:27)
//     …

API

This package exports the following identifier: bail. There is no default export.

bail(err?)

Throw a given error (Error?).

Types

This package is fully typed with TypeScript. There are no extra exported types.

Compatibility

This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+. It also works in Deno and modern browsers.

Security

This package is safe.

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer

Keywords

FAQs

Last updated on 07 Nov 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc