Skip to content

Commit e0f4e41

Browse files
authored
Merge pull request #296760 from mumian/0321-fail-function
Document the fail() function
2 parents 7fd5a72 + 3832b79 commit e0f4e41

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Bicep functions - flow control
3+
description: Describes the functions that incluence execution flow.
4+
ms.topic: reference
5+
ms.custom: devx-track-bicep
6+
ms.date: 03/25/2025
7+
---
8+
9+
# Flow control functions for Bicep
10+
11+
Bicep provides the `fail` function for influencing execution flow.
12+
13+
The `fail` function is primarily used for enforcing constraints and terminating evaluation based on logical conditions. It usually works within short-circuiting expressions like [?? (coalesce operator)](./operators-logical.md#coalesce-) and [?: (ternary operator)](./operators-logical.md#conditional-expression--). For more information, see [Logical operators](./operators-logical.md).
14+
15+
## fail
16+
17+
`fail(arg1)`
18+
19+
The `fail` function is useful for enforcing constraints in expressions, but it can only be used within short-circuiting functions—operations that stop execution as soon as the outcome is determined.
20+
21+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
22+
23+
### Parameters
24+
25+
| Parameter | Required | Type | Description |
26+
|:--- |:--- |:--- |:--- |
27+
| arg1 |Yes |string|A descriptive error message that explains why the failure occurred.|
28+
29+
### Examples
30+
31+
The following example shows how to use `fail`.
32+
33+
```bicep
34+
anObjectParameter.?name ?? anObjectParameter.?id ?? fail('Expected anObjectParameter to have either a .name or a .id property')
35+
```
36+
37+
Here, the coalesce operator (??) ensures that if **.name** exists, execution stops immediately. If **.name** is null or undefined, **.id** is checked. If both are missing, `fail` is triggered, preventing further execution.
38+
39+
```bicep
40+
x != 0 ? y / x : fail('x cannot be zero because it will be used as a divisor')
41+
```
42+
43+
In this case, the ternary operator (? :) checks if **x** is nonzero before performing division. If **x** is **0**, `fail` is invoked, stopping execution before an invalid operation occurs.
44+
45+
## Next steps
46+
47+
* For other actions involving logical values, see [logical operators](./operators-logical.md).

articles/azure-resource-manager/bicep/bicep-functions-logical.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A boolean of the converted value.
3232

3333
### Examples
3434

35-
The following example shows how to use bool with a string or integer.
35+
The following example shows how to use `bool` with a string or integer.
3636

3737
```bicep
3838
output trueString1 bool = bool('true')

articles/azure-resource-manager/bicep/bicep-functions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ The following functions are available for loading the content from external file
8787
* [loadYamlContent](bicep-functions-files.md#loadyamlcontent)
8888
* [loadTextContent](bicep-functions-files.md#loadtextcontent)
8989

90+
## Flow-control functions
91+
92+
The following function is available for influencing execution flow. This function is in the `sys` namespace:
93+
94+
* [fail](bicep-functions-flow-control.md#fail)
95+
9096
## Lambda functions
9197

9298
The following functions are available for working with lambda expressions. All of these functions are in the `sys` namespace:

articles/azure-resource-manager/bicep/operators-logical.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep logical operators
33
description: Describes Bicep logical operators that evaluate conditions.
44
ms.topic: reference
55
ms.custom: devx-track-bicep
6-
ms.date: 02/12/2025
6+
ms.date: 03/21/2025
77
---
88

99
# Bicep logical operators
@@ -153,7 +153,7 @@ Output from the example:
153153

154154
`operand1 ?? operand2`
155155

156-
Returns first non-null value from operands.
156+
Returns first non-null value from operands. To control the execution flow, see the [fail function](./bicep-functions-flow-control.md#fail).
157157

158158
### Operands
159159

@@ -197,7 +197,7 @@ Output from the example:
197197

198198
`condition ? true-value : false-value`
199199

200-
Evaluates a condition and returns a value whether the condition is true or false.
200+
Evaluates a condition and returns a value whether the condition is true or false. To control the execution flow, see the [fail function](./bicep-functions-flow-control.md#fail).
201201

202202
### Operands
203203

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@
338338
- name: File functions
339339
displayName: loadFileAsBase64,loadJsonContent,loadYamlContent,loadTextContent
340340
href: bicep-functions-files.md
341+
- name: Flow-control functions
342+
displayName: fail
343+
href: bicep-functions-flow-control.md
341344
- name: Lambda functions
342345
displayName: filter,map,reduce,sort,toObject
343346
href: bicep-functions-lambda.md

0 commit comments

Comments
 (0)