@yakika/func

1.0.2 • Public • Published

Error handling without try/catch and error transforming

Inspired by golang error system and C return values.

Build Status Maintainability Coverage Status

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

Readme

Keywords

Package Sidebar

Install

npm i @yakika/func

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

130 kB

Total Files

5

Last publish

Collaborators

  • yakika