question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-undef-init vs init-declarations using try/catch

See original GitHub issue

How would you propose assigning data in a try/catch?

For instance:

let data;

try {
  data = somethingThatCouldGoWrong();
} catch (error) {
  // handle error
}

We currently disable @typescript-eslint/init-declarations to overcome this problem.

Another workaround would be:

let data = undefined;

try {
  data = somethingThatCouldGoWrong();
} catch (error) {
  // handle error
}

But this would mean I need to disable no-undef.

In Vue, for example, the template needs the variable to always be defined before it can reliably be accessed.

This is an issue I bumped into while working on #577, but it seems too broad to mention there.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
EvgenyOrekhovcommented, Jul 26, 2022

@alexkramer98 I see. Let’s disable init-declarations and @typescript-eslint/init-declarations for .vue files in https://github.com/EvgenyOrekhov/eslint-config-hardcore/pull/577.

1reaction
alexkramer98commented, Jul 25, 2022

@EvgenyOrekhov I would prefer init-declarations to be disabled. (Maybe just for .vue files?).

Unfortunately, your approach of keeping the variable in the scope of the try-block is not possible when that variable needs to be accessible in the template at some point when using the composition API (which Vue 3 has quite strongly moved towards).

<template>
<span>My data is: {{ data }}</span>
</template>

<script setup>
try {
  const data = somethingThatCouldGoWrong();
  // do something with `data`
} catch (error) {
  // handle error
}
</script>

<style>
</style>

data would not exist in the template.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling in R with tryCatchLog: Catching, logging, ...
The main advantages of the tryCatchLog function over tryCatch are: Easy logging of errors, warnings and messages into a file or console ...
Read more >
tryCatch in R execute in case of error
Is it possible to execute certain commands in case of error when using tryCatch in R ? I am using the code below...
Read more >
try...catch - JavaScript - MDN Web Docs - Mozilla
The try...catch statement is comprised of a try block and either a catch block, a finally block, or both. The code in the...
Read more >
Basic Error Handing in R with tryCatch()
In this post we'll try to clarify a few things and describe how R's error handling functions can be used to write code...
Read more >
Exceptions and debugging - Advanced R. - Hadley Wickham
tryCatch () is a general tool for handling conditions: in addition to errors, you can take different actions for warnings, messages, and interrupts....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found