Skip to content

Mention error handling as a potential reason to use a loop #1988

@ItsHarper

Description

@ItsHarper

When you're doing validation on iterable data and want to report problems using error make, it's much better to use for than each, as each will throw an nu::shell::eval_block_with_input instead of the actual error you made.

Example

for

> for x in [0] { error make { msg: "Custom error" } }
Error:   × Custom error
   ╭─[entry #635:1:16]
 1  for x in [0] { error make { msg: "Custom error" } }
   ·                ─────┬────
   ·                     ╰── originates from here
   ╰────

each

> [0] | each { error make { msg: "Custom error" } }
Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
   ╭─[entry #629:1:2]
 1  [0] | each { error make { msg: "Custom error" } }
   ·  
   ·  ╰── source value
   ╰────

Error:
  × Custom error
   ╭─[entry #629:1:14]
 1  [0] | each { error make { msg: "Custom error" } }
   ·              ─────┬────
   ·                   ╰── originates from here
   ╰────

The fact that you get a nu::shell::eval_block_with_input instead of the error you made of course impacts catch blocks as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions