JSPM

  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q63332F
  • License MIT

Errors as Values πŸ›Ÿ ── Protected ── πŸ“¦ zero-dependency

Package Exports

  • pcall.js

Readme

Pcall.js

logo-of-pcall

Errors as Values

zero-dependency
╢─╴╢╴╢╴╢╴╢╴╢╴╢╴╢╴╢╴╢╴╢─╴
  • 🎌 Delegate Promise Resolutions
  • 🧬 Lifecycle Callback Options
  • πŸ“ Concise and Convenient Signature
  • πŸ“¦ Zero-Dependency
  • πŸ›‘ Avoid try/catch ~~HELL~ πŸ‘Ή
  • 🌟 Better Visibility and Control
  • 🌐 Works in Node.js (ESM/CJS) and all modern browsers
  • 〽️ Minimal Obsessive Disorder



simple

pcall-simple-usage

options

pcall-option-usage

custom

pcall-custom-usage

create new safe utilities with shared side-effect behavior



Inspiration

Lua approach to error handling is simple yet powerful. ^Lua:5.4 ^Lua:8.4, ^Lua:8.5

πŸ”Ή pcall.js is heavily inspired by Lua pcall with superpowers πŸ¦„!


SYNOPSIS

pcall({f}, {arg1}, {...})

pcall() Calls function {f} with the given arguments in protected mode. This means that any error inside {f} is not propagated;

Instead, pcall catches the error and returns a tuple.

Its first element is the status code (a boolean), which is true if the call succeeds without errors. And all results from the call, on second element; [true, {res}]

In case of any error, pcall returns false plus the error message; [false, {err}]


Usage

# install
npm install pcall.js
// ESM
import Pcall from 'pcall.js'

// CJS
const Pcall = require('pcall.js')

Convert

import { readFile } from 'node:fs/promises'

// πŸ”» BEFORE
try {
  const res = await readFile('./package.json', { encoding: 'utf8' })
} catch(error) {
  console.error(error, 'πŸ”₯')
}

// ─────────────────
// πŸ”ΉAFTER
import Pcall from 'pcall.js'
const [err, user] = await Pcall(readFile, './package.json', { encoding: 'utf8' })

// πŸ”ΈTHROW
err && throw new Error("XYZZY", { cause: err });

// ─────────────────
// πŸ”Έ NO @ITERATOR
import Pcall from 'pcall.js'
const pcall = new Pcall({ noError: true })
const res = await pcall(readFile, './package.json', { encoding: 'utf8' })

// ─────────────────
// πŸ”Έ MOCK
const readJson = new Pcall({
  fn: readFile,
  noError: true,
  args: [{ encoding: 'utf8' }],
  transformOnSuccess: (args, res) => JSON.parse(res),
  transformOnFailure: (args, { name, message }) => ({ name, message }),
})
const path = 'test/sample-good.json'

const res = await readJson(path)
log(res.hogo) // fuga

Options

import { readFile } from 'node:fs/promises'
import Pcall from 'pcall.js'

const pcall = new Pcall({
  onSuccess: (args, res) => log({ res, args }, true),
  onFailure: (args, err) => log({ err, args }, false),
  transformOnSuccess: (args, res) => res,
  transformOnFailure: (args, res) => res,
  cleanup: () => {},
  noError: false,
  noTrace: false,
})

const path = './package.json'
const opts = { encoding: 'utf8' }

const [err, res] = await pcall(readFile, path, opts)

πŸ’‘ Check test/ files for more examples


Development

# run test playground in watch mode
npm run dev

# build production
npm run build

# build stub
npm run build:stub

TODO

  • πŸŒ€ Lifecycle Hooks
  • [.] πŸ”Œ Serializer
  • [.] 🧬 Parser
  • [.] πŸ“œ JSDoc
  • [.] πŸ”§ ESLint
  • [o] πŸ“– Docs
  • [o] ⚠️ Tests
  • [.] πŸ’‘ Examples

License

MIT