Using OnError Event in Power Apps: Unhandled Errors Tracing

Hello everyone and welcome to my blog. In today’s blog, we will explore the OnError event of the App object and how it can be used to trace and log all unhandled errors in your Power App.

Let’s get started. The App object in canvas apps has an OnError property. And quite surprisingly, while many app makers are aware of this property, many don’t realize how useful it can be to log any errors in your app.

So what is this OnError event of the app? The OnError event is fired whenever there is any unhandled exception in your canvas app be it related for formula or any HttpRequest call you make.

But what is an unhandled exception? Let’s take an example.

I have a button – Generate Error in my app. In the OnSelect of the button, I have deliberately introduced a Divide by zero exception.

Quite obviously, when the button is clicked, we will get an Divide by zero exception. But it is an unhandled exception because we are not handling the error at the formula level. We could have easily done that if we used the IfError function but we will come to that scenario later.

Now that an unhandled exception has occurred, the OnError of the App will be raised. But how to get the source of the error and the error message? And this is what majority of app makers are unaware of.

Inside the OnError event, we can get the entire error information for which OnError event was raised. And that is using the FirstError property.

The FirstError property will give you all details regarding the error.

  • Error Message
  • Source of Error
  • If the error is the result of HttpRequest
  • Error Kind.

Using the below formula, I have extracted the error message and source of the error in OnError event.

And below is the output when I click on the Generate Error button.

Off-course we could have logged the error in trace or application insights. But I will keep that for another blog where I will write in detail about all tracing methods in Power Apps.

Quite interesting, isn’t it? Additionally the FirstError has a property called Details. The Details property return the HttpResponse status code and HttpResponse message for errors generating out of Http request calls.

I would strongly suggest that any app developer must use the OnError event to take care of any unhandled exceptions in the code. Afterall there may be so many scenarios in your app where you cannot anticipate an error. The OnError would be fallback for all such scenarions.

Now let’s check for errors which are handled at the formula level. This time I revised the formula for OnSelect of the button but with error handling using IfError.

Since I have handled the error at the formula level using IfError, the OnError event of the app will not fire.

I hope it’s pretty clear now and you understand why the OnError of the app must be used to handle all unhandled exceptions.

Hope this helped.

Debajit Dutta


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading