diff --git a/crates/nu-cmd-lang/src/core_commands/try_.rs b/crates/nu-cmd-lang/src/core_commands/try_.rs index 78534e97fa..778aa3822b 100644 --- a/crates/nu-cmd-lang/src/core_commands/try_.rs +++ b/crates/nu-cmd-lang/src/core_commands/try_.rs @@ -2,8 +2,8 @@ use nu_engine::{eval_block, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Block, Closure, Command, EngineState, Stack}; use nu_protocol::{ - Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, - Value, + Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, + Type, Value, }; #[derive(Clone)] @@ -59,17 +59,13 @@ impl Command for Try { match result { Err(error) => { let error = intercept_block_control(error)?; - let err_value = Value::Error { - error: Box::new(error), - }; - handle_catch(err_value, catch_block, engine_state, stack) + let err_record = err_to_record(error, call.head); + handle_catch(err_record, catch_block, engine_state, stack) } Ok(PipelineData::Value(Value::Error { error }, ..)) => { let error = intercept_block_control(*error)?; - let err_value = Value::Error { - error: Box::new(error), - }; - handle_catch(err_value, catch_block, engine_state, stack) + let err_record = err_to_record(error, call.head); + handle_catch(err_record, catch_block, engine_state, stack) } // external command may fail to run Ok(pipeline) => { @@ -145,6 +141,19 @@ fn intercept_block_control(error: ShellError) -> Result } } +/// Convert from `error` to [`Value::Record`] so the error information can be easily accessed in catch. +fn err_to_record(error: ShellError, head: Span) -> Value { + let cols = vec!["msg".to_string(), "debug".to_string(), "raw".to_string()]; + let vals = vec![ + Value::string(error.to_string(), head), + Value::string(format!("{error:?}"), head), + Value::Error { + error: Box::new(error), + }, + ]; + Value::record(cols, vals, head) +} + #[cfg(test)] mod test { use super::*; diff --git a/crates/nu-command/tests/commands/try_.rs b/crates/nu-command/tests/commands/try_.rs index 5e597a789d..0af8df34da 100644 --- a/crates/nu-command/tests/commands/try_.rs +++ b/crates/nu-command/tests/commands/try_.rs @@ -24,7 +24,7 @@ fn try_catch() { fn catch_can_access_error() { let output = nu!( cwd: ".", - "try { foobarbaz } catch { |err| $err }" + "try { foobarbaz } catch { |err| $err | get raw }" ); assert!(output.err.contains("External command failed")); @@ -34,7 +34,7 @@ fn catch_can_access_error() { fn catch_can_access_error_as_dollar_in() { let output = nu!( cwd: ".", - "try { foobarbaz } catch { $in }" + "try { foobarbaz } catch { $in | get raw }" ); assert!(output.err.contains("External command failed")); @@ -101,3 +101,12 @@ fn loop_try_break_on_command_should_show_successful() { assert!(!output.out.contains("failed")); } + +#[test] +fn catch_block_can_use_error_object() { + let output = nu!( + cwd: ".", + "try {1 / 0} catch {|err| print ($err | get msg)}" + ); + assert_eq!(output.out, "Division by zero.") +}