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

1.1.4 • Public • Published

Try2be: Try-catch wrapper for better javascript error handling

  • Build with Typescript, ship with Built-in TypeDefinitions
  • Handle Promise and async Function
  • Zero dependencies

Installation

Just use npm to install the library, bring with built-in type definitions:

npm i try2be

Usage

try2Arr

Convert a try-catch style code to result and error in array (Go-style, Best with array destruction)

import { try2Arr } from "try2be";

const [ret, err] = try2Arr(() => {
  return "tom";
});
// ret = 1;

const [ret, err] = try2Arr(() => {
  throw "jerry";
});
// err = 'jerry';
import { try2Arr } from "try2Arr";

const [ret, err] = try2Arr(() => {
  return "tom";
});
// ret = 1;

const [ret, err] = try2Arr(() => {
  throw "jerry";
});
// err = 'jerry';

try2Obj

Convert a try-catch style code to result and error in object (Best with object desctruction)

import { try2Obj } from "try2be";

const { ret, err } = try2Obj(() => {
  return "tom";
});
// ret = 1;

const { ret, err } = try2Obj(() => {
  throw "jerry";
});
// err = 'jerry';
import { try2Arr } from "try2Obj";

const { ret, err } = try2Obj(() => {
  return "tom";
});
// ret = 1;

const { ret, err } = try2Obj(() => {
  throw "jerry";
});
// err = 'jerry';

try2Ignore

Convert a try-catch code to just ignore throw error

import { try2Ignore } from 'try2Be'

const res = try2Ignore(() => {
    return "tom";
})
// res = "tom";

const res = try2Ignore(() => {
    return "jerry";
})
// res = undefined;

Work well with Promise and async Function

import { try2Arr } from "try2Arr";

const [ret, err] = await try2Arr(async () => {
  return "tom";
}) // Type: Promise<[string, any]>;
// ret = "tom";

const [ret, err] = await try2Arr(async () => {
  throw "jerry";
}); // Type: Promise<[undefined, any]>;
// err = "jerry";

Use with CommonJS

const { try2Arr } = require('try2Arr');

Readme

Keywords

none

Package Sidebar

Install

npm i try2be

Weekly Downloads

2

Version

1.1.4

License

MIT

Unpacked Size

12.7 kB

Total Files

9

Last publish

Collaborators

  • wwwzbwcom