A better way to handle errors
$ npm install betterrbetterr 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;betterrcan be used with both asynchronous and synchronous callbacksbetterrSynccan only be used with synchronous callbacks, but avoids wrapping the data in a promise so thatawaitis not necessary
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
Errorobject. It defaults toError.
/**
* 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