Giter VIP home page Giter VIP logo

syhner / betterr Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 1.0 180 KB

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.

Home Page: https://www.npmjs.com/package/betterr

License: MIT License

TypeScript 100.00%
async-await error-handling errors exceptions try-catch typescript package promise

betterr's Introduction

betterr

A better way to handle errors

build status coverage

Advantages

Unlike with try...catch or promises:

  • 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

Installation

$ npm install betterr

Usage

  1. Wrap any code that may throw
import { betterr } from 'betterr'; // const { betterr } = require('betterr');

const { data: user, err } = await betterr(() => getUserWithId(1));
// user: User | null, err: Error | null
  1. Now either
  • Avoid handling the error, and use optional chaining

    const maybeName = user?.name; // maybeName: string | undefined
  • Handle the error (interrupting the control flow), after which optional chaining is not needed

    if (err) return; // user: User (after error handled)
    const name = user.name; // name: string

Explanation

betterr / betterSync execute a callback and return 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.

  • 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 err) 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

betterr's People

Contributors

github-actions[bot] avatar syhner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ryoppippi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.