json-rules-engine icon indicating copy to clipboard operation
json-rules-engine copied to clipboard

Handle error thrown by fact

Open robross0606 opened this issue 3 years ago • 4 comments

I'm trying to figure out what the Engine does it a Fact function throws an exception. As far as I can tell, there is no event handler for this and it doesn't return the error as the Fact value or anything like that. It also doesn't seem to be caught by either of the existing success or failure events. Is this supported in some way or does the entire engine.run() bomb out?

robross0606 avatar Aug 11 '22 17:08 robross0606

I have just found that a fact throwing an error will kick back from engine.run():

try {
  await engine.run()
} catch (error) {
  engine.stop()
}

However, the remaining rule facts somehow continue to execute on the node.js process loop outside the await used in the try/catch block. Calling engine.stop() does not stop that execution thought it does set the status of that RULE to 'FINISHED'. Nevertheless, I've found via Jest spy that fact execution seems to continue even after.

Is there any way to prevent this short of never throwing an exception from a fact?

robross0606 avatar Aug 17 '22 22:08 robross0606

Turns out the engine keeps a factResultCache on the almanac that has all the promises on it. There are indeed some that are "pending" even after the engine is closed in an error situation. I was able to work around the problem with a special error type which includes the almanac on the thrown error:

      if (error.almanac) {
        // Resolve remaining fact cache to prevent continuing async execution
        await Promise.allSettled(error.almanac.factResultsCache.values())
      }

robross0606 avatar Aug 18 '22 00:08 robross0606

This is effectively a process/memory leak. Can this be included in Milestone 7?

robross0606 avatar Sep 14 '23 12:09 robross0606

Another potentially useful feature here would be to have an event handler for "error":

engine.on('error', (event, almanac, error) => {
  // TODO:  Handle errors thrown by facts
})

robross0606 avatar Sep 16 '23 15:09 robross0606