CacheControl / json-rules-engine

A rules engine expressed in JSON
ISC License
2.54k stars 455 forks source link

Handle error thrown by fact #311

Open robross0606 opened 1 year ago

robross0606 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 10 months ago

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

robross0606 commented 10 months ago

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
})