@nkp/error
TypeScript icon, indicating that this package has built-in type declarations

0.0.14 • Public • Published

@nkp/error

npm version Node.js Package snyk

Npm package that coerces an unknown values into an instance of the JavaScript Error class.

Exposes two methods: maybeError and alwaysError.

Table of contents

Installation

NPM

npm install @nkp/error

Yarn

yarn add @nkp/error

Exports

@nkp/error targets CommonJS and ES modules. To utilise ES modules consider using a bundler like webpack or rollup.

Usage

maybeError

  • tries to coerce the value to an Error instance but may fail if the value is not ErrorLike
  • returns a Maybe instance using the library @nkp/maybe
    • returns a Some instance on success
    • returns a None instance on failure
import { Maybe } from '@nkp/maybe';

function maybeError(oldError: unknown): Maybe<Error>;
import { maybeError, ErrorLike } from '@nkp/error';

try {
  throw { message: 'something went wrong', } ErrorLike; 
} catch (_err: unknown) {
  maybeError(_err)
    // if failed to coerce, log the failure and create a new error instead
    .tapNone(() => console.warn('Warning: unknown error', _err))
    .mapNone(() => new Error('unknown error'))
    .throw();
}

alwaysError

  • always returns an Error instance
  • if the value was not ErrorLike:
    • alwaysError will return an error with the coercion failure reason
    • the coercion failure reason will be logged to the console
import { alwaysError } from '@nkp/error';

try {
  throw 'something went wrong';
} catch (_err: unknown) {
  throw alwaysError(_err);
  // throw alwaysError(_err, { silent: true });
  // throw alwaysError(_err, { logger: console });
}

Publishing

To a release a new version:

  1. Update the version number in package.json
  2. Push the new version to the master branch on GitHub
  3. Create a new release on GitHub for the latest version

A GitHub action run use npm-publish.yml to test and publish the npm package. Note, the dist folder is published.

Readme

Keywords

Package Sidebar

Install

npm i @nkp/error

Weekly Downloads

0

Version

0.0.14

License

Mozilla Public License 2.0

Unpacked Size

30.2 kB

Total Files

13

Last publish

Collaborators

  • nickkelly