Error.Record

语法

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

输出

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

示例 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

输出

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