Skip to content

Latest commit

 

History

History
149 lines (122 loc) · 4.03 KB

ErrorRecord.Grammar.md

File metadata and controls

149 lines (122 loc) · 4.03 KB

Inline throw

error is the name of a language keyword that throws an exception. There's a built in function named Error.Record to construct the argument record for you

error [
  Reason = "Business Rule Violated", 
  Message = "Item codes must start with a letter", 
  Detail = "Non-conforming Item Code: 456"
]

To get a GUID to easily filter query tracing, you can Table.AddColumn() a guid for traces using trace specific rows to specific Diagnostics.ActivityId() as text

= Diagnostics.ActivityId()

Function: Error.Record() signature

Creates the record to throw

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

See Also

Examples from the Docs

The Ellipsis expression is sugar for throwing this expression

x =...

is equivalent to

x = error Error.Record("Expression.Error", "Not Implemented")

This expression

x = error Error.Record(
    "FileNotFound", "File my.txt not found", "my.txt")

is equivalent to

x = error [
    Reason = "FileNotFound",
    Message = "File my.txt not found",
    Detail  = "my.txt"
]

more examples

Extra Requirements Specific to Try Catch

  • The catch function must be defined inline, so a reference to a function cannot be used.
  • each cannot be used to define the catch-function
  • the function defintion cannot include any type constraints

Try Expression Types

Try catch (e) =>

= Table.AddColumn(
Source, 
    "Clean Standard Rate",
    each 
        try [Standard Rate]
        catch (e) =>
            if e[Message] = "Invalid cell value '#REF!'." then [Special Rate] * 2
            else if e[Message] = "Invalid cell value '#DIV/0!'." then [Special Rate] / 3
            else 0
)

Try catch () =>

Functionally equivalent to try <e> otherwise <e>

let
    x = try 1 / 0
        catch () => "null"
in 
    x

Try otherwise

let
    x = try 1 / 0 
        otherwise "null"
in 
    x

Try

let
    x = try "A"
in
    if x[HasError] then x[Error] else x[Value]

yaml summary

returns:
    type: record
reason: 
    type: text
    default: "Expression.Error"
    notes: . | 
        the default value isn't mandatory, may be implementation details and change
message:
    type: optional nullable text

detail:
    type: optional detail as any

parameters:
    type: optional nullable list