Best practices: Error handling
The Workflows platform can display any errors thrown by an API to a connector's user.
The If Error (Try/Catch) function can be limited to special circumstances, like handling messages in a connector's _authping flow. However, there are some best practices for error handling when designing cards for a connector.
Required input handling
When you're creating inputs for a connector, the Inputs dialog allows you to mark fields as being required. Setting a field as required means that users of a connector card must either enter a value into that field or drag in an output from another card.
Because users can drag in output fields to satisfy the card's front-end validation, there's no guarantee that the output field contains an actual value when the flow executes. As such, it's good practice for connector developers to verify that required values exist before calling an API. This prevents the platform from sending known bad requests to the API, and also protects the user's API limits.
Validate the required fields
The simplest way to validate required fields is to use the Return Error If card along with an error message of the following form: No <Input Name> input provided. Enter a valid <Input Name>.
Be sure to include a 400 status code with the message. A 400 Bad Request response status code indicates that the server can't process the request due to something that it perceives as a client error. For example, malformed request syntax, invalid request message framing, or deceptive request routing.
Example
-
Add a Return Error If function card after your Connector Action (event) card.
-
Drag the required input into the value a field of the Return Error If card, and set the comparison to is empty.
-
In the message field, type No <Input Name> input provided. Enter a valid <Input Name>. where <Input Name> is the display name of the field.
-
Enter 400 into the statusCode field.
-
Repeat for each field you want to handle and return an error message.