Error.Record

Syntaxe

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

Informace

Vrátí záznam chyby z zadaných textových hodnot z důvodu, zprávy, podrobností a kódu chyby.

  • reason: Hlavní příčina chyby.
  • message: (Volitelné) Popis chyby.
  • detail: (Volitelné) Další podrobné informace o chybě.
  • parameters: (Volitelné) Seznam hodnot, které poskytují další kontext chyby, obvykle používané pro diagnostiku nebo programové zpracování.
  • errorCode: (Volitelné) Identifikátor chyby.

Příklad 1

Zpracování chyby dělení nulou

Využití

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

Výstup

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

Příklad 2

Zpracování položky s neexistující chybou ID zákazníka Pokud nedojde k žádné chybě, uveďte úspěšnou položku.

Využití

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

Výstup

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