Skip to content

a lightweight utility for typescript/javascript that simplifies error handling for both synchronous and asynchronous functions.

License

Notifications You must be signed in to change notification settings

mcking-07/safe-wrapper

Repository files navigation

safe-wrapper

npm version License Build Status npm downloads

safe-wrapper is a lightweight, typescript-first utility that simplifies error handling for both synchronous and asynchronous functions. inspired by the safe assignment operator proposal, this utility allows for graceful error management by wrapping functions in a way that enables error handling without the need for explicit try-catch blocks.

in modern javascript and typescript, dealing with functions that might throw errors (especially in asynchronous operations or third-party libraries) often leads to repetitive boilerplate. safe-wrapper offers a cleaner and a more functional approach by reducing boilerplate, enabling full typescript support for type inference, and allowing for flexible error handling by letting you filter specific error types, or transform them into custom formats.

Features

  • handles synchronous and asynchronous functions.
  • supports specifying error types to control which errors are caught and handled.
  • returns a consistent response in [error, result] format,
    • error: null if successful, else an error (or transformed object).
    • result: return value (sync) or resolved value (async) when successful, else null.
  • supports custom error transformation for advanced error handling.
  • written in typescript with comprehensive type definitions, enabling full type inference support.

Installation

npm install safe-wrapper

Usage

import safe from safe-wrapper to use it with any function. the types for error and result are automatically inferred.

the safe function takes a target function (synchronous or asynchronous) and returns a function which handles errors and returns a response in a consistent way. the function returns an array [error, result], where error is an instance of the specified error type or null if successful, and result is the result of the function when there is no error.

directly wrapping functions

we can directly wrap any function definition with safe.

import { safe } from 'safe-wrapper';

const safeSync = safe((args) => {
  throw new Error('sync error occurred');
});

const safeAsync = safe(async (args) => {
  throw new Error('async error occurred');
});

const [error, result] = await safeAsync(args);

wrapping existing functions

safe can be applied to pre-existing functions, including ones from third-party libraries.

import { safe } from 'safe-wrapper';

const sync = (args) => {
  throw new Error('sync error occurred');
}

const safeSync = safe(sync);
const [error, result] = safeSync(args);

handling specific error types

we can specify error types to catch, allowing other errors to throw.

import { safe } from 'safe-wrapper';

const safeAsync = safe(async (args) => {
  throw new TypeError('async type error occurred');
}, [TypeError]);

const [error, result] = await safeAsync(args);

handling multiple error types

we can specify multiple error types when wrapping a function, enabling safe to catch any of the specified errors.

import { safe } from 'safe-wrapper';

const sync = (args: boolean): string => {
  if (args) {
    throw new TypeError('sync type error occurred');
  } else {
    throw new RangeError('sync range error occurred');
  }
}

const safeSync = safe(sync, [TypeError, RangeError]);
const [error, result] = safeSync(args);

using synchronous custom error transformer

you can provide a custom error transformer function to modify how errors are handled:

import { safe } from 'safe-wrapper';

type TransformedError = { code: string, message: string, timestamp: Date };

const transformer = (error: Error): TransformedError => ({
  code: error.name,
  message: error.message,
  timestamp: new Date()
});

const safeWithTransform = safe(
  () => { throw new Error('custom sync error'); },
  [Error],
  transformer
);

const [error, result] = safeWithTransform();
// error will be of type TransformedError as: { code: 'Error', message: 'custom sync error', timestamp: Date }

using asynchronous custom error transformer

you can provide an asynchronous custom error transformer to modify how errors are handled.

import { safe } from 'safe-wrapper';

type AsyncTransformedError = { code: string, message: string, timestamp: Date };

const transformer = async (error: Error): Promise<AsyncTransformedError> => {
  await report(error);

  return {
    code: error.name,
    message: error.message,
    timestamp: new Date()
  }
}

const safeWithTransform = safe(
  async () => { throw new Error('custom async error'); },
  [Error],
  transformer
);

const [error, result] = await safeWithTransform();
// error will be of type AsyncTransformedError as: { code: 'Error', message: 'custom async error', timestamp: Date }

wrapping built-in functions

we can also wrap built-in functions, like JSON.parse, Object.keys, and more.

import { safe } from 'safe-wrapper';

const safeJsonParse = safe(JSON.parse);
const [error, result] = safeJsonParse('invalid_json');

const [error, result] = safe(Object.keys, [TypeError])(null);

API Reference

safe(action, types, transformer)

  • parameters
    • action (function): function to be wrapped. it can either be synchronous or asynchronous.
    • types (array, optional): an array of error types to catch. if no types are specified, all errors are caught. each element must be a valid error constructor.
    • transformer (function, optional): a function that takes an error object and returns a transformed version of it. if not provided, the original error is used.
  • returns [error, result]
    • error (error | null): the error object if error occurred, otherwise null. if an transformer is provided, this will be the transformed error.
    • result (result | null): the result of the action function if no error occurred, otherwise null.

About

a lightweight utility for typescript/javascript that simplifies error handling for both synchronous and asynchronous functions.

Topics

Resources

License

Stars

Watchers

Forks