Tips on resolving the issue of 'caught local exception thrown' while utilizing instanceof for identifying the type of internal errors

I've encountered a scenario where I'm loading a set of plugins in my code. Each plugin executes a series of functions within a try-catch block to check for failures and handle them accordingly. Some functions within the plugin can return specific errors, indicating that while the plugin hasn't failed overall, it's not suitable for executing certain subsequent functions.

Let me illustrate this with an example (the code is Typescript but I'll keep it as language-neutral as possible):

for each plugin:
  try: 
    plugin.function1(data)
    plugin.function2(data)
    plugin.function3(data)

    try:
      plugin.function4(data)
    catch error:
      if error instanceof PluginNotValidForThisKindOfDataError:
        continue
      else:
        throw error
   
    plugin.function5(data)
    plugin.function6(data)
  catch error:
    log(plugin has failed)   

(I hope the code is clear enough. I'll update it if required)

In the above code snippet, when invoking function4, I handle specific errors individually because some are considered acceptable and just mean that the function is incompatible with the given data for function5 and function6. However, other errors must be thrown as they're not recoverable. Finally, all errors are caught at a higher level to determine if the entire plugin has crashed or not.

In my JetBrains IDE (specifically WebStorm), I receive a 'thrown exception caught locally' warning. I'm struggling to figure out how to redesign this block to address this warning effectively. I'm not using exceptions for control flow, but merely propagating errors.

The limitation here is that JavaScript doesn't support something like

catch PluginNotValidForThisKindOfDataError
, which would simplify handling such cases. Given the existing tools and capabilities, how can I refactor this situation?

Thank you for any insights and suggestions.

I've tagged this question under both language-agnostic and javascript due to the specifics of Javascript's try-catch mechanism.

Answer №1

There are three potential solutions that can be considered:

  1. In an ideal scenario, the plugin should not produce an error for a situation that is not actually erroneous. Rather, it would be better if it could return a specific value to indicate whether functions 5 and 6 should be executed based on the code in your query.

    If you prefer to maintain the current setup, you have two options:

  2. Dismiss the warning (perhaps there is a way to deactivate it for a single line), or

  3. Avoid re-throwing the error; instead, mimic the actions taken in the outer catch block (log(plugin has failed)) followed by continue. While duplicating this process is not optimal, especially for something more complex, consider segregating it into a function (potentially a local one) and invoke it from both instances.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to narrow down a selection of elements in d3js?

I am trying to implement a feature where all elements below the one that is being hovered over will translate downwards. Currently, I am able to filter the selection using .filter for elements greater than 10. Is there a way to dynamically use the id of th ...

Vue 3 select component with first character filtering functionality

Is there a way to filter options based on user input in Select2, especially when I need to get the first symbols entered by the user? I have tried using the @select event but it doesn't seem suitable for this task. How can I achieve this? <Select2 ...

How can you transform the outcome of a TYPO3 repository search into a JSON format?

Is it possible to convert the outcome of a "findAll()" function on a Repository into a JSON object, make changes to specific properties in JavaScript, and then send it back to the Action, converting it again for use by the Action to persist it in the datab ...

Run TypeScript with module import and definition using ts-node

I am attempting to initialize a database using TypeScript. Here is my TypeScript code: import { User, UserRole } from '../entity/User'; import crypto from 'crypto'; import {dbManager, pwhash } from '..'; async function initu ...

Navigating Angular's ng-grid: Utilizing JSON attributes structured with continually increasing integers

Currently, I am facing an issue with populating an ng-grid instance using JSON received from a rest API. In the past, I successfully tested this setup and it was working without any configuration problems. The previous JSON response had a top-level "users" ...

JavaScript error: "null or not an object" bug

I have implemented the JavaScript code below to display horizontal scrolling text on the banner of my website. Surprisingly, it works perfectly fine on one server but throws an error on another server with the message: Error: 'this.mqo' is nul ...

What is the best way to set the state to false upon unmounting a component in react?

Currently, I am controlling the state of a dialog using context. Initially, the isOpen state is set to false. When the add button is clicked, the state isOpen becomes true and clicking the cancel button will revert it back to false. If the user does not c ...

The use of asterisk (*) in importing dynamically

Hello everyone, I'm currently working on a project structure that looks like this: intranet ├── modules │ ├── storage │ │ ├── views │ │ └── route │ │ └── index.js │ │ │ ├── ...

Tips on declaring an object with a nested array of objects in TypeScript

In my code, I have defined two classes. One is called Stuff and the other is called Thing. class Stuff { constructor() { } things: Thing[] = []; name: string; } class Thing { constructor() { } active: boolean; } As I was working on my applicat ...

How can I display multiple lines of dynamic data using JavaScript in a Highcharts chart?

I have successfully implemented a chart in jsf that dynamically generates based on user input selection from a drop-down list. However, I am now facing the challenge of enabling multiple item selections at once to display multiple lines on the chart simu ...

Having difficulty displaying nested arrays in AngularJS

Understanding JSON Data in AngularJS As I delve into the world of AngularJS, I've encountered a minor roadblock when it comes to fetching JSON data. <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/boots ...

Modify components in a directive template depending on the information in the scope

In my project, I am facing an issue with a directive nested within an ng-repeat. The ng-repeat item is passed to the directive and I am trying to generate a directive template or templateUrl with dynamic elements based on a key/value in the item. Specifica ...

Saving integer data retrieved from DynamoDB in a React Native application

I need to store a user's progress by saving a value in a DynamoDB. While storing the value is not difficult, accessing this value for use in my application has proven to be quite challenging. When using dynamoDBWrapper.getItem(params), where the para ...

Disabling the Annoying Video Popup Ad that's Blocking my "Load More" Feature in Selenium

I am currently in the process of scraping around 1000 URLs using Selenium, and I am very close to getting it to work smoothly. Each URL contains a "load more" button that I keep clicking until a Stale Element exception is thrown, which I handle. Everything ...

Using JavaScript to convert an image URL to a File Object

Looking to obtain an image file from a URL entered into an input box, leading to the transformation of an image URL into a file object. To illustrate, when selecting a random image on Google Images, you can either copy the Image or its URL. In this scena ...

NodeJs: Transforming strings into UUID v4 efficiently

Suppose I have the following string: const input = '83e54feeeb444c7484b3c7a81b5ba2fd'; I want to transform it into a UUID v4 format as shown below: 83e54fee-eb44-4c74-84b3-c7a81b5ba2fd My current approach involves using a custom function: funct ...

ID is Enclosed in Quotation Marks by JSON.Stringify

My JSON file is being modified by using JSON.stringify and JSON.parse based on updates from an online database. While everything is functioning correctly, there is an issue where numbers are being converted to strings with quotes in the JSON file. For in ...

Guide to dynamically generating Angular watchers within a loop

I'm looking to dynamically create angular watches on one or more model attributes. I attempted the following approach: var fields = ['foo', 'bar']; for (var i=0, n=fields.length; i<n; i++) { $scope.$watch('vm.model.&ap ...

When attempting to retrieve the value of my select element, I encounter difficulty due to a hidden field that only becomes visible when a certain option is selected

I have a dropdown menu with different options, including one labeled "other". When the user selects "other", a hidden text field appears for them to enter a custom value. This functionality works correctly. However, if the user selects any other option and ...

The Android webview is blocking the display of a frame due to its X-Frame-Options being set to 'DENY'

Encountering an issue while attempting to display a Google calendar in a webview, an error is displayed: [INFO:CONSOLE(0)] "Refused to display 'https://accounts.google.com/ServiceLogin?service=cl&passive=1209600&continue=https://www.google.co ...