Error.Record
ExpressionCreates an error record from the provided reason, message, detail, parameters, and error code values.
Syntax
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 recordParameters
| Name | Type | Required | Description |
|---|---|---|---|
reason | text | Yes | The high-level cause of the error (e.g., "DataFormat.Error", "Expression.Error"). |
message | text | No | A human-readable description of the error. |
detail | any | No | Additional detailed information about the error. |
parameters | list | No | A list of values that provide additional context for the error, typically used for diagnostics or programmatic handling. |
errorCode | text | No | An identifier for the error. |
Return Value
record — An error record with Reason, Message, Detail, Message.Parameters, and ErrorCode fields.
Remarks
Error.Record creates a structured error record that can be used with the error keyword to raise custom errors in M. The resulting record contains the fields Reason, Message, Detail, Message.Format, Message.Parameters, and ErrorCode.
This is the recommended way to construct error values for use with error expressions and try ... catch patterns:
``powerquery error Error.Record("MyCustomError", "Something went wrong", [Step = "Validation"]) ``
### Error record fields
The returned record contains these fields:
- Reason — the high-level error category (from the
reasonparameter). - Message — the human-readable error description (from the
messageparameter). - Detail — additional structured detail (from the
detailparameter). - Message.Format — the original message format string.
- Message.Parameters — the parameters list for message formatting.
- ErrorCode — the error code identifier.
While you can also raise errors with a plain text string (error "something"), Error.Record gives you full control over all fields of the error record, making it easier to catch and handle errors programmatically.
Examples
Example 1: Raise and catch a custom error
let
Result = try error Error.Record(
"DivideByZero",
"You attempted to divide by zero."
)
in
#table(
{"HasError", "Reason", "Message"},
{{Result[HasError], Result[Error][Reason], Result[Error][Message]}}
)HasError | Reason | Message | |
|---|---|---|---|
| 1 | TRUE | DivideByZero | You attempted to divide by zero. |
Example 2: Error record with all fields
let
Result = try error Error.Record(
"CustomerNotFound",
"Customer ID 12345 was not found.",
"Customer does not exist.",
{"Invalid ID = 12345", "Valid IDs: https://api.contoso.com/customers"},
"ERR404"
)
in
#table(
{"Reason", "Message", "ErrorCode"},
{{Result[Error][Reason], Result[Error][Message], Result[Error][ErrorCode]}}
)Reason | Message | ErrorCode | |
|---|---|---|---|
| 1 | CustomerNotFound | Customer ID 12345 was not found. | ERR404 |