Propagating Clips Error Messages in PyClips
Question: I'm finding it very difficult to develop with PyClips, because it appears to
replace useful error messages thrown by Clips with a generic "syntax error"
message. This makes debugging very laborious and practically impossible on
large codebases when using PyClips.

Consider the following example. I wrote a very large expression, which
contained the multiplication operator, but I mistakenly forgot to add the
second argument. Instead of simply telling I was missing an argument, PyClips
told me there was a syntax error. What should have taken me 1 second to
correct, took me 5 minutes to correct as I hunted through my large expression,
looking for the mistake.

Here's a condensed version:

In Clips, with a useful error message:

    
    
    clips
    CLIPS> (defrule myrule "" (myfact 123) => (bind ?prob (* (min 1 2))))
    [ARGACCES4] Function * expected at least 2 argument(s)
    
    ERROR:
    (defrule MAIN::myrule ""
       (myfact 123)
       =>
       (bind ?prob (* (min 1 2))
    

And in PyClips, with an unuseful error message:

    
    
    python
    >>> import clips
    >>> clips.BuildRule('myrule','(myfact 123)','(bind ?prob (* (min 1 2)))','')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.6/dist-packages/clips/_clips_wrap.py", line 2839, in BuildRule
        _c.build(construct)
    _clips.ClipsError: C08: syntax error, or unable to parse expression
    

How can I get PyClips to give me the _real_ error thrown by Clips?


Answer: Catch the ClipsError, then read ErrorStream for the details. For example:

    
    
    engine = clips.Environment()
    engine.Reset()
    engine.Clear()
    try:
        engine.Load(os.path.abspath(rule_file))
    except clips.ClipsError:
        logging.error(clips.ErrorStream.Read())
    

