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

exceptionout

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exceptionout

no more exceptions

latest
Source
npmnpm
Version
0.0.7
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

exceptionout = no more exceptions

With exceptionout you can get rid of exceptions and write your code in more predictable, functional way.
Simply wrap code that may throw, in one of the following functions and deal with error in an easy way.

Table of Contents

  • Either
  • Optional
  • Result
  • Tuple

Either

Either is inspired by either monad and stands for either Left (error) or Right (success).

import { either } from "exceptionout";

const externalCall = Math.random() >= 0.5 
  ? Promise.resolve(42) 
  : Promise.reject(new Error("external error"));

const result = await either(externalCall, () => new Error("error"));

result
  .mapRight((r) => r.toFixed(1)) // r is number
  .mapLeft((l) => l.message) // l is Error

Optional

Optional means value or undefined if an error occurred.

import { either } from "exceptionout";

const externalCall = Math.random() >= 0.5 
  ? Promise.resolve(42) 
  : Promise.reject(new Error("external error"));

const result = await optional(externalCall);

if (result) {
    result.toFixed(1); // result is number
}

Result

Result is an union type of Error or success T.

import { result, isSuccess } from "exceptionout";

const externalCall = Math.random() >= 0.5 
  ? Promise.resolve(42) 
  : Promise.reject(new Error("external error"));

const result = await result(externalCall);

if (isSuccess(result)) {
  result.toFixed(1); // result is number
} else {
  result.message; // result is Error
}

Tuple

Tuple represents a tuple of Error and success T.

import { tuple } from "exceptionout";

const externalCall = () => Math.random() >= 0.5 
  ? Promise.resolve(42) 
  : Promise.reject(new Error("external error"));

const [, success] = await tuple(externalCall());

if (success) {
    success.toFixed(1); // success
}

const [error] = await tuple(externalCall());

if (error) {
    error.message; // error
}

Keywords

functional programming

FAQs

Package last updated on 13 Sep 2021

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