diff --git a/crates/nu-command/src/core_commands/try_.rs b/crates/nu-command/src/core_commands/try_.rs index b07651a979..95b4d5b1a7 100644 --- a/crates/nu-command/src/core_commands/try_.rs +++ b/crates/nu-command/src/core_commands/try_.rs @@ -57,6 +57,13 @@ impl Command for Try { match result { Err(error) | Ok(PipelineData::Value(Value::Error { error }, ..)) => { + if let nu_protocol::ShellError::Break(_) = error { + return Err(error); + } else if let nu_protocol::ShellError::Continue(_) = error { + return Err(error); + } else if let nu_protocol::ShellError::Return(_, _) = error { + return Err(error); + } if let Some(catch_block) = catch_block { let catch_block = engine_state.get_block(catch_block.block_id); let err_value = Value::Error { error }; diff --git a/crates/nu-command/tests/commands/try_.rs b/crates/nu-command/tests/commands/try_.rs index 225bca76da..11a1f72502 100644 --- a/crates/nu-command/tests/commands/try_.rs +++ b/crates/nu-command/tests/commands/try_.rs @@ -49,3 +49,55 @@ fn external_failed_should_be_catched() { assert!(output.out.contains("fail")); } + +#[test] +fn loop_try_break_should_be_successful() { + let output = nu!( + cwd: ".", + "loop { try { echo 'successful'; break } catch { echo 'failed'; continue } }" + ); + + assert_eq!(output.out, "successful"); +} + +#[test] +fn loop_catch_break_should_show_failed() { + let output = nu!( + cwd: ".", + "loop { + try { invalid 1; + continue; } catch { echo 'failed'; break } + } + " + ); + + assert_eq!(output.out, "failed"); +} + +#[test] +fn loop_try_ignores_continue() { + let output = nu!( + cwd: ".", + "mut total = 0; + for i in 0..10 { + try { if ($i mod 2) == 0 { + continue;} + $total += 1 + } catch { echo 'failed'; break } + } + echo $total + " + ); + + assert_eq!(output.out, "5"); +} + +#[test] +fn loop_try_break_on_command_should_show_successful() { + let output = nu!( + cwd: ".", + "loop { try { ls; break } catch { echo 'failed';continue }}" + ); + + assert!(!output.out.contains("failed")); +}