2

The python module that am using has list of error codes like the following: -

DRV_ERROR_CODES = 20001
DRV_SUCCESS = 20002
DRV_VXDNOTINSTALLED = 20003
DRV_ERROR_SCAN = 20004
DRV_ERROR_CHECK_SUM = 20005
DRV_ERROR_FILELOAD = 20006
DRV_UNKNOWN_FUNCTION = 20007
...

I currently just compare the return value against the success error code in order check if the process has been successful.

if atmcd.DRV_SUCCESS==ret:

I would like to use the return value and output what exactly caused the error (e.g. the return value was 2004, therefore the script will print that there was a scan error). Is there a a way to compare against all these variables? Is there a better way of finding the error?

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Marmstrong
  • 1,686
  • 7
  • 30
  • 54

4 Answers4

6

You can collect all error codes in a mapping:

errornames = {value: name for name, value in vars(atmcd).items() if name.isupper() and name.startswith('DRV_')}

This produces a mapping from error code to name used in the atmcd module:

{'20007': 'DRV_UNKNOWN_FUNCTION', '20006': 'DRV_ERROR_FILELOAD', '20005': 'DRV_ERROR_CHECK_SUM', '20004': 'DRV_ERROR_SCAN', '20003': 'DRV_VXDNOTINSTALLED', '20002': 'DRV_SUCCESS', '20001': 'DRV_ERROR_CODES'}

Now you can both test for the error code and get the name for each:

if ret in errornames:
    print 'Received a {} code'.format(errornames[ret])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Missing an `.items()`, I think. Fix that and since you beat me I can delete mine. – DSM Dec 18 '13 at 16:43
  • @DSM: I could have sworn I had that in there.. Thanks for the pointer. – Martijn Pieters Dec 18 '13 at 16:44
  • I new about dictionaries but that first line of code is amazing. Its exactly what I need. I have learned a lot of different things from this answer that will help me elsewhere. Thanks – Marmstrong Dec 18 '13 at 17:18
1

This is an arcane way of programming. Modern programming languages use Exceptions to inform of errors. If I were you I would group all the variables in a dict this way: {error_code:'error_name'} and write a wrapper for the method that do this:

ret = method_call()
if ret != atmcd.DRV_SUCCESS:
    raise Exception('Error number %d: %s' % (error_code, error_name))

Maybe a good way doing this is with a decorator to the actual method you're calling.

Hope this helps!

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
0

You can use a dictionary which maps the error code with the string to be printed

errors = { DRV_ERROR_CODES : 'Explanation for this error code',
           DRV_SUCCESS : 'Explanation for this error code',
           DRV_VXDNOTINSTALLED : 'Explanation for this error code',
           DRV_ERROR_SCAN : 'Explanation for this error code',
           DRV_ERROR_CHECK_SUM : 'Explanation for this error code',
           DRV_ERROR_FILELOAD : 'Explanation for this error code',
           DRV_UNKNOWN_FUNCTION : 'Explanation for this error code'}

if ret in errors.keys():
    print errors[ret]
svituz
  • 69
  • 5
0
def error_track(code):

    return {
            20001: "ERROR",
            20002: "SUCCESS",
            20003: "FILE LOAD ERROR",
            20004: "ERROR CHECK SUm"
            }.get(code, "UNKNOWN ERROR")


print error_track(20001)
print error_track(50000)

OUTPUT

ERROR
UNKNOWN ERROR
Siva Cn
  • 929
  • 4
  • 10