Power Fx 包含一個預覽功能,可實現公式級錯誤處理。 預設情況下,此功能在「設定」中處於啟用狀態。
此設定提供對 IfError、IsError、Error 和 IsBlankorError 等公式的存取。 這些函式可讓您偵測錯誤、提供替代值或根據錯誤採取特定的動作。
驗證錯誤處理
這些功能有助於驗證輸入,例如不正確的格式或必填欄位。 使用 If 陳述式或 IsBlank 和 IsError 等函式來驗證使用者輸入。 提供清晰的錯誤消息,並防止進一步處理,直到輸入得到糾正。
If( IsBlank(TextInput.Text),
Notify("Field cannot be blank",
NotificationType.Error),
// Continue with processing
)
修補檔函式錯誤處理
與前面的範例類似,Error 函式有助於在將資料修補到資料來源時擷取錯誤。
Patch 函式以兩種方式報告錯誤。
它可以返回錯誤值作為操作的結果。
UpdateContext(
{
result : Patch(
Feeds,
Defaults(Feeds),
{
createdon: Now(),
crde8_content: TextInput1_1.Text
cr9ce_imageurl: filename
}
)
}
)
您可以使用 IsError 來偵測錯誤,並使用 IfError 取代或抑制錯誤。
IfError(result, Notify("There was an issue saving data" , NotificationType.Error));
IfError(result, Notify("There was an issue saving data" , & FirstError.Message, NotificationType.Error))
If(
IsError(
Patch(
Feeds,
Defaults(Feeds),
{
createdon: Now(),
crde8_content: TextInput1_1.Txt,
cr9ce_imageurl: filename
}
)
),
Notify("Error: There was an issue saving data", NotificationType.Error)
)
表單錯誤處理
當您使用表單透過 SubmitForm 函式提交資料時,請使用表單控制項屬性 OnFailure 通知使用者錯誤訊息。
// OnSelect property of the form's submit button
SubmitForm(frm_SubmitData);
// OnSuccess property of the form
Navigate('Success Screen');
// OnFailure property of the form
Notify("Error: the invoice could not be created", NotificationType.Error);
使用 OnError 屬性自訂錯誤訊息
Power Apps OnError 屬性可讓您擷取應用程式中所有未處理的錯誤。
OnError 屬性可讓您執行一個運算式,該運算式在應用程式每次未處理錯誤時執行 (例如將其儲存在變數中或使用 IfError 等函式將其替換為其他值)。 若要使用該OnError 屬性,需要將其添加到要應用它的應用。 然後,可以透過在 OnError 屬性框中編寫公式來指定要顯示的錯誤訊息。
需要注意的是,App.OnError 無法像 IfError 那樣取代錯誤。 在執行 App.OnError 時,錯誤已經發生,結果已經透過其他公式傳播。
App.OnError 僅控制如何向終端使用者報告錯誤,並在需要時為製作者提供一個勾點來記錄錯誤。
App.OnError 上的這段程式碼可以幫助定位錯誤的來源:
Notify(
Concatenate(
FirstError.Message,
", Observed: ",
FirstError.Observed,
", Source: ",
FirstError.Source
),
NotificationType.Error
)