Skip to content

Try/catch as an expression with filtered catch clauses.

Notifications You must be signed in to change notification settings

JsCommunity/try-expr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

try-expr Build Status

Try/catch as an expression with filtered catch clauses.

Install

Installation of the npm package:

> npm install --save try-expr

Usage

import tryExpr from 'try-expr'

const data = tryExpr(
  JSON.parse
).catch(SyntaxError, error => {
  // syntax error, returns a default value
  return null
})(jsonValue)

Ideal with promises:

const safeParseJson = tryExpr(
  JSON.parse
).catch(SyntaxError, error => {
  return null
})

readFile('./data.json').then(safeParseJson).then(data => {
  // do something with the data
})

Works with async functions:

const safeReadFile = tryExpr(
    readFile
).catch({ code: 'ENOENT'}, error => {
  return ''
})

safeReadFile('./data.json').then(data => {
  // do something with the data
})

If no function is passed to tryExpr(), the first param of the chain will be considered as the error to match catch clauses with, which makes it handy with Promise#catch():

readFile('./data.json').catch(
  tryExpr().catch({ code: 'ENOENT' }, error => {
    // file does not exist, return an empty string for instance
    return ''
  })
)

Development

# Install dependencies
> npm install

# Run the tests
> npm test

# Continuously compile
> npm run dev

# Continuously run the tests
> npm run dev-test

# Build for production (automatically called by npm install)
> npm run build

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet

About

Try/catch as an expression with filtered catch clauses.

Resources

Stars

Watchers

Forks

Packages

No packages published