Mastering Error Handling in Power Fx: Best Practices and Tips for Smooth Functioning

Mastering Error Handling in Power FX: Best Practices and Tips for Smooth Functioning

Introduction

Power FX is a low-code programming language that is used to build custom business solutions on the Microsoft Power Platform. It offers a range of functions and formulas that help users create powerful apps without requiring deep technical knowledge. However, as with any programming language, errors can occur during app development, which can cause problems and disrupt the user experience.

To address this issue, Microsoft has introduced a new preview feature in Power FX called “Formula-level error management”. This feature allows users to manage errors at the formula level, making it easier to identify and fix issues that may arise in their apps. With formula-level error management, users can create custom error messages, handle exceptions, and debug their formulas to ensure that their apps function smoothly and efficiently.

In this blog post, we will dive into the details of Power FX’s Formula-level error management preview feature, exploring how it works, what benefits it offers, and how you can use it to optimize your app development process. We will also provide some best practices and tips for using this feature effectively and avoiding common errors in your Power FX apps. So, if you’re looking to take your Power Platform development skills to the next level, read on to learn more about this exciting new feature!

Estimated reading time: 12 minutes

Table of contents

Turn on Formula-level Error Management

It’s important to note that the behavior described in this blog post is only available when the Formula-level error management preview feature is turned on in Power FX. To enable this feature, you need to navigate to Settings -> Upcoming features -> Preview in your Power Apps environment, and then turn on the Formula-level error management toggle.

Once enabled, you’ll be able to access the new error management features in the formula bar, such as the ability to create custom error messages and handle exceptions. These tools can help you streamline your app development process and create more robust and efficient solutions on the Power Platform.

For more information about controlling which features are enabled see this link.

How to turn on formula-lelel error management

Write a Custom Error Message

In addition to the Formula-level error management preview feature, Power Apps also offers another way to create custom error messages using the OnError property. This property allows you to define a custom error message that is displayed to users when an error occurs in your app.

To use the OnError property, you need to add it to the app or control that you want to apply it to. Then, you can specify the error message that you want to display by writing a formula in the OnError property box. For example, you could write a formula that displays a message like “Sorry, an error occurred. Please try again later” when a user encounters an error… 🙂

Using the OnError property can help you create a more user-friendly app experience by providing clear and concise error messages that guide users through the app. It can also help you identify and address errors more quickly, as you’ll have more detailed information about the nature of the error.

Overall, custom error messages with the Power Apps OnError property are a useful tool for developers who want to create more intuitive and efficient apps on the Power Platform.

Custom Error Message With Power Apps OnError Property
Notify(
    Concatenate(
        FirstError.Message,
        ", Observed: ",
        FirstError.Observed,
        ", Source: ",
        FirstError.Source
    ),
    NotificationType.Error
);

Now when we enter the value on TextInput control a more helpful error message is shown.

How to Send Error Details To Power Apps Monitor

Power Apps monitor is a tool that developers can use to monitor and debug their apps in real-time. It provides a detailed view of all the activity in a user’s session, including user interactions, data changes, and errors.

However, the default error messages logged by Power Apps monitor are often not detailed enough to diagnose and fix problems quickly. To address this issue, developers can use the Trace function to create custom logging that includes all the information needed to diagnose and fix a problem.

The Trace function allows developers to add custom logging statements to their apps, which can include information such as variable values, function inputs and outputs, and other important context that can help identify the cause of an error. These custom logs can be viewed in Power Apps monitor alongside the default logs, providing a complete picture of the app’s behavior.

By using Power Apps monitor and the Trace function together, developers can gain deep insights into their apps’ behavior and quickly diagnose and fix errors as they arise. This can help ensure that apps are running smoothly and efficiently, providing a great user experience for all.

Add the Trace function code to the OnError property of the app.

Notify(
    Concatenate(
        FirstError.Message,
        ", Observed: ",
        FirstError.Observed,
        ", Source: ",
        FirstError.Source
    ),
    NotificationType.Error
);
Trace(
    "Unhandled Error",
    TraceSeverity.Error,
    {
        Kind: FirstError.Kind,
        Message: FirstError.Message,
        Observed: FirstError.Observed,
        Screen: App.ActiveScreen.Name,
        Source: FirstError.Source,
        User: User().Email
    }
)

In order to check the result, open Power Apps Monitor from the solution’s context menu.

Then click on Play published app Command.

To generate an error message, you can follow these steps:

  1. Navigate to the text input control where you want to generate the error message.
  2. Input a text value into a text input control.
  3. Press the enter key to trigger the error handling.
  4. Power Apps will recognize that “B” cannot be converted to a number and display an error message that says: “Our goal is generating an error message. Now we have the ability to do that. Write the letters “B” in the text input and click the enter. When we do this Power Apps will display an error message that says “The value ‘B’ cannot be converted to a number…”.

Upon examining the error in detail, we can observe additional information added by the trace. This additional information can provide more context to the error, such as the specific function or module where the error occurred, the line number, and the values of any relevant variables at the time of the error. By analyzing the trace information, we can gain a better understanding of the root cause of the error and potentially identify a solution.

How to Track Unhandled Power Apps Errors In Application Insights

Power Apps allows you to track unhandled errors in Application Insights, a tool that can provide insights and analytics on the performance and usage of your application. By implementing proper error handling in Power FX and enabling this feature, you can collect detailed information on any unhandled errors that occur in your Power Apps environment, including the error message, stack trace, and relevant session and user information.

To set up error handling in Power FX and track unhandled errors in Application Insights, you will need to configure the Power Apps Trace function to capture errors and send them to Application Insights. Once you have set up Application Insights for your Power Apps environment and configured the Trace function, you can access the error data in Application Insights and use it to diagnose and troubleshoot issues in your application.

By leveraging the capabilities of Application Insights and implementing proper error handling in Power FX, you can improve the quality and reliability of your Power Apps applications, ensuring that they provide a seamless and error-free user experience.

How to Create an Application Insights resource in Azure?

In order to create an Application Insight Resource, go to portal.azure.com and open Application Insights service. Then create a new application.

Choose a resource group and enter your application name. Click on Review + Create button.

Are you looking to monitor and analyze the telemetry data generated by your Power Apps application using Application Insights? If you have already created a new app in Application Insights, linking it to your Power Apps application is a quick and easy process.

  1. Go to the Azure portal and navigate to your Application Insights resource.
  2. Click on the Overview tab and copy the Instrumentation Key.
  1. Open your Power Apps application in Power Apps Studio.
  2. Then go back to Power Apps, select the app object, open the right-menu and paste the Instrumentation Key into the field shown below.

It is important to note that error handling in Power FX plays a crucial role in linking your Power Apps application to Application Insights. Linking your app to Application Insights involves more than just copying and pasting the Instrumentation Key. To ensure that your app is properly linked to Application Insights and to handle any potential errors that may arise during the linking process, you must implement proper error handling in your Power FX code. Additionally, it is important to save and publish the app after making the necessary changes to the app settings to ensure that the changes are properly applied and the app is linked to Application Insights.

Generating a Report of Power Apps Errors in Application Insights

Once you have set up the Power Apps Trace function to capture errors and sent them to Application Insights, you can easily view the captured errors in the Application Insights portal. Simply select “Logs” from the left-menu of the Application Insights portal. From there, you can use the query editor to filter the logs based on specific criteria, such as error type, severity, and time frame. You can also create custom charts and dashboards to visualize the error data and gain insights into how your Power Apps application is performing. By regularly reviewing the error logs and analyzing the error data, you can identify and address any issues that may be affecting the performance and reliability of your Power Apps application.

error handling in power fx

To retrieve all traces captured by the Power Apps Trace function in Application Insights, you can use the following query in the Logs tab of the Application Insights portal:

traces
| where message =="Unhandled Error"

This query filters the traces where the message is “Unhandled Error”. You can also add additional filters to the query to narrow down the results based on other criteria, such as time range, severity level, and error message. Once you have retrieved the traces, you can analyze the data to identify any patterns or trends and use that information to improve the performance and reliability of your Power Apps application.

Copy and paste this code into the query editor and click the Run button.

Once you have retrieved the traces using the query in Application Insights, all of the error messages will be displayed in the “results” field. From there, you can drill down into each error message to get more detailed reporting, including the error message, stack trace, and relevant session and user information.

One of the great features of Application Insights is that you can easily export the error data to a CSV file or open it in an Excel spreadsheet for further analysis. You can also integrate the data with other reporting tools such as Power BI to create custom reports and dashboards.

It is important to note that the Power Apps Trace function will only log results while the app is in Play Mode, and no traces will be sent during preview mode in Power Apps Studio. So, to ensure that you capture all errors and exceptions, it is recommended to test your app thoroughly in Play Mode before publishing it to a wider audience.

error handling in power fx

 

Conclusion

In a nutshell, Power Fx is a kickass low-code programming language that lets you create custom business solutions on the Microsoft Power Platform. You don’t need to be a technical genius to use it, thanks to its range of functions and formulas that make app building a breeze. But, let’s face it, even the best programmers can make mistakes. That’s where the “Formula-level error management” preview feature comes in! With this cool new tool, you can manage errors at the formula level, customize error messages, and debug your formulas with ease. We’ll take a deep dive into this feature, its benefits, and how you can use it to optimize your app development process. So, if you’re ready to become a Power Platform pro, buckle up and read on!

If you’re looking for more tips and tricks for Power FX app development, check out our post on Power FX Snippets for Model-Driven Apps! This post offers a collection of useful Power FX snippets that can help you streamline your development process and create powerful, efficient apps.

These links below gave me inspiration and use cases in this post: