Inline Try-Catch for Azure Data Factory activity output

At the moment Azure Data Factory expressions and functions are limited and there is not try-catch if you need to make the code to behave differently in case an activity’s output was success or fail. Here is a workaround:

Suppose you have a lookup and you need to set the variable based on the output of lookup being success or failure. Please note below the activities are connected through “completion”. Meaning the second activity will run in both cases of first activity’s success or fail.

The trick is all activities generate an “output” attribute upon completion. This is an example of a success output:

{“firstRow”:{“”:1},”effectiveIntegrationRuntime”:”resourcename”,”billingReference”:{“activityType”:”PipelineActivity”,”billableDuration”:[{“meterType”:”SelfhostedIR”,”duration”:0.016666666666666666,”unit”:”Hours”}]},”durationInQueue”:{“integrationRuntimeQueue”:1}}

And below is an example of a failed activity:

{“durationInQueue”:{“integrationRuntimeQueue”:5}}

Now in “set variable” activity we can run below, we can write below to determine the success/failure of first activity

@if(greater(length(string(activity(‘Lookup1’).output)),55), ‘Success’, ‘Failure’)

Having done that, in case of failure you can get the errorcode from below

@{activity(‘Lookup1’).error.message}