PowerApps 中的 Errors 函数Errors function in PowerApps

提供之前对数据源的更改的错误信息。Provides error information for previous changes to a data source.

概述Overview

数据源的记录发生更改时可能会出现错误。Errors can happen when a record of a data source is changed. 导致错误的原因有许多,包括网络故障、权限不足和编辑冲突。Many causes are possible, including network outages, inadequate permissions, and edit conflicts.

Patch 函数和其他数据函数不直接返回错误。The Patch function and other data functions don't directly return errors. 而会返回其运算的结果。Instead they return the result of their operation. 数据函数执行后,可使用 Errors 函数获取任何错误的详细信息。After a data function executes, you can use the Errors function to obtain the details of any errors. 可在公式 IsEmpty( Errors ( ... ) ) 中使用 [IsEmpty] 函数检查是否存在错误。You can check for the existence of errors with the [IsEmpty] function in the formula IsEmpty( Errors ( ... ) ).

可使用 ValidateDataSourceInfo 函数在错误出现之前避免这些错误。You can avoid some errors before they happen by using the Validate and DataSourceInfo functions. 请参阅使用数据源,了解如何处理和避免错误的更多建议。See working with data sources for more suggestions on how to work with and avoid errors.

说明Description

Errors 函数会返回一个错误的,其中包含以下The Errors function returns a table of errors that contains the following columns:

  • 记录Record. 数据源中有错误的记录。The record in the data source that had the error. 如果在记录的创建过程中发生错误,此列将为空白If the error occurred during the creation of a record, this column will be blank.
  • Column. 导致错误的列(如果该错误可归因于单个列)。The column that caused the error, if the error can be attributed to a single column. 如果没有,此列将为空白If not, this will be blank.
  • 消息Message. 错误的说明。A 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.
  • 错误Error. 可在公式中使用以帮助解决错误的错误代码:An error code that can be used in formulas to help resolve the error:
ErrorKindErrorKind 说明Description
ErrorKind.ConflictErrorKind.Conflict 对同一个记录进行了其他更改,从而导致更改冲突。Another change was made to the same record, resulting in a change conflict. 使用 Refresh 函数重新加载记录,尝试再次进行更改。Use the Refresh function to reload the record and try the change again.
ErrorKind.ConstraintViolationErrorKind.ConstraintViolation 违反了一个或多个约束。One or more constraints have been violated.
ErrorKind.CreatePermissionErrorKind.CreatePermission 尝试创建一个记录,但当前用户没有权限创建记录。An attempt was made to create a record, and the current user doesn't have permission to create records.
ErrorKind.DeletePermissionErrorKind.DeletePermission 尝试删除一个记录,但当前用户没有权限删除记录。An attempt was made to delete a record, and the current user doesn't have permission to delete records.
ErrorKind.EditPermissionErrorKind.EditPermission 尝试编辑一个记录,但当前用户没有权限编辑记录。An attempt was made to edit a record, and the current user doesn't have permission to edit records.
ErrorKind.GeneratedValueErrorKind.GeneratedValue 尝试更改数据源自动生成的列。An attempt was made to change a column that the data source generates automatically.
ErrorKind.MissingRequiredErrorKind.MissingRequired 记录中缺少所需列的值。The value for a required column is missing from the record.
ErrorKind.NoneErrorKind.None 不存在错误。There is no error.
ErrorKind.NotFoundErrorKind.NotFound 尝试编辑或删除一个记录,但找不到该记录。An attempt was made to edit or delete a record, but the record couldn't be found. 其他用户可能已更改该记录。Another user may have changed the record.
ErrorKind.ReadOnlyValueErrorKind.ReadOnlyValue 尝试更改只读列。An attempt was made to change a column that's read only.
ErrorKind.SyncErrorKind.Sync 数据源报告了错误。An error was reported by the data source. 查看“消息”列了解详细信息。Check the Message column for more information.
ErrorKind.UnknownErrorKind.Unknown 存在未知类型的错误。There was an error, but of an unknown kind.
ErrorKind.ValidationErrorKind.Validation 检测到一个常规的验证问题,不符合其他任一类型。There was a general validation issue detected, that did not fit one of the other kinds.

通过向函数提供 Record 参数,可针对整个数据源,也可仅针对选定的行返回错误。Errors can be returned for the entire data source, or for only a selected row by providing the Record argument to the function.

如果无法创建记录,Patch 或其他数据函数可能会返回空白值。Patch or another data function may return a blank value if, for example, a record couldn't be created. 可以将 blank 传递到 Errors,它将在这类情况下返回相应的错误信息。You can pass blank to Errors, and it will return appropriate error information in these cases. 随后对同一数据源使用数据函数将清除此错误信息。Subsequent use of data functions on the same data source will clear this error information.

如果没有错误,Errors 返回的表将为,且可使用 IsEmpty 函数进行测试。If there are no errors, the table that Errors returns will be empty and can be tested with the IsEmpty function.

语法Syntax

Errors( DataSource [, Record ] )Errors( DataSource [, Record ] )

  • DataSource - 必需。DataSource – Required. 要为其返回错误的数据源。The data source for which you want to return errors.
  • 记录 - 可选。Record – Optional. 要为其返回错误的特定记录。A specific record for which you want to return errors. 如果未指定此参数,函数将返回整个数据源的错误。If you don't specify this argument, the function returns errors for the entire data source.

示例Examples

分步操作Step by Step

在此示例中,我们将使用 IceCream 数据源:For this example, we'll be working with the IceCream data source:

通过该应用,用户将 Chocolate 记录加载到一个数据输入窗体中,然后将 Quantity 更改为 90。Through the app, a user loads the Chocolate record into a data-entry form and then changes the value of Quantity to 90. 要使用的记录置于上下文变量 EditRecord 中:The record to be worked with is placed in the context variable EditRecord:

  • UpdateContext( { EditRecord: First( Filter( IceCream, Flavor = "Chocolate" ) ) } )UpdateContext( { EditRecord: First( Filter( IceCream, Flavor = "Chocolate" ) ) } )

若要在数据源中进行此更改,请使用 Patch 函数:To make this change in the data source, the Patch function is used:

  • Patch( IceCream, EditRecord, Gallery.Updates )Patch( IceCream, EditRecord, Gallery.Updates )

其中 Gallery.Updates 计算结果为 { Quantity: 90 },因为只修改了 Quantity 属性。where Gallery.Updates evaluates to { Quantity: 90 }, since only the Quantity property has been modified.

但就在调用 Patch 函数之前,其他人将 Chocolate 的 Quantity 修改为 80。Unfortunately, just before the Patch function was invoked, somebody else modifies the Quantity for Chocolate to 80. PowerApps 将检测到此情况,且不允许发生更改冲突。PowerApps will detect this and not allow the conflicting change to occur. 可使用以下公式检查是否存在这种情况:You can check for this situation with the formula:

  • IsEmpty( Errors( IceCream, EditRecord ) )IsEmpty( Errors( IceCream, EditRecord ) )

它将返回 false,因为 Errors 函数返回了下表:which returns false, because the Errors function returned the following table:

记录Record Column 消息Message 错误Error
{ Flavor: "Chocolate", Quantity: 100 }{ Flavor: "Chocolate", Quantity: 100 } 空白blank “另一个用户已修改你正尝试修改的记录。"Another user has modified the record that you're trying to modify. 请重新加载该记录,然后重试。”Please reload the record and try again." ErrorKind.ConflictErrorKind.Conflict

可在窗体上放置标签,向用户显示此错误。You can place a label on the form to show this error to the user.

  • 若要显示错误,请将标签的 Text 属性设置为此公式:To show the error, set the label's Text property to this formula:
    Label.Text = First(Errors( IceCream, EditRecord )).MessageLabel.Text = First(Errors( IceCream, EditRecord )).Message

还可以在窗体上添加“重新加载”按钮,使用户可高效地解决冲突。You can also add a Reload button on the form, so that the user can efficiently resolve the conflict.

  • 若要只在发生冲突时显示该按钮,请将按钮的 Visible 属性设置为此公式:To show the button only when a conflict has occurred, set the button's Visible property to this formula:
    “!IsEmpty( Lookup( Errors( IceCream, EditRecord ), Error = ErrorKind.Conflict ) )”!IsEmpty( Lookup( Errors( IceCream, EditRecord ), Error = ErrorKind.Conflict ) )
  • 若要还原用户选择此按钮时的更改,请将其 OnSelect 属性设置为此公式:To revert the change which the user selects the button, set its OnSelect property to this formula:
    “ReloadButton.OnSelect = Revert( IceCream, EditRecord )”ReloadButton.OnSelect = Revert( IceCream, EditRecord )