Description
When a Napi::Error
is constructed, it acquires a handle to a JS error object via napi_get_and_clear_last_exception()
. Then the Napi::Error
instance is intended to be thrown and caught as a C++ exception. But if any stack frame between where it is thrown and where it is caught has set up a handle scope, then the error object handle gets released as the exception propagates up the stack.
One way to resolve this is whenever using handle scopes be sure to catch any Napi::Error
and escape it from the scope before re-throwing it. But this makes it difficult to use handle scopes correctly and therefore would result in bugs that only occur in exception conditions. So, not a good solution.
Instead, the Error
class can be changed to acquire a persistent reference to the error object, instead of just a handle. I'm working on this.