Sending errors up the Worker call-chain

Purpose :

To demonstrate how a developer can send an error that occurs in a Worker's MHL or EHL to its Caller, or to any Worker above it within an application's Worker call-chain.

Introduction

It is quite common when developing asynchronous applications with Workers to want to send the errors that occur in one Worker up to another Worker in the Worker call-chain to handle the error. However, it is not always known which Worker in the Worker call-chain hierarchy will handle the errors of Workers below it. Maybe a Worker's Caller will handle the error of a subWorker, or it might be that the head Worker of an application is used to collect all the errors from all Workers below it. Depending on the design of your application and the level of coupling you want to have between your Workers, there are a few ways to handle this problem.

Method 1: Using a Public API Response

  • Advantages: Simple, decoupled from Caller, uses Worker Public API, does not require a base class.

  • Disadvantages: If sending the error multiple levels up the Worker call-chain, you will need to implement a similar Public API Response in every Worker between the Worker that sends the error and the Worker that processes it.

To create a Public API Response, you can use the Public API Builder tool to create a Public Response with its data input type as a standard LabVIEW error cluster. You can then use the Response VI to send an error to the Worker's Caller from anywhere within the Worker's MHL.

To learn how to create Worker Public API Responses, see the link below.

Method 2: Using the Workers Palette "Send Error to Owner" VI

  • Advantages: Simple, decoupled from Caller, does not require a base class. Requires no additional VIs.

  • Disadvantages: Not part of a Worker's Public API so it might not be obvious to other developers that you have used this method.

This VI can be found on the Workers Palette. The VI will pass input Error to Pass to Owner to the Worker's Caller. The Caller will then continue to pass the error up to its Caller, all the way up to the head Worker.

Pass Error to Owner.vi can be found on the Workers Palette


The error can be captured by any Worker in the application's Worker call-chain above the Worker that calls this VI, by manually adding the case <Pass Error to Owner> to a Worker's MHL. An example of how to implement this MHL case is shown below. The error will be caught by this case and no longer propagated up the Worker call-chain.

Implementation of MHL case <Pass Error to Owner> to catch an error sent to it from a subWorker.


Method 3: Using a recursive Public API Request in a base class

  • Advantages: a single base class Public API Request can be used to send an error from the bottom of a Worker's call-chain all the way up to the head Worker.

  • Disadvantages: all Workers between (and including) the Worker that sends the error and the Worker that processes it must inherit from the same base class.

By sending a recursive message to the MHL case of a Worker base class, you can easily propagate a message either up or down a Worker’s call-chain. In this example we will use it specifically to send an error up to the head Worker from a Worker lower in the Worker call-chain.

Let’s take an example of three Workers as shown below. First we have a Worker call-chain diagram, and next to it the Worker class inheritance hierarchy. All three Workers inherit from a base class called Worker Base Class.

  1. First, create a Public Request in the Worker base class. Let’s call it Handle Error. You can perform this operation with the Public API Builder tool.

  2. In the <Handle Error> case of the Worker base class (below), drop the Handle Error Request VI into the MHL case and wire to its data input the error wire that is received by the case. Then wire to its Worker input terminal the Caller's class data wire after you cast it to the the base class data type. Like so.

    Worker Base Class (MHL Cases.vi)
  3. Next, in the Worker that you want to catch the message (let’s say the Head Worker) you want to use the Public API Builder tool to override the Public API Request Handle Error. The tool will add the MHL case <Handle Error> into the head Worker's MHL. The result is the following:

    Head Worker (Main.vi)
  4. Now send the Public Request from a Worker lower in the Worker call-chain. For example, from the Second Level Worker. The message will then be propagated all the way up the Worker call-chain until a Worker decides to handle the error (i.e. the Worker that overrode the base class MHL case).

    Second Level Worker (Main.vi)

Source Code

The example source code for the three methods above can be found in the Workers Community Additional Example Projects repository here.

You can use https://download-directory.github.io/ to download individual folders directly from the GitHub repository.