mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:25:38 +02:00
Friendly error message for access beyond end (#6944)
Adds `ShellError::AccessEmptyContent`
This commit is contained in:
@ -358,6 +358,15 @@ Either make sure {0} is a string, or add a 'to_string' entry for it in ENV_CONVE
|
||||
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
|
||||
AccessBeyondEnd(usize, #[label = "index too large (max: {0})"] Span),
|
||||
|
||||
/// You attempted to access an index when it's empty.
|
||||
///
|
||||
/// ## Resolution
|
||||
///
|
||||
/// Check your lengths and try again.
|
||||
#[error("Row number too large (empty content).")]
|
||||
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
|
||||
AccessEmptyContent(#[label = "index too large (empty content)"] Span),
|
||||
|
||||
/// You attempted to access an index beyond the available length of a stream.
|
||||
///
|
||||
/// ## Resolution
|
||||
|
@ -641,8 +641,10 @@ impl Value {
|
||||
Value::List { vals: val, .. } => {
|
||||
if let Some(item) = val.get(*count) {
|
||||
current = item.clone();
|
||||
} else if val.is_empty() {
|
||||
return Err(ShellError::AccessEmptyContent(*origin_span))
|
||||
} else {
|
||||
return Err(ShellError::AccessBeyondEnd(val.len(), *origin_span));
|
||||
return Err(ShellError::AccessBeyondEnd(val.len() - 1, *origin_span));
|
||||
}
|
||||
}
|
||||
Value::Binary { val, .. } => {
|
||||
@ -651,8 +653,10 @@ impl Value {
|
||||
val: *item as i64,
|
||||
span: *origin_span,
|
||||
};
|
||||
} else if val.is_empty() {
|
||||
return Err(ShellError::AccessEmptyContent(*origin_span))
|
||||
} else {
|
||||
return Err(ShellError::AccessBeyondEnd(val.len(), *origin_span));
|
||||
return Err(ShellError::AccessBeyondEnd(val.len() - 1, *origin_span));
|
||||
}
|
||||
}
|
||||
Value::Range { val, .. } => {
|
||||
@ -839,8 +843,10 @@ impl Value {
|
||||
Value::List { vals, .. } => {
|
||||
if let Some(v) = vals.get_mut(*row_num) {
|
||||
v.upsert_data_at_cell_path(&cell_path[1..], new_val)?
|
||||
} else if vals.is_empty() {
|
||||
return Err(ShellError::AccessEmptyContent(*span));
|
||||
} else {
|
||||
return Err(ShellError::AccessBeyondEnd(vals.len(), *span));
|
||||
return Err(ShellError::AccessBeyondEnd(vals.len() - 1, *span));
|
||||
}
|
||||
}
|
||||
v => return Err(ShellError::NotAList(*span, v.span()?)),
|
||||
@ -931,8 +937,10 @@ impl Value {
|
||||
Value::List { vals, .. } => {
|
||||
if let Some(v) = vals.get_mut(*row_num) {
|
||||
v.update_data_at_cell_path(&cell_path[1..], new_val)?
|
||||
} else if vals.is_empty() {
|
||||
return Err(ShellError::AccessEmptyContent(*span));
|
||||
} else {
|
||||
return Err(ShellError::AccessBeyondEnd(vals.len(), *span));
|
||||
return Err(ShellError::AccessBeyondEnd(vals.len() - 1, *span));
|
||||
}
|
||||
}
|
||||
v => return Err(ShellError::NotAList(*span, v.span()?)),
|
||||
@ -1005,8 +1013,10 @@ impl Value {
|
||||
if vals.get_mut(*row_num).is_some() {
|
||||
vals.remove(*row_num);
|
||||
Ok(())
|
||||
} else if vals.is_empty() {
|
||||
Err(ShellError::AccessEmptyContent(*span))
|
||||
} else {
|
||||
Err(ShellError::AccessBeyondEnd(vals.len(), *span))
|
||||
Err(ShellError::AccessBeyondEnd(vals.len() - 1, *span))
|
||||
}
|
||||
}
|
||||
v => Err(ShellError::NotAList(*span, v.span()?)),
|
||||
@ -1069,8 +1079,10 @@ impl Value {
|
||||
Value::List { vals, .. } => {
|
||||
if let Some(v) = vals.get_mut(*row_num) {
|
||||
v.remove_data_at_cell_path(&cell_path[1..])
|
||||
} else if vals.is_empty() {
|
||||
Err(ShellError::AccessEmptyContent(*span))
|
||||
} else {
|
||||
Err(ShellError::AccessBeyondEnd(vals.len(), *span))
|
||||
Err(ShellError::AccessBeyondEnd(vals.len() - 1, *span))
|
||||
}
|
||||
}
|
||||
v => Err(ShellError::NotAList(*span, v.span()?)),
|
||||
@ -1157,8 +1169,10 @@ impl Value {
|
||||
Value::List { vals, .. } => {
|
||||
if let Some(v) = vals.get_mut(*row_num) {
|
||||
v.insert_data_at_cell_path(&cell_path[1..], new_val)?
|
||||
} else if vals.is_empty() {
|
||||
return Err(ShellError::AccessEmptyContent(*span));
|
||||
} else {
|
||||
return Err(ShellError::AccessBeyondEnd(vals.len(), *span));
|
||||
return Err(ShellError::AccessBeyondEnd(vals.len() - 1, *span));
|
||||
}
|
||||
}
|
||||
v => return Err(ShellError::NotAList(*span, v.span()?)),
|
||||
|
Reference in New Issue
Block a user