跳到主要内容

Error.Record

从为原因、消息、详细信息和错误代码提供的文本值返回错误记录。

Syntax

Error.Record(
reason as text,
optional message as text,
optional detail as any,
optional parameters as list,
optional errorCode as text
) as record

Remarks

从为原因、消息、详细信息和错误代码提供的文本值返回错误记录。

  • reason: 错误的高级原因。
  • message:(可选)下面是错误的说明。
  • detail:(可选)错误的其他详细信息。
  • parameters:(可选)提供额外错误上下文的值列表,通常用于诊断或程序处理。
  • errorCode:(可选)错误标识符。

Examples

Example #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

Result:

[
HasError = true,
Error =
[
Reason = "DivideByZero",
Message = "You attempted to divide by zero.",
Detail = null,
Message.Format = null,
Message.Parameters = null,
ErrorCode = null
]
]

Example #2

处理客户 ID 不存在错误的条目。如果没有错误,则表示输入成功。

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

Result:

[
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"
]
]

Category

Error