Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

Handle business exceptions (for example, invalid data)

One more scenario to handle: what to do with invalid data?:

consumer.robot:

*** 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} ELSE Handle invalid traffic data ${traffic_data} END 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} Handle invalid traffic data [Arguments] ${traffic_data} ${message}= Set Variable Invalid traffic data: ${traffic_data} Log ${message} WARN Release Input Work Item ... state=FAILED ... exception_type=BUSINESS ... code=INVALID_TRAFFIC_DATA ... message=${message}
  • The Handle invalid traffic data keyword takes care of dealing with invalid traffic data. It logs a warning, including the invalid data. The work item is released as FAILED. This time the exception type is BUSINESS. The code can be any custom code. You choose to use INVALID_TRAFFIC_DATA. The exception type and the code help filter the failed work items in Control Room.
  • You add a new branch to the Process traffic data keyword for handling the invalid data case.

After running the consumer task, you see the following log entry regarding the invalid data (due to the country code not matching the validation rule):

[ WARN ] Invalid traffic data: {'country': 'SWError', 'year': 2019, 'rate': 3.13947}

Your consumer robot is now complete!

The consumer:

  • loops all the work items one by one.
  • validates the data.
  • posts the data to the sales system API.
  • handles successful responses.
  • handles application exceptions.
  • handles business exceptions.

All done. The producer produces, and the consumer consumes. Bravissimo! 👏