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:


*** Settings *** Documentation Inhuman Insurance, Inc. Artificial Intelligence System robot. ... Consumes traffic data work items. Resource shared.robot *** Tasks *** Consume traffic data work items For Each Input Work Item Process traffic data *** Keywords *** Process traffic data ${payload}= Get Work Item Payload ${traffic_data}= Set Variable ${payload}[${WORK_ITEM_NAME}] ${valid}= Validate traffic data ${traffic_data} IF ${valid} Post traffic data to sales system ${traffic_data} Validate traffic data [Arguments] ${traffic_data} ${country}= Get Value From Json ${traffic_data} $.country ${valid}= Evaluate len("${country}") == 3 RETURN ${valid} Post traffic data to sales system [Arguments] ${traffic_data} ${status} ${return}= Run Keyword And Ignore Error ... POST ... https://robocorp.com/inhuman-insurance-inc/sales-system-api ... json=${traffic_data} Handle traffic API response ${status} ${return} ${traffic_data} Handle traffic API response [Arguments] ${status} ${return} ${traffic_data} IF "${status}" == "PASS" Handle traffic API OK response ELSE Handle traffic API error response ${return} ${traffic_data} END Handle traffic API OK response Release Input Work Item DONE Handle traffic API error response [Arguments] ${return} ${traffic_data} Log ... Traffic data posting failed: ${traffic_data} ${return} ... ERROR Release Input Work Item ... state=FAILED ... exception_type=APPLICATION ... code=TRAFFIC_DATA_POST_FAILED ... message=${return}
  • You added the returned value (the error message) and the traffic data that the robot tried to post as arguments to the response handler keyword (Handle traffic API response).
  • Another branch was added to the IF condition to handle statuses other than PASS (essentially, FAIL).
  • The Handle traffic API error response keyword takes the return value and the traffic data as arguments. The exception is logged. In addition, 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.

At the top of the log, you see timestamp links and the logged error messages. Clicking on a timestamp opens the corresponding details in the log.

[ ERROR ] Traffic data posting failed: {'country': 'SWE', 'year': 2018, 'rate': 3.61718} HTTPError: 500 Server Error: Internal Server Error for url: https://robocorp.com/inhuman-insurance-inc/sales-system-api

Excellent! Now the robot knows how to handle both successful and failed API requests.