Power Apps : Error Handling

How to manage your errors in Power Apps? No matter what you make, how you make it, at some point user is going to find an error. Mostly due to the fact that a Power Apps development goes faster, with a limited number of developpers. But as a good developper, you have managed all the errors, if not, follow this post.

  1. App.OnError
  2. IsError()
  3. IfError()
  4. Errors()
  5. Forms.OnFailure

1. App.OnError

We start which is, for me, the most important to know : App.OnError

The code in OnError is going to be triggered when an error appears into your App.

Inside the OnError, you can get the error through the Array AllErrors. As it is an array, use Last([Array]) to get the error. The error is an object as following:

KeyValue description
KindKind error code, list below.
ObservedThis is where the error appears for the end user. You will mostly find the ControlName.PropertyName.
MessageThe error message.
SourceThis is where the error come from. You might find the same value as Observed.
DetailsDetails information about networking errors.

List Of Power Apps Error Kind Codes:

If you have been around with Power Platform, you might have been to Matthew Devaney‘s blog. Thanks to him, he has made a matching in between kind error code and its meaning:

ErrorKindValue
None0
Sync1
MissingRequired2
CreatePermission3
EditPermissions4
DeletePermissions5
Conflict6
NotFound7
ConstraintViolated8
GeneratedValue9
ReadOnlyValue10
Validation11
Unknown12
Div013
BadLanguageCode14
BadRegex15
InvalidFunctionUsage16
FileNotFound17
AnalysisError18
ReadPermission19
NotSupported20
InsufficientMemory21
QuotaExceeded22
Network23
Numeric24
InvalidArgument25
Internal26

Mor information : https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-iferror#iferror

Here is an example of what I have in my App.OnError:

/* COLLECT ERROR */
Collect(
    colOnError,
    {
        message: Last(AllErrors).Message,
        kind: Last(AllErrors).Kind,
        observed: Last(AllErrors).Observed,
        source: Last(AllErrors).Source,
        htpresponse: Last(AllErrors).Details.HttpResponse,
        httpstatutscode: Last(AllErrors).Details.HttpStatusCode
    }
);
/* NOTIFICATION : USE THE ERROR AND DO SOMEHTING */
'PowerApps-Log'.Run(
    "Error from OnError - Power Apps",
    Coalesce(
        Last(AllErrors).Message,
        "-"
    ),
    Coalesce(
        Last(AllErrors).Source,
        "-"
    ),
    fx_usero365profil.mail
)

I have played around with my app. On Dataverse I have made a number column where value has to be in between 0 and 10. Then I have tried to patch a number which is above 10:

2. IsError()

Method to find out if a value return an error. True if return an error and false if success.

IsError([VALUE])

Examples :

// Return false
IsError(10/1);

// Return true
IsError(10/0);

// You can check variable as well: return true
UpdateContext({locDividedByZero : 10/0});
IsError(locDividedByZero);

More info: https://learn.microsoft.com/fr-fr/power-platform/power-fx/reference/function-iferror#iserror

Note: We often use IsBlankOrError() which is IsBlank() || IsError().

3. IfError()

Method to check out if something generate an error.

You can patch, set variable, do anything you want, and if error do something. This is like try, catch in developement.

You can do another action if success, it is otpional but can be usefull.

IfError([DO SOMETHING],[DO SOMETHING IF ERROR], [DO SOMETHING IF SUCCESS])

Example:

So in txtResult (displayed as 1 colored green in the picture).

IfError(
    Text(niDenominator.Value / niNumerator.Value),
    "Error"
)

So if I want to divide by zero, here is the result:

If you want to show a message while patch for example (check out Errors() example below):

IfError(
    Patch(...),
    UpdateContext({locMessage : "Error"}),
    UpdateContext({locMessage : "Success"})
)

More info: https://learn.microsoft.com/fr-fr/power-platform/power-fx/reference/function-iferror#iferror

4. Errors()

Get all the errors returned by a source. The errors are returned in an array of object.

The object is as following:

KeyValue description
RecordThe record in the data source that had the error. If the error occurred during the creation of a record, this column will be blank.
ColumnThe column that caused the error, if the error can be attributed to a single column. If not, this will be blank.
MessageA description of the error. This error string can be displayed for the end user. Be aware that this message may be generated by the data source and could be long and contain raw column names that may not have any meaning to the user.
ErrorAn error code, named ErrorKind that can be used in formulas to help resolve the error

Here are the following ErrorKind infiormation you can get:

ErrorKindDescription
ErrorKind.ConflictAnother change was made to the same record, resulting in a change conflict. Use the Refresh function to reload the record and try the change again.
ErrorKind.ConstraintViolationOne or more constraints have been violated.
ErrorKind.CreatePermissionAn attempt was made to create a record, and the current user doesn’t have permission to create records.
ErrorKind.DeletePermissionAn attempt was made to delete a record, and the current user doesn’t have permission to delete records.
ErrorKind.EditPermissionAn attempt was made to edit a record, and the current user doesn’t have permission to edit records.
ErrorKind.GeneratedValueAn attempt was made to change a column that the data source generates automatically.
ErrorKind.MissingRequiredThe value for a required column is missing from the record.
ErrorKind.NoneThere is no error.
ErrorKind.NotFoundAn attempt was made to edit or delete a record, but the record couldn’t be found. Another user may have changed the record.
ErrorKind.ReadOnlyValueAn attempt was made to change a column that’s read only.
ErrorKind.SyncAn error was reported by the data source. Check the Message column for more information.
ErrorKind.UnknownThere was an error, but of an unknown kind.
ErrorKind.ValidationThere was a general validation issue detected, that did not fit one of the other kinds.

Object and ErrorKind table come from : https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-errors

Example:

In this scenario, you are going to patch and check for an Error and get the value:

// Do the Patch in IfError
IfError(
    Patch(
        'DataSource',
        Record,
        {
            Name: "Hello World"
        }
    ),
    // If Error get the last errors
    UpdateContext({locMessage: Last(Errors('DataSource')).Message}),
    // If Success
    UpdateContext({locMessage: "Your item has been edited"})
)

5. Forms.OnFailure

If you are using a Form into your Power Apps, you can use OnFailure property of your Form to manage the errors.

You can use Form.Error to get the error message.

Example: