語法
Error.Record(
reason as text,
optional message as nullable text,
optional detail as any,
optional parameters as nullable list,
optional errorCode as nullable text
) as record
關於
從提供的文字值中回傳錯誤記錄,涵蓋原因、訊息、詳細及錯誤代碼。
-
reason:錯誤的主要原因。 -
message:(可選)錯誤描述。 -
detail:(可選)關於錯誤的更多詳細資訊。 -
parameters:(可選)一串提供錯誤額外上下文的值清單,通常用於診斷或程式處理。 -
errorCode:(可選)錯誤識別碼。
範例 1
處理除以零的誤差。
使用方式
let
input = 100,
divisor = 0,
result = try if divisor = 0 then
error Error.Record(
"DivideByZero",
"You attempted to divide by zero."
)
else
input / divisor
in
result
Output
[
HasError = true,
Error =
[
Reason = "DivideByZero",
Message = "You attempted to divide by zero.",
Detail = null,
Message.Format = null,
Message.Parameters = null,
ErrorCode = null
]
]
範例 2
處理一個不存在的客戶識別錯誤條目。 若未發生錯誤,請表示成功輸入。
使用方式
let
CustomerId = 12345,
result = try if CustomerId > 9999 then
error Error.Record(
"CustomerNotFound",
Text.Format("Customer ID #{0} wasn't found.", {CustomerId}),
"Customer doesn't exist.",
{
Text.Format("Invalid ID = #{0}", {CustomerId}),
"Valid IDs: https://api.contoso.com/customers"
},
"ERR404"
)
else CustomerId
in
result
Output
[
HasError = true,
Error = [
Reason = "CustomerNotFound",
Message = "Customer ID 12345 wasn't found.",
Detail = "Customer doesn't exist.",
Message.Format = "Customer ID 12345 wasn't found.",
Message.Parameters = {
"Invalid ID = 12345",
"Valid IDs: https://api.contoso.com/customers"
},
ErrorCode = "ERR404"
]
]