throw-not
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

throw-not

Utilites to help writing code using the pattern of returning errors instead of throwing them.

Part of the throw-not family of packages:

Install

npm install throw-not
pnpm install throw-not
yarn install throw-not

Example

import { Result, isError } from 'throw-not';

function somethingThatCouldThrow(): Result<string> {
  if (...) {
    return new Error('something went wrong');
  }
  return 'something';
}

function something(): string {
  const result = somethingThatCouldThrow();
  if (isError(result)) {
    return 'failed';
  }
  return result;
}
// suppose ApiError is an error class created by `that-error`
import { ApiError, isApiError } from './api-errors';

function somethingThatCouldThrow(): Result<string, InstanceType<typeof ApiError>> {
  if (...) {
    return new ApiError('not_found', 'customer', 'cus_1234567890');
  }
  return 'something';
}

function get(req, res): string {
  const result = somethingThatCouldThrow();
  if (isApiError(result)) {
    return res.send(result.meta.statusCode, {
      message: result.message,
      meta: result.meta
    });
  }
  return res.send(200, result);
}

Readme

Keywords

none

Package Sidebar

Install

npm i throw-not

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

4.56 kB

Total Files

6

Last publish

Collaborators

  • bmealey