hmenyus/node-calls-python

how to handle python exceptions ?

valerino opened this issue · 3 comments

hi! just stumbled into your proj and it perfectly suit my needs.
just a question: is there a way to trap an exception caused by python ?

i.e.

i have this code i call:

def pythonfunction(a):
    if a:
       return True
    raise Exception('error!!!!')

from node-calls-python, if i do like:

try {
  res = py.callSync(mypythonmodule, 'pythonfunction', False)
}
catch (ex) {
  console.log(ex)
}

i don't get into the catch branch, it just return res=undefined.

if i call it with await it "somewhat" work, but i still not get the exception but a generic message.

this is a real world example with my real code, the python code throws an HttpError:

async function createKeypair(license_mgr_url, bits = 384, proxy = null, server_cert_pem = null, client_cert_pem = null, client_key_pem = null, client_key_password = null) {
    let res = null
    try {
        res = await py.call(licmgr_client, 'create_keypair', license_mgr_url, 13, proxy, server_cert_pem, [client_cert_pem, client_key_pem], client_key_password)
        console.log(res)
    }
    catch (ex) {
        console.log(ex) <===== 'Cannot call function', i'd like to have the exception.
        process.exit(1)
    }
    console.log('ok')
    process.exit(1)

result:

➜ ~/work/liblicmgr_nodejs (nodecallspython) ✗ node test
[.] TESTING: testCreateKeypair
Traceback (most recent call last):
  File "/home/valerino/work/test/liblicmgr_python/liblicmgr_client/licmgr_client.py", line 639, in create_keypair
    response = _post(license_mgr_url, 'create_keypair',
  File "/home/valerino/work/test/liblicmgr_python/liblicmgr_client/licmgr_client.py", line 111, in _post
    response = libstk.requests.post_json(
  File "/home/valerino/work/test/libstk/libstk/requests.py", line 142, in post_json
    raise HTTPError(url, response.status_code,
requests.exceptions.HTTPError: [Errno https://localhost:8082/create_keypair] 400: 'POST error, status_code=400, content=b\'{"status":"error","req_id":1646470817712752121,"time_msec":1646470817762,"message":"invalid bits length (13)","code":1}\''
Cannot call function <===== this is what i get with console.log(ex)

Exceptions were not handled at all. It is now improved in 1.6.3.

We now get a readable exception message, yes, but it seems the trace is still omitted. Any way to get that without manually wrapping the python code I'm calling into try-except?