Error Handling and Show Error Message from API in Canvas App

This post will explain how you can handle errors from a connector (or custom connector) and show a proper error message from the response of the API.


Whenever we use the Patch() function to create/update a row in Microsoft Dataverse, call an action of a (custom) connector or call a Power Automate cloud flow, there may be an error due to the server-side validation or an exception being thrown by the underlying API. In such a scenario, you can use the IfError function to handle the error and show a proper message to the user.

Example 1: A new row is created in the Test table in the Microsoft Dataverse with the Patch() function as in the formula below.
Patch(
    Tests,
    Defaults(Tests),
    {Text: "Power Apps Test"}
) 
But there is an error from Microsoft Dataverse and the patch operation is failed with the following generic error message. Instead of having such a generic error message from the platform, we need to show a more meaningful error message (which describes more detailed information) to the user.


Example 2: Search action is called from the custom connector with the following formula.
Set(
    varSearchResults,
    CustomConnector.Search(
        {SearchText: "Power Apps"}
    )
)
But there is an error responded by the API that the custom connector is connecting to. If there is any error from the API, we need to show the detailed error message from the API and clear the search result from the previous search (if there is any).


To handle those scenarios, Microsoft announced Error Handling as an experimental features in March 2018. You can read more details in this Error Power Fx Formula reference article.

🛈 Note

IfError function is part of an experimental feature and are subject to change. That function is available only when the Formula-level error management experimental feature in Settings > Upcoming features > Experimental is turned on. (Yes, you read it right. It is still an *experimental* feature after more than 4 years and counting.)



For Example 1 (error in Patch function), we can use the formula below with IfError function to show a custom error message if there is an error in the Patch() function. The IfError function tests the formula in the first parameter. If there is an error, it returns the formula in the second parameter. In our case, there is nothing to return so the Notify() function is used to show a custom error message with NotificationType.Error for 5000 milliseconds (5 seconds).
IfError(
    Patch(
        Tests,
        Defaults(Tests),
        {Text: "Power Apps Test"}
    ),
    Notify(
        "There is an error while creating a row in Test table.",
        NotificationType.Error,
        5000
    ),
    false
);
The following error message will be shown when there is an error in the Patch() function.



Instead of just catching and showing the custom error, you can use the FirstError to get the error details from the API. (Use AllErrors table if there is more than one error. Read this Microsoft Documentation for more details). You can check out the very useful video by Phil Topness on Advanced Connector Error Handling for Canvas Apps which is part of the Power CAT Live series. The video was recorded more than a year ago, so the ErrorInfo used in the video has been changed and FirstError needs to be used instead (That explains why this is still an experimental feature). In the formula below, the response from the Dataverse connector (FirstError.Details.HttpResponse) is used to show as an error message instead of the custom text.
IfError(
    Patch(
        Tests,
        Defaults(Tests),
        {Text: "Power Apps Test"}
    ),
    Notify(
        FirstError.Details.HttpResponse,
        NotificationType.Error,
        5000
    ),
    false
);
The error message from the API is returned and we can now see that the error message is from the real-time workflow. However, there is a lot of other information from the HttpResponse and we need to extract the message part of the response JSON.



Since there is no parse JSON in Power Fx, we can use this method mentioned in the blog post by Inogic to parse JSON from a string using the Match function.
IfError(
    Patch(
        Tests,
        Defaults(Tests),
        {Text: "Power Apps Test"}
    ),
    Notify(
        Match(
            FirstError.Details.HttpResponse,
            "\""code"":""(?<code>[^""]*)"",""message"":""(?<message>[^""]*)"
        ).message,
        NotificationType.Error,
        5000
    ),
    false
);
Now, we can show the error message from the real-time workflow without any other error details.



Other than showing an error notification, there is also another way to handle the error if the data from the API is bound to a gallery or a data table. If the search data from Example 2 is bound to the gallery and there is an error from the custom connector, you might want to clear the search result from the previous search and show a proper message.
Set(varSearchResults, Blank());
Set(varMessage, "Loading...");
Set(
    varSearchResults,
    IfError(
        CustomConnector.Search(
            {SearchText: "Power Apps"}
        ),
        Set(
            varMessage,
            Match(
                FirstError.Details.HttpResponse,
                "\""code"":""(?<code>[^""]*)"",""message"":""(?<message>[^""]*)"
            ).message
        ),
        If (
            (CountRows(varSearchResults) > 0),
Set(varMessage, ""), Set(varMessage, "No data available"}); ); ); );
In this example, the gallery is bound to the varSearchResults and varMessage is set to the value of a label in the middle of the gallery. The formula above is triggered OnSelect of the Search button. Before the search, clear the previous search results and show the "Loading..." message to the user by setting the varMessage. If there is any error from the custom connector, the error message will be displayed on the label. If there is no error but there is no data returned from the search API, the label will show a "No data available" message.


Summary

Unlike traditional programming languages, error handling in Power Fx is a bit different and such kinds of errors from API can only be handled by enabling the formula-level error management experimental feature.

Comments

Popular Posts