1.0.0 ⢠Published 6 years ago
lefu v1.0.0
lefu 
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 lefuUsage
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 TypeErrorOr 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 TypeErrorContributing
Read the contributing guidelines
Licence
Read the licence
š©š©š©
1.0.0
6 years ago