Error Handling
Since your modules will receive a FlowMessage and this is a dynamic object you can´t be sure that the person building the flow will send in the data your module expects.
So, your modules should be very fault tolerant and swallow any invalid/unexpected data they might get.
If you want to throw an exception inside a module you can use the FlowModuleException
Tip
To alert users building flows about invalid data you should use SetStatus.
Output Modules Need Special Care
Since output modules represent some kind of communication with the outside world things can go wrong. This means that you need to think about always sending a result out from your output modules.
Example
Let´s say that you create a module that sends a message to one of the services on Azure. At some point the service might be down or your device might have a failing internet connection. To overcome this possible issue you need to make sure that you always pass a message to the next module and that this message contains the result of the modules operation.
protected override async Task MessageReceived(IFlowMessage msg)
{
try
{
// logic here...
// If the operation was a success
msg.SetSuccess();
// If the operation failed
msg.SetError("My awesome module failed to communicate with Azure :(");
}
catch(Exception ex)
{
msg.SetError(ex.Message);
}
finally
{
await this.Next(msg);
}
}
By using the logic above your module will always output the result.
The methods SetSuccess
and SetError
takes care of writing a crosser
object to the message. For example, if you call msg.SetSuccess()
your msg will look like:
{
'crosser':
{
'success':true
},
// rest of message
}
If you call msg.SetError("My awesome module failed to communicate with Azure :(")
your msg will look like:
{
'crosser':
{
'success':false,
'message': 'My awesome module failed to communicate with Azure :('
},
// rest of message
}