Skip to content

Npm package. Coerce an unknown error into an instance of the Error class

License

Notifications You must be signed in to change notification settings

NickKelly1/nkp-error

Repository files navigation

@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.

About

Npm package. Coerce an unknown error into an instance of the Error class

Resources

License

Stars

Watchers

Forks

Packages

No packages published