ReturnValues¶
DIRAC return dictionary
Message values are converted to string
keys are converted to string
- exception DIRAC.Core.Utilities.ReturnValues.SErrorException(result)¶
Bases:
Exception
Exception class for use with convertToReturnValue
- __init__(result)¶
Create a new exception return value
If result is a S_ERROR return it directly else convert it to an appropriate value using S_ERROR(result).
- Parameters
result – The error to propagate
- args¶
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- DIRAC.Core.Utilities.ReturnValues.S_ERROR(*args, **kwargs)¶
return value on error condition
Arguments are either Errno and ErrorMessage or just ErrorMessage fro backward compatibility
- DIRAC.Core.Utilities.ReturnValues.S_OK(value=None)¶
return value on success
- Parameters
value – value of the ‘Value’
- Returns
dictionary { ‘OK’ : True, ‘Value’ : value }
- DIRAC.Core.Utilities.ReturnValues.convertToReturnValue(func)¶
Decorate a function to convert return values to S_OK/S_ERROR
If func returns, wrap the return value in S_OK. If func raises
SErrorException
, return the associated S_ERROR If func raises any other exception type, convert it to an S_ERROR object- Parameters
result – The bare result of a function call
- Returns
S_OK/S_ERROR
- DIRAC.Core.Utilities.ReturnValues.isReturnStructure(unk)¶
Check if value is an S_OK/S_ERROR object
- DIRAC.Core.Utilities.ReturnValues.isSError(value)¶
Check if value is an S_ERROR object
- DIRAC.Core.Utilities.ReturnValues.reprReturnErrorStructure(struct, full=False)¶
- DIRAC.Core.Utilities.ReturnValues.returnSingleResult(dictRes)¶
Transform the S_OK{Successful/Failed} dictionary convention into an S_OK/S_ERROR return. To be used when a single returned entity is expected from a generally bulk call.
- Parameters
dictRes – S_ERROR or S_OK( “Failed” : {}, “Successful” : {})
- Returns
S_ERROR or S_OK(value)
The following rules are applied:
if dictRes is an S_ERROR: returns it as is
we start by looking at the Failed directory
if there are several items in a dictionary, we return the first one
if both dictionaries are empty, we return S_ERROR
For an item in Failed, we return S_ERROR
Far an item in Successful we return S_OK
Behavior examples (would be perfect unit test :-) ):
{'Message': 'Kaput', 'OK': False} -> {'Message': 'Kaput', 'OK': False} {'OK': True, 'Value': {'Successful': {}, 'Failed': {'a': 1}}} -> {'Message': '1', 'OK': False} {'OK': True, 'Value': {'Successful': {'b': 2}, 'Failed': {}}} -> {'OK': True, 'Value': 2} {'OK': True, 'Value': {'Successful': {'b': 2}, 'Failed': {'a': 1}}} -> {'Message': '1', 'OK': False} {'OK': True, 'Value': {'Successful': {'b': 2}, 'Failed': {'a': 1, 'c': 3}}} -> {'Message': '1', 'OK': False} {'OK': True, 'Value': {'Successful': {'b': 2, 'd': 4}, 'Failed': {}}} -> {'OK': True, 'Value': 2} {'OK': True, 'Value': {'Successful': {}, 'Failed': {}}} -> {'Message': 'returnSingleResult: Failed and Successful dictionaries are empty', 'OK': False}
- DIRAC.Core.Utilities.ReturnValues.returnValueOrRaise(result)¶
Unwrap an S_OK/S_ERROR response into a value or Exception
This method assists with using exceptions in DIRAC code by raising
SErrorException
if result is an error. This can then by propagated automatically as an S_ERROR by wrapping public facing functions with @convertToReturnValue.- Parameters
result – Result of a DIRAC function which returns S_OK/S_ERROR
- Returns
The value associated with the S_OK object
- Raises
If result[“OK”] is falsey the original exception is re-raised. If no exception is known an
SErrorException
is raised.