Canvas Apps Features

How to create custom errors in Power Apps!

Don't let his happen to your apps!
Don't let his happen to your apps!

Background

For the past month or so, I have been working on an app that shows an org structure, among other things. At a very high level, you select a user (from a gallery) to then view their manager and direct reports. The app uses one of my favorite connectors - Office365Users. While it provides a lot of information about users, it does have its own weird behavior and errors.

Issue - runtime errors

Once a user is selected, the following steps happen:

  1. The manager of the selected user is fetched and populated into a collection
  2. The selected user is added to the same collection, and finally,
  3. The direct reports of the selected user are added to the same collection

The problem is Office365Users connector throws an error when you use the GetManagerV2 function and the user that is passed on as a parameter to the function does not have a manager. I created a simplified version of the app to illustrate this issue.

Here is how the app behaves when there is a manager for the selected user:

When the selected user has a manager
When the selected user has a manager (click to enlarge)

Here is how the app behaves when there is no manager for the selected user:

When the selected user has no manager
When the selected user has no manager (click to enlarge)

Here is a screenshot of the error message that the end user sees:

Error when the selected user has no manager
Error when the selected user has no manager (click to enlarge)

The app maker also sees this within the studio:

Error within Power Apps studio
Error within Power Apps studio (click to enlarge)

And the app checker does its part too:

App checker error
App checker error (click to enlarge)

My initial thought was to simply check if the function returned a manager or not so I tried the following expressions but none of them worked:

  1. IsError:
    IsError(Office365Users.ManagerV2(ThisItem.mail))
  2. IsBlank:
    IsBlank(Office365Users.ManagerV2(ThisItem.mail))
  3. IsEmpty:
    IsEmpty(Office365Users.ManagerV2(ThisItem.mail))

Resolution - error management

To have the ability to "catch" this error (and other similar ones), turn on the following experimental feature:

  • Formula-level error management: Create custom errors to improve app usability and gather valuable error information

Formula-level error management experimental feature
Formula-level error management experimental feature

With this feature turned on, I can use something like this in the OnSelect of the users gallery:

If(
    IsError(Office365Users.ManagerV2(ThisItem.Id)),
    Notify(
        ThisItem.DisplayName & " has no manager defined in Office365.",
        NotificationType.Warning
    )
)

Here's how the app behaves with the experimental feature turned on and the addition of the above code:

With Formula-level error management experimental feature
With Formula-level error management experimental feature (click to enlarge)

Summary

To summarize, if you want to have more control over errors in your apps to provide better user experience, turn on the Formula-level error management experimental feature. Also note that to write blank values to a database, you need this experimental feature. This is referenced in one of my previous blog posts here and in Microsoft Docs here.

Recent articles

  1. Office365Users Connector – Get rid of this warning!
  2. Power Apps galleries – how to auto scroll!
  3. A-Z: 26 tips! Learn how to improve your Power Apps!

6 thoughts on “How to create custom errors in Power Apps!”

Leave a Reply