Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ betterr Public

A better way to handle errors. Both data and errors are declared with const, available at the top level, and non-nullable (once the other is handled). Errors are always Error objects.

License

Notifications You must be signed in to change notification settings

syhner/betterr

Repository files navigation

betterr

A better way to handle errors

build status coverage vulnerabilities

Installation

$ npm install betterr

Usage

betterr executes a callback and returns an object with data (callback return value) and err (error during execution), one of which will be null depending on the success of the callback.

// const { betterr, betterrSync } = require('betterr');
import { betterr, betterrSync } from 'betterr';

// user: User | null, err: Error | null
const { data: user, err } = await betterr(() => getUserWithId(1));

// Safely use optional chaining without having to handle the error case
const maybeName = user?.name;

// The error is guaranteed to be an Error object inside the conditional block
if (err) return;

// The data (user) is now non-nullable due to an early return in the error case
const name = user.name;
  • betterr can be used with both asynchronous and synchronous callbacks
  • betterrSync can only be used with synchronous callbacks, but avoids wrapping the data in a promise so that await is not necessary

TypeScript

Both betterr and betterrSync are generic.

  • The callback return type must be assignable to the first generic parameter (for data). It defaults to the callback return type.
  • The second generic parameter (for error) must extend the Error object. It defaults to Error.
/**
 * const betterrSync: <TData, TError extends Error = Error>
 * (callback: () => TData) =>
 * | { data: TData; err: null }
 * | { data: null; err: TError }
 */

const { data, err } = betterrSync<User, RangeError>(() => ({ id: 1 }));
// data: User | null, err: RangeError | null

About

A better way to handle errors. Both data and errors are declared with const, available at the top level, and non-nullable (once the other is handled). Errors are always Error objects.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •