Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

lefu

1.0.0 • Public • Published

lefu Build Status

Shit happens.

Always.

In every single line of code.

Handling errors is an hassle in JavaScript, because you do not have multiple catch block for typed errors.

This is a function way to handle try/catch constructs functionally.

Install

# npm 
npm i lefu
 
# yarn 
yarn add lefu

Usage

You can use this library as a full try/catch construct

import { Try, Catch } from 'lefu';
 
const result = Try(
    () => 1 + a,
    Catch.Error(() => 'OMG, an error'),
    Catch.TypeError(() => 'Darn! A type error'),
    Catch.CustomError(() => '🦄'),
    // ... all other errors
  );
 
console.log(result) // 'Darn! A type error'

you can also try/catch async functions

import { TryAsync, Catch } from 'lefu';
 
const result = Try(
    async () => {
      throw new TypeError('💥💥🐛💥💥');
    },
    Catch.Error(() => 'OMG, an error'),
    Catch.TypeError(() => 'Darn! A type error'),
    Catch.CustomError(() => '🦄'),
    // ... all other errors
  );
 
console.log(result) // Promise('Darn! A type error')

You can use this library for executing a function when an error is caught

import { handlers } from 'lefu';
 
try {
  1 + a
} catch (error) {
  handlers.Error(error)(() => console.log('it is an Error'));
  handlers.TypeError(error)(() => console.log('it is a TypeError'));
  handlers.MyCustomError(error)(() => console.log('it is a user defined error'));
}
 
// output:
//  it is a TypeError

Or you can use it to check if an error is of a certain type

import { match } from 'lefu';
 
try {
  1 + a
} catch (error) {
  console.log(match(error).Error); // false
  console.log(match(error).TypeError); // true
  console.log(match(error).MyCustomError); // false
}
 
try {
  throw { name: 'MyCustomError', message: 'I broke it' };
} catch (error) {
  console.log(match(error).Error); // false
  console.log(match(error).TypeError); // false
  console.log(match(error).MyCustomError); // true
}
 
// output:
//  it is a TypeError

Contributing

Read the contributing guidelines

Licence

Read the licence

💩💩💩

Install

npm i lefu

DownloadsWeekly Downloads

3

Version

1.0.0

License

none

Unpacked Size

13.3 kB

Total Files

8

Last publish

Collaborators

  • avatar