jruizgit / rules

Durable Rules Engine
MIT License
1.14k stars 207 forks source link

How can I handle durable.engine.MessageNotHandledException properly? #286

Open maifire opened 4 years ago

maifire commented 4 years ago
from durable.lang import *

def risk7_callback(c):
    print('risk7 fraud detected')

dynamic_ruleset = { 'risk7': {
    'suspect': {
        'run': risk7_callback,
        'all': [
            {'m': {'$gt': {'succ': 30}}}
        ],
    }
}}

get_host().set_rulesets(dynamic_ruleset)

post('risk7', {'succ': 31, 'fail': 11})
post('risk7', {'succ': 30, 'fail': 11})

Hi, I try your module for checking condition with dynamic rule load from json. I planned rule file & data from redis and insert it to rule engine and send alert to any other DBs or API. But when I try above code, at last line, I met below Exception. durable.engine.MessageNotHandledException: {'succ': 30, 'fail': 11}

I think this will be your intended work but don't know how can I handle that properly. When met last line I hope nothing happen or call some other callback function. Could you guide it?

jruizgit commented 4 years ago

Hi, thanks for asking the question. MessageNotHandledException is raised when the object posted doesn't satisfy any rules. In that case, the rules engine does not keep a copy of it. You can can handle the exception and ignore:

try:
    post('risk7', {'succ': 31, 'fail': 11})
    post('risk7', {'succ': 30, 'fail': 11})
except MessageNotHandledException as error:
    pass