0.1.1 • Published 8 months ago

pry-ts v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

pry

npm version Codecov

Ergonomic function error handling in Typescript without try catch using a result type. Works for both async/sync functions and of course, typesafe.

Getting Started

Install:

# npm
npm install pry-ts

# yarn
yarn add pry-ts

# pnpm
pnpm install pry-ts

Pass a promise or function (sync) to pry and it will return a Result type.

Asynchronous (Promise) Example

Pass your promise to pry and it will return a Result type.

import { pry } from "pry-ts";

const result = await pry(promise);
if (!result.ok) {
  console.error(result.err);
  return;
}
console.log(result.val);

// Type definition
type Result<T, U = Error> =
  | {
      ok: true;
      val: T;
    }
  | {
      ok: false;
      err: U;
    };

More examples

Synchronous (Promise)

Also works! Pass your function to pry and it will return a Result type.

const result = pry(syncFn);
const file = pry(() => fs.readFileSync("file.txt", "utf-8"));
if (!file.ok) {
  return console.error(file.err);
}
console.log(file.val);

❗️Note the arguments

  • For asynchronous, you must pass the promise NOT the function that returns a promise.
  • For synchronous, you must pass the function itself, not the result of the function.

Typescript

Result value type is inferred from your promise/function, so be sure to type that! You can also explicitly type the value.

Note that you must check ok before accessing value or error (Discriminate types).

const result = await pry<Data>(promise);
if (result.ok) {
  // Checking for `ok` or inversely for error
  result.val; // Data
}

Non-Error error typing

While in javascript you can throw any expression, the library uses a sensible default of Error to handle general use. You can also pass a custom error type.

// Typing custom errors
const result = await pry<Data, CustomError>(promise);

Design decisions

Why ok

A pattern that can be seen in javascript with Response.

const response = await fetch(url);
if (!response.ok) {
  throw new Error(response.statusText);
}
response.body;

Looking into a Go style approach

For reference, a Go error handling approach might be to return a tuple with the result and error.

const [result, err] = await goPry(promise);

However, I found it clunky when working with multiple async operations due to having to use a unique variable name for each error.

const [user, userError] = await goPry(promise);
const [category, categoryError] = await goPry(promise);
const [product, productError] = await goPry(promise);

const [[user, userError], [category, categoryError], [product, productError]] =
  await Promise.all([promise, promise, promise]);

Development

  • Clone this repository
  • Install latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Published under MIT License.

0.1.1

8 months ago

0.0.4

8 months ago

0.0.2

10 months ago

0.0.1

10 months ago

0.0.0

10 months ago