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.