Allow access to raw server message even when an iRODSException is thrown
This kind of approach is sometimes used as an error guard on an iRODS api call and its possible consequence, the raising of an iRODSException due to a fatal error of some sort during the server operation :
import irods.exception as ex
try:
value = ses.data_objects.replica_truncate(path, length)
except ex.iRODSException as e:
logging.getLogger().info(returned server code = %d',e.code)
print ('Exception during replica_truncate: {!r}'.format(e))
#raise # (... only if we want to abort)
Two possible cases arise:
- the API call runs with no fatal error (error code of 0) and returns normally, with return
valuepossibly deriving from som information in the raw response message from the server (replica_truncateactually extracts only the returned JSON message in the form of a Python dict, from the server's returning message - an error occurs that results in the API call returning a nonzero error code , indicating a fatal error occurred, in which case currently
msgcannot be returned.
However it turns out the designer of the server API in question (true for replica_truncate in particular) may wish to convey information and/or some error message, for example a JSON string contained in the msg structure, which then should be accessible regardless of the exception being thrown.
We seek some way in which this can be done. In the absence of a better plan, we will attach the msg value which would have been returned. Thus we could intercept the returning JSON choose whether to convert it to a data structure for further utilitiy, or simply do this:
try:
msg = ses.data_objects.replica_truncate(path, length)
except ex.iRODSException as e:
json_return_str = e.server_msg.get_main_message( STR_PI).myStr
logging.getLogger(__name__).info("replica_truncate: code=%d returned JSON message:%s", e.code, json_return_str )