JSPM

@yakika/func

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q24817F
  • License MIT

Error handling without try/catch

Package Exports

  • @yakika/func

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@yakika/func) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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