Skip to content

Instantly share code, notes, and snippets.

@mholtzhausen
Last active September 11, 2020 09:24
Show Gist options
  • Save mholtzhausen/81e30dc90525f28ac3c85009d0b4566e to your computer and use it in GitHub Desktop.
Save mholtzhausen/81e30dc90525f28ac3c85009d0b4566e to your computer and use it in GitHub Desktop.
Catch -- try-catch wrapper for promises (with a bonus)

Catch

A wrapper for promise.catch() that guarantees a resolved promise

normally if you want to use 'await' in the root of your project, you have to wrap it thusly:

  let promise=Promise.reject(new Error('test error'))
  try{
     ;(async ()=>{
       try{
          let result = await promise
          console.log(`The promise results in : ${result}`)
       }catch(error){
          console.log(`The promise responded with an error: ${error.message}`)
       }
     })()
  }catch(error){
    console.log(`some error has occurred: $(error.message)`)
    process.exit(-1)
  }

This seems very akward and not very readable. That is why I made Catch:

  let promise=Promise.reject(new Error('test error'))
  
  Catch((async ()=>{
    let result = await Catch(promise, Catch.log.message.prefix(`The promise responded with an error:`))
    if (result) console.log(`The promise results in : ${result}`)
  })(),Catch.exit.withLog)

But this is just hiding the errors!

Not really -- it hides the errors by default and lets you specify an error handler in a way that results in more readable code. Very frequently, we discard caught errors, because it just denotes that a resource or operation did not succeed. This is also very frequently evident from the fact that the result is undefined or null, and often we need no more than that.

It is for this purpose this utility was made. It just abstracts away the requirements of dealing with errors when there is no interest in the errors that result from a promise.

const Catch = async (promise, errorHandler) => {
if (promise instanceof Promise) {
try {
let returnValue = await promise
return returnValue
} catch (e) {
if (typeof errorHandler === 'function') return errorHandler(e)
return errorHandler
}
}
return promise
}
// Some default error handlers
//log if an error is found
Catch.log = console.log
//log just the message when an error is found
Catch.log.message = e => console.log(e.message)
//create a error message log function with a prefix
Catch.log.message.prefix = prefix => (e => console.log(`${prefix} ${e.message}`))
//process.exit when an error is found
Catch.exit = e => process.exit(-1)
//process.exit with a success code when an error is found
Catch.exit.clean = e => process.exit(0)
//log the error and process.exit when an error is found
Catch.exit.withLog = e => { console.log(e); process.exit(-1) }
//log the error and process.exit with a success code when an error is found
Catch.exit.clean.withLog = e => { console.log(e); process.exit(0) }
module.exports = Catch
/*******************************************************************************
Below is the usage example, you can remove without effect
*******************************************************************************/
if(false){
// normally if you want to use 'await' in the root of your project, you have to wrap it thusly:
let promise=Promise.reject(new Error('test error'))
try{
;(async ()=>{
try{
let result = await promise
console.log(`The promise results in : ${result}`)
}catch(error){
console.log(`The promise responded with an error: ${error.message}`)
}
})()
}catch(error){
console.log(`some error has occurred: $(error.message)`)
process.exit(-1)
}
// this seems very akward and not very readable. That is why I made `Catch`:
Catch((async ()=>{
let result = await Catch(promise, Catch.log.message.prefix(`The promise responded with an error:`))
if (result) console.log(`The promise results in : ${result}`)
})(),Catch.exit.withLog)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment