@yakika/func
v1.0.2
Published
Error handling without try/catch
Downloads
3
Readme
Error handling without try/catch and error transforming
Inspired by golang error system and C return values.
Func Usage
Import or Require
import { func, trErr } from '@yakika/func'
// OR
const { func, trErr } = require('@yakika/func')
Func without transform
function myFunction() {
const [res, err] = await func(someAsyncTask())
if(err) { return handleError() }
return res
}
Func with transform
function myFunction() {
const [res, err] = await func(
someAsyncTask(), `couldn't do it`
)
// err.message will be of format
// `error: ${msg}\n- original error: ${originalErr}`
if(err) { return handleError(err) }
return res
}
Transform can be used out of func context
function myFunction() {
try{
// Some code here that might throw an Error
}catch(e=>{
handleError(trErr('some code threw an error'))
})
}
Transform ca be used with a promise error immediatly
async function myFunction() {
const prom = new Promise((s,r)=>{r()})
prom().catch(trErr('That promised failed :('))
}
Requirements
For testing:
- NodeJS >= 8.0.0
For using:
- NodeJS >= 6.0.0