Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @yakika/func with all npm packages installed. Try it out:

var func = require("@yakika/func")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@yakika/func v1.0.2

Error handling without try/catch

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

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free