Condividi tramite


Error.Record

Sintassi

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

Informazioni

Restituisce un record di errore dai valori di testo forniti per motivo, messaggio, dettaglio e codice errore.

  • reason: causa generale dell'errore.
  • message: (Facoltativo) Descrizione dell'errore.
  • detail: (Facoltativo) Informazioni dettagliate aggiuntive sull'errore.
  • parameters: (Facoltativo) Elenco di valori che forniscono contesto aggiuntivo per l'errore, in genere usato per la diagnostica o la gestione a livello di codice.
  • errorCode: (Facoltativo) Identificatore per l'errore.

Esempio 1

Gestire un errore di divisione per zero.

Utilizzo

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

Risultato

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

Esempio 2

Gestire una voce con un errore di ID cliente inesistente. Se non si verifica alcun errore, indicare una voce riuscita.

Utilizzo

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

Risultato

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