I just upgraded to Brython 3.2.3 from 3.2.0 alpha and encountered a problem. I found a workaround solution and would like to know if it is the best way to proceed and if it is likely to be future-proof.
[Note: I am working with Brython 3.2.3 only on a locally hosted private version of my site.]
try {
eval_python(src);
} catch (e) {
// console.log(e)
process(e)
}
where eval_python(src) can be thought of being
def eval_python(src):
exec(src, some_dict)
Up until now (Brython 3.2.0 and many earlier), "e" would be an object with various attributes including in particular __name__ and "$message". I also defined my own custom errors, with attributes, and could get them.
With Brython 3.2.3, I do not get this. If I log the error in the console, instead of a "nice" error object with the relevant attributes, I get a "native error" raised at line 6423 of brython.js with the following information available
Error
at Error (native)
at _b_.SyntaxError (<anonymous>:105:390)
at eval (<anonymous>:16:81)
...
Line 6423 of brython .js is var result=js_attr.apply(self.js,args)
So, no more "nice" javascript object to decipher...
=====
After much experimentation, I arrived at this workaround solution (again, simplified). I catch any error in eval_python as follows:
def eval_python(src):
window.__python_error = ""
try:
exec(src, some_dict)
except Exception as e:
window.__python_error = e
and I modified the way I run the code from Javascript as follows:
try {
eval_python(src);
if (window.__python_error) {
throw window.__python_error;
}
} catch (e) {
// console.log(e)
process(e)
}
====
Is there a better way to have errors be lifted from the Python context to the surrounding Javascript context and handling them?
André