Friendly error message for access beyond end (#6944)

Adds `ShellError::AccessEmptyContent`
This commit is contained in:
WindSoilder
2022-10-30 01:47:50 +08:00
committed by GitHub
parent 7039602e4d
commit 4f7f6a2932
7 changed files with 46 additions and 11 deletions

View File

@ -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

View File

@ -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()?)),