Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to return the error without throwing #169

Open
Aqzhyi opened this issue Jan 17, 2020 · 4 comments
Open

Add method to return the error without throwing #169

Aqzhyi opened this issue Jan 17, 2020 · 4 comments

Comments

@Aqzhyi
Copy link
Sponsor

Aqzhyi commented Jan 17, 2020

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
Copy link
Owner

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.

@sindresorhus sindresorhus changed the title how to get Error directly from ow(true, ow.boolean.false) Add method to return the error without throwing Feb 13, 2020
@GentileFulvio
Copy link
Contributor

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

@Aqzhyi
Copy link
Sponsor Author

Aqzhyi commented Apr 30, 2020

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
Copy link
Owner

If anyone wants to work on this, see the initial attempt and PR feedback: #176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants