Handle a failed sales system API response (exception)
Now that the successful work items have been handled, it's time to do something about the failing requests.
In case of an application exception, you release the work item as failed and add information about the failure:
- You added an extra return value to
post_traffic_data_to_sales_system(). When you are interested in more than just one information, python allows multiple returns to make your life easier. In our case, we need both the status and the response json to see what happened after our call.
- You added an
elsestatement to handle the application failure case, when the status is different from 200.
item.fail()is called, an exception is logged and the work item is released as
FAILED. More information is added by providing the exception type (
APPLICATION- indicating a technical exception), a custom code (for exception filtering purposes in Control Room), and a message (to be displayed in Control Room).
You run the consumer task. This time, the robot does not give up when an exception occurs. It logs the error, releases the work item as failed, and continues with the rest of the work items.
Excellent! Now the robot knows how to handle both successful and failed API requests.