Skip to content

Commit 7004be1

Browse files
committed
Edits
1 parent 7c0aa7f commit 7004be1

1 file changed

Lines changed: 38 additions & 11 deletions

File tree

query-languages/m/m-spec-error-handling.md

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: M Language Error Handling
33
description: Describes error handling in the Power Query M formula language
44
ms.topic: language-reference
5-
ms.date: 8/2/2022
5+
ms.date: 1/29/26
66
ms.custom: "nonautomated-date"
77
ms.subservice: m-specification
88
---
@@ -24,29 +24,56 @@ The syntax for raising an error is as follows:
2424
_error-raising-expression:_<br/>
2525
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`error` _expression_
2626

27-
Text values can be used as shorthand for error values. For example:
27+
The _expression_ being raised must evaluate to an _error value_.
28+
29+
In canonical form, an _error value_ is a record with the following structure:
2830

2931
```powerquery-m
30-
error "Hello, world" // error with message "Hello, world"
32+
[
33+
ErrorCode = ...,
34+
Reason = ...,
35+
Message = ...,
36+
Detail = ...,
37+
Message.Format = ...,
38+
Message.Parameters = ...
39+
]
3140
```
3241

33-
Full error values are records and can be constructed using the `Error.Record` function:
42+
All of the above record fields are optional, and so may be omitted or set to null. When not null, _Reason_, _Message_, and _Message.Format_ must be text, and _Message.Parameters_ must be a list.
43+
44+
Any additional fields included in the record will be ignored and so not included in the error that is raised.
45+
46+
_Message.Format_ may contain string interpolation placeholders in the form of `#{x}`, where `x` is a zero-based index. When an error is raised with a non-null _Message.Foramt_, interpolation will be performed using _Message.Format_ as the format string and _Message.Parameters_ as the values to be positionally applied. The resulting output will become the raised error's _Message_.
47+
48+
For example:
3449

3550
```powerquery-m
36-
error Error.Record("FileNotFound", "File my.txt not found",
37-
"my.txt")
51+
error [
52+
Message.Format = "Unexpected value '#{0}' in field #{1}",
53+
Message.Parameters = {"???", "Customer"}
54+
]
3855
```
3956

40-
The above expression is equivalent to:
57+
Will result in the following error being raised:
4158

4259
```powerquery-m
43-
error [
44-
Reason = "FileNotFound",
45-
Message = "File my.txt not found",
46-
Detail = "my.txt"
60+
[
61+
ErrorCode = null,
62+
Reason = null,
63+
Message = "Unexpected value '???' in field Customer",
64+
Detail = null,
65+
Message.Format = "Unexpected value '#{0}' in field #{1}",
66+
Message.Parameters = {"???", "Customer"}
4767
]
4868
```
4969

70+
Text values can be used as shorthand for error values. For example:
71+
72+
```powerquery-m
73+
error "Hello, world" // error with message "Hello, world"
74+
```
75+
76+
5077
Raising an error will cause the current expression evaluation to stop, and the expression evaluation stack will unwind until one of the following occurs:
5178

5279
* A record field, section member, or let variable&mdash;collectively: an _entry_&mdash;is reached. The entry is marked as having an error, the error value is saved with that entry, and then propagated. Any subsequent access to that entry will cause an identical error to be raised. Other entries of the record, section, or let expression are not necessarily affected (unless they access an entry previously marked as having an error).

0 commit comments

Comments
 (0)