Handling Errors
If your UDx encounters an unrecoverable error, it should throw a UdfException
. The exception triggers a rollback of the function call. In Python, this function rollback occurs when the UDx raises an exception.
The following code shows how you can add error checking to your UDx. In this example, if one of the arguments is less than 100, then the Python UDx throws an error.
while(True): # Example of error checking best practices. product_id = block_reader.getInt(2) if product_id < 100: raise ValueError("Invalid Product ID")
When an exception is raised in your Python UDx, the UDx throws a UdfException
and generates an error message.
=> SELECT add2ints(prod_cost, sale_price, product_id) FROM bunch_of_numbers; ERROR 3399: Failure in UDx RPC call InvokeProcessBlock(): Error calling processBlock() in User Defined Object [add2ints] at [/scratch_a/release/svrtar11244/vbuild/vertica/OSS/UDxFence/PythonInterface.cpp:168], error code: 0, message: Error [/scratch_a/release/svrtar11244/vbuild/vertica/OSS/UDxFence/PythonInterface.cpp:385] function ['call_method'] (Python error type [<class 'ValueError'>]) Traceback (most recent call last): File "/home/dbadmin/py_db/v_py_db_node0001_catalog/Libraries/02fc4af0ace6f91eefa74baecf3ef76000a0000000004fc4/pylib_02fc4af0ace6f91eefa74baecf3ef76000a0000000004fc4.py", line 13, in processBlock raise ValueError("Invalid Product ID") ValueError: Invalid Product ID