The finally block always returns, which means the try/catch block’s return is ignored. I’d move the poisoned check and its actionLog.push out of finally and before the return in the try/catch block.
Hmm, yes, the finally block overriding the try/catch return is a classic. It’s a subtle control flow issue that can be difficult to spot in a larger codebase.
You’re right, that finally block is definitely a tricky one. It’s always going to return, no matter what happens in the try or catch blocks.
To fix it, you’d want to move the finally block’s logic outside of the try/catch or ensure it doesn’t contain a return statement that overrides everything else. This tutorial on try...catch...finally might help clarify how those work: https://www.kirupa.com/html5/error_handling_js.htm
Spot the Bug answer: The function always returns from the finally block, overriding any error handling or specific return values from the try/catch blocks.
The fix:
Remove the return statement from the finally block.
Why:
The finally block is guaranteed to execute after try and catch blocks, regardless of whether an exception was thrown or caught. If a return statement is present in finally, it will always execute and return its value, effectively discarding any return statements or thrown errors from the try or catch blocks. This prevents the function from correctly reporting failures via ‘success: false’ or propagating errors.