New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

throw-utils

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throw-utils

Helpers for error throwing

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

throw-utils

Actions Status npm license

Tiny helpers for error throwing.

Installation

npm i throw-utils

Use Cases

  • Assign value or throw error if value is empty:

    import { throwError } from 'throw-utils';
    
    - if (!process.env.FOO) {
    -   throw new Error('FOO is not defined');
    - }
    - const foo = process.env.FOO;
    
    + const foo = process.env.FOO || throwError('FOO is not defined');
    
  • Return result from function or throw error if result is empty:

    const { throwIf } = require('throw-utils');
    
    function foo(a) {
    - if (!a) {
    -   throw new Error('Parameter a is required.');
    - }
    - return result;
    
    + return result || throwError('Empty result');
    }
    
  • Check function parameters in single line:

    import { throwIf } from 'throw-utils';
    
    function f(a) {
    - if (!a) {
    -   throw new Error('Parameter a is required.');
    - }
    
    + throwIf(!a, 'Parameter a is required.');
    }
    

API

throwError

Throws new error. Allows simple usage of throw in expressions and arrow functions.

FunctionType
throwError(msg: ErrorLike) => never

throwIf

Conditionally throws error. Convenient replacement of if...throw block with one-liner:

FunctionType
throwIf(condition: unknown, msg: ErrorLike) => void

toError

Converts anything to Error.

FunctionType
toError(msg: ErrorLike) => Error

License

MIT @ Vitaliy Potapov

FAQs

Package last updated on 21 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts