irods / python-irodsclient

A Python API for iRODS
Other
62 stars 73 forks source link

Allow access to raw server message even when an iRODSException is thrown #606

Closed d-w-moore closed 3 months ago

d-w-moore commented 3 months ago

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:

  1. the API call runs with no fatal error (error code of 0) and returns normally, with return value possibly deriving from som information in the raw response message from the server (replica_truncate actually extracts only the returned JSON message in the form of a Python dict, from the server's returning message
  2. an error occurs that results in the API call returning a nonzero error code , indicating a fatal error occurred, in which case currently msg cannot 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 msgvalue 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 )
alanking commented 3 months ago

@d-w-moore - Please close if complete. Thanks