From 15c7e1b725e4bee09d66d1b3af247c98411818a0 Mon Sep 17 00:00:00 2001 From: nibon7 Date: Fri, 1 Dec 2023 22:56:06 +0800 Subject: [PATCH] Add OutOfBounds error (#11201) # Description This is a continuation of #11190. Try to add `OutOfBounds` error. It seems that `OutOfBounds` is more accurate than `InvalidRange`. # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-command/src/strings/str_/index_of.rs | 2 +- crates/nu-protocol/src/shell_error.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/strings/str_/index_of.rs b/crates/nu-command/src/strings/str_/index_of.rs index 9287ece98..4c1d89d21 100644 --- a/crates/nu-command/src/strings/str_/index_of.rs +++ b/crates/nu-command/src/strings/str_/index_of.rs @@ -173,7 +173,7 @@ fn action( if s.get(start_index..end_index).is_none() { return Value::error( - ShellError::InvalidRange { + ShellError::OutOfBounds { left_flank: start_index.to_string(), right_flank: end_index.to_string(), span: range_span, diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 02f18730b..f64098506 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -1236,6 +1236,22 @@ This is an internal Nushell error, please file an issue https://github.com/nushe #[label = "cannot spread value"] span: Span, }, + + /// Out of bounds. + /// + /// ## Resolution + /// + /// Make sure the range is within the bounds of the input. + #[error( + "The selected range {left_flank}..{right_flank} is out of the bounds of the provided input" + )] + #[diagnostic(code(nu::shell::out_of_bounds))] + OutOfBounds { + left_flank: String, + right_flank: String, + #[label = "byte index is not a char boundary or is out of bounds of the input"] + span: Span, + }, } // TODO: Implement as From trait