jbaiter / chdkptp.py

Python bindings for chdkptp
GNU General Public License v3.0
13 stars 14 forks source link

parse ptp errors gracefully #9

Open meelash opened 6 years ago

meelash commented 6 years ago

First, chdkptp errors do not have a 'message' key, instead, it is 'msg'. Then, for some reason the dictionary get() method throws an error:

TypeError: 'NoneType' object is not callable

no matter if the key exists, or not. Not sure why that is so.

For example, using str(parse_table(err_table)), the structure of err_table is as follows:

{'msg': 'I/O error', 'etype': 'ptp', 'traceback': '\nstack traceback:\n\t[C]: in function \'chdk_connection.execlua\'\n\t[C]: in function \'pcall\'\n\t...on2.7/site-packages/chdkptp/vendor/chdkptp/lua/chdku.lua:2084: in function \'execlua_pcall\'\n\t...on2.7/site-packages/chdkptp/vendor/chdkptp/lua/chdku.lua:1016: in function <...on2.7/site-packages/chdkptp/vendor/chdkptp/lua/chdku.lua:966>\n\t(...tail calls...)\n\t[string ""]:4: in function <[string ""]:1>\n\t[C]: in function \'pcall\'\n\t[string ""]:1: in main chunk', 'ptp_rc': 767}

then the following error is thrown when using get() (no matter which key, just for example here is ptp_rc):

File "/Users/blah/Books/software/pi-scan/env/lib/python2.7/site-packages/chdkptp/lua.py", line 15, in init errcode = err_table.get('ptp_rc') TypeError: 'NoneType' object is not callable

but using err_table['ptp_rc'], it will correctly get that key.

Not sure why that happens, but the simple fix is to use brackets. Yes it will throw if the key doesn't exists, but currently it is throwing in all cases, so at the very least, it is better than the current situation ;)

meelash commented 6 years ago

The second commit is to expose the PTPError class so client libraries can catch it and parse it correctly, for example, by doing something with the PTP traceback property it has.