Skip to content

Commit 9ab3e55

Browse files
authored
Mention loop advantage on custom errors
Resolves #1988
1 parent 5676457 commit 9ab3e55

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

book/control_flow.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,44 @@ $result
211211
# => ╰───┴───╯
212212
```
213213

214+
When creating custom errors, loops have an advantage over pipelines because they don't fail evaluation.
215+
216+
Creating an error in a pipeline yields a pipeline evaluation error *and* the custom error:
217+
218+
```nu
219+
[0] | each { error make { msg: "Custom error" } | default '' }
220+
# => Error: nu::shell::eval_block_with_input
221+
# =>
222+
# => × Eval block failed with pipeline input
223+
# => ╭─[entry #20:1:2]
224+
# => 1 │ [0] | each { error make { msg: "Custom error" } | default '' }
225+
# => · ┬
226+
# => · ╰── source value
227+
# => ╰────
228+
# =>
229+
# => Error:
230+
# => × Custom error
231+
# => ╭─[entry #20:1:14]
232+
# => 1 │ [0] | each { error make { msg: "Custom error" } | default '' }
233+
# => · ─────┬────
234+
# => · ╰── originates from here
235+
# => ╰────
236+
```
237+
238+
When using a loop, a `for` loop in this example, evaluation succeeds and only the created error occurs:
239+
240+
```nu
241+
for x in [0] { error make { msg: "Custom error" } }
242+
# => Error: nu::shell::error
243+
# =>
244+
# => × Custom error
245+
# => ╭─[entry #18:1:16]
246+
# => 1 │ for x in [0] { error make { msg: "Custom error" } }
247+
# => · ─────┬────
248+
# => · ╰── originates from here
249+
# => ╰────
250+
```
251+
214252
### `for`
215253

216254
[`for`](/commands/docs/for.html) loops over a range or collection like a list or a table.

0 commit comments

Comments
 (0)