sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.8k stars 105 forks source link

Add method to return the error without throwing #169

Open Aqzhyi opened 4 years ago

Aqzhyi commented 4 years ago

Hello, thank this excellent module come to this world. Make much convenient!

I want to send error.message to Google Analytics and control flow in TypeScript, now I use this workaround:

// TypeScript control flow
try {
  ow(!gameId || !gameTitle, ow.boolean.false)
} catch (error) {
  gaAPI.send({
    ec: 'linebot',
    ea: `${gameTitle}/query/streams/error`,
    el: JSON.stringify({
      context: `!gameId || !gameTitle`,
      errorMessage: error.message,
    }),
  })
  await context.sendText(i18nAPI.t('error/internal'))
}
if (!gameId || !gameTitle) return

const response = await twitchAPI.getStreams({
  gameId,
  language: LanguageParam.zh,
})

How can I get rid of TryCatch? like

// TypeScript control flow
const error = ow.getError(!gameId || !gameTitle, ow.boolean.false)

if (error && (!gameId || !gameTitle)) {
  gaAPI.send({
    ec: 'linebot',
    ea: `${gameTitle}/query/streams/error`,
    el: JSON.stringify({
      context: `!gameId || !gameTitle`,
      errorMessage: error.message,
    }),
  })

  await context.sendText(i18nAPI.t('error/internal'))
  return
}

const response = await twitchAPI.getStreams({
  gameId,
  language: LanguageParam.zh,
})
sindresorhus commented 4 years ago

We don't have a method for that, but it sounds useful, so PR welcome. It could also be useful if you need to augment the error object in some way before throwing.

GentileFulvio commented 4 years ago

What is the status on this issue ? Otherwise i'd like to have a look at it

Aqzhyi commented 4 years ago

What is the status on this issue ? Otherwise i'd like to have a look at it

Hello @GentileFulvio, I have a PR #176, and I am not sure why codecov -0.11%. and it #176 is the last commit.

sindresorhus commented 3 years ago

If anyone wants to work on this, see the initial attempt and PR feedback: https://github.com/sindresorhus/ow/pull/176