npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

@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

install

npm i @yakika/func

Downloadsweekly downloads

0

version

1.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability