mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:55:42 +02:00
Convert more ShellError variants to named fields (#11222)
# Description Convert errors to named fields: * NeedsPositiveValue * MissingConfigValue * UnsupportedConfigValue * DowncastNotPossible * NonUtf8Custom * NonUtf8 * DidYouMeanCustom * DidYouMean * ReadingFile * RemoveNotPossible * ChangedModifiedTimeNotPossible * ChangedAccessTimeNotPossible Part of #10700
This commit is contained in:
@ -248,7 +248,13 @@ mod test {
|
||||
span,
|
||||
),
|
||||
Value::nothing(span),
|
||||
Value::error(ShellError::DidYouMean("what?".to_string(), span), span),
|
||||
Value::error(
|
||||
ShellError::DidYouMean {
|
||||
suggestion: "what?".to_string(),
|
||||
span,
|
||||
},
|
||||
span,
|
||||
),
|
||||
Value::cell_path(
|
||||
CellPath {
|
||||
members: vec![PathMember::Int {
|
||||
|
@ -42,17 +42,25 @@ impl SQLiteDatabase {
|
||||
span: Span,
|
||||
ctrlc: Option<Arc<AtomicBool>>,
|
||||
) -> Result<Self, ShellError> {
|
||||
let mut file =
|
||||
File::open(path).map_err(|e| ShellError::ReadingFile(e.to_string(), span))?;
|
||||
let mut file = File::open(path).map_err(|e| ShellError::ReadingFile {
|
||||
msg: e.to_string(),
|
||||
span,
|
||||
})?;
|
||||
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
file.read_exact(&mut buf)
|
||||
.map_err(|e| ShellError::ReadingFile(e.to_string(), span))
|
||||
.map_err(|e| ShellError::ReadingFile {
|
||||
msg: e.to_string(),
|
||||
span,
|
||||
})
|
||||
.and_then(|_| {
|
||||
if buf == SQLITE_MAGIC_BYTES {
|
||||
Ok(SQLiteDatabase::new(path, ctrlc))
|
||||
} else {
|
||||
Err(ShellError::ReadingFile("Not a SQLite file".into(), span))
|
||||
Err(ShellError::ReadingFile {
|
||||
msg: "Not a SQLite file".into(),
|
||||
span,
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -515,7 +523,7 @@ pub fn convert_sqlite_value_to_nu_value(value: ValueRef, span: Span) -> Value {
|
||||
ValueRef::Text(buf) => {
|
||||
let s = match std::str::from_utf8(buf) {
|
||||
Ok(v) => v,
|
||||
Err(_) => return Value::error(ShellError::NonUtf8(span), span),
|
||||
Err(_) => return Value::error(ShellError::NonUtf8 { span }, span),
|
||||
};
|
||||
Value::string(s.to_string(), span)
|
||||
}
|
||||
|
@ -746,14 +746,14 @@ mod windows_helper {
|
||||
&mut find_data,
|
||||
) {
|
||||
Ok(_) => Ok(find_data),
|
||||
Err(e) => Err(ShellError::ReadingFile(
|
||||
format!(
|
||||
Err(e) => Err(ShellError::ReadingFile {
|
||||
msg: format!(
|
||||
"Could not read metadata for '{}':\n '{}'",
|
||||
filename.to_string_lossy(),
|
||||
e
|
||||
),
|
||||
span,
|
||||
)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -393,7 +393,7 @@ fn rm(
|
||||
|
||||
if let Err(e) = result {
|
||||
let msg = format!("Could not delete {:}: {e:}", f.to_string_lossy());
|
||||
Value::error(ShellError::RemoveNotPossible(msg, span), span)
|
||||
Value::error(ShellError::RemoveNotPossible { msg, span }, span)
|
||||
} else if verbose {
|
||||
let msg = if interactive && !confirmed {
|
||||
"not deleted"
|
||||
|
@ -151,12 +151,13 @@ impl Command for Touch {
|
||||
&item,
|
||||
FileTime::from_system_time(date.expect("should be a valid date").into()),
|
||||
) {
|
||||
return Err(ShellError::ChangeModifiedTimeNotPossible(
|
||||
format!("Failed to change the modified time: {err}"),
|
||||
call.positional_nth(index)
|
||||
return Err(ShellError::ChangeModifiedTimeNotPossible {
|
||||
msg: format!("Failed to change the modified time: {err}"),
|
||||
span: call
|
||||
.positional_nth(index)
|
||||
.expect("already checked positional")
|
||||
.span,
|
||||
));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@ -170,12 +171,13 @@ impl Command for Touch {
|
||||
ref_date_atime.expect("should be a valid date").into(),
|
||||
),
|
||||
) {
|
||||
return Err(ShellError::ChangeAccessTimeNotPossible(
|
||||
format!("Failed to change the access time: {err}"),
|
||||
call.positional_nth(index)
|
||||
return Err(ShellError::ChangeAccessTimeNotPossible {
|
||||
msg: format!("Failed to change the access time: {err}"),
|
||||
span: call
|
||||
.positional_nth(index)
|
||||
.expect("already checked positional")
|
||||
.span,
|
||||
));
|
||||
});
|
||||
};
|
||||
} else {
|
||||
// Should not panic as we return an error above if we can't parse the date
|
||||
@ -183,12 +185,13 @@ impl Command for Touch {
|
||||
&item,
|
||||
FileTime::from_system_time(date.expect("should be a valid date").into()),
|
||||
) {
|
||||
return Err(ShellError::ChangeAccessTimeNotPossible(
|
||||
format!("Failed to change the access time: {err}"),
|
||||
call.positional_nth(index)
|
||||
return Err(ShellError::ChangeAccessTimeNotPossible {
|
||||
msg: format!("Failed to change the access time: {err}"),
|
||||
span: call
|
||||
.positional_nth(index)
|
||||
.expect("already checked positional")
|
||||
.span,
|
||||
));
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ impl Command for DropColumn {
|
||||
|
||||
let columns = if let Some(columns) = columns {
|
||||
if columns.item < 0 {
|
||||
return Err(ShellError::NeedsPositiveValue(columns.span));
|
||||
return Err(ShellError::NeedsPositiveValue { span: columns.span });
|
||||
} else {
|
||||
columns.item as usize
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ fn first_helper(
|
||||
// the first N elements is covered by `take`
|
||||
let return_single_element = rows.is_none();
|
||||
let rows_desired: usize = match rows {
|
||||
Some(i) if i < 0 => return Err(ShellError::NeedsPositiveValue(head)),
|
||||
Some(i) if i < 0 => return Err(ShellError::NeedsPositiveValue { span: head }),
|
||||
Some(x) => x as usize,
|
||||
None => 1,
|
||||
};
|
||||
|
@ -77,7 +77,7 @@ impl Command for Last {
|
||||
// It has the same issue.
|
||||
let return_single_element = rows.is_none();
|
||||
let rows_desired: usize = match rows {
|
||||
Some(i) if i < 0 => return Err(ShellError::NeedsPositiveValue(head)),
|
||||
Some(i) if i < 0 => return Err(ShellError::NeedsPositiveValue { span: head }),
|
||||
Some(x) => x as usize,
|
||||
None => 1,
|
||||
};
|
||||
|
@ -145,10 +145,10 @@ fn from_csv(
|
||||
let unicode_sep = u32::from_str_radix(&sep, 16);
|
||||
char::from_u32(unicode_sep.unwrap_or(b'\x1f' as u32)).unwrap_or(',')
|
||||
} else {
|
||||
return Err(ShellError::NonUtf8Custom(
|
||||
"separator should be a single char or a 4-byte unicode".to_string(),
|
||||
call.span(),
|
||||
));
|
||||
return Err(ShellError::NonUtf8Custom {
|
||||
msg: "separator should be a single char or a 4-byte unicode".into(),
|
||||
span: call.span(),
|
||||
});
|
||||
}
|
||||
}
|
||||
None => ',',
|
||||
|
@ -390,7 +390,7 @@ fn to_xml(
|
||||
let s = if let Ok(s) = String::from_utf8(b) {
|
||||
s
|
||||
} else {
|
||||
return Err(ShellError::NonUtf8(head));
|
||||
return Err(ShellError::NonUtf8 { span: head });
|
||||
};
|
||||
Ok(Value::string(s, head).into_pipeline_data())
|
||||
})
|
||||
|
@ -302,16 +302,16 @@ impl UrlComponents {
|
||||
return Ok(true);
|
||||
}
|
||||
match key {
|
||||
"host" => Err(ShellError::UnsupportedConfigValue(
|
||||
"non-empty string".into(),
|
||||
"empty string".into(),
|
||||
value_span,
|
||||
)),
|
||||
"scheme" => Err(ShellError::UnsupportedConfigValue(
|
||||
"non-empty string".into(),
|
||||
"empty string".into(),
|
||||
value_span,
|
||||
)),
|
||||
"host" => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "non-empty string".into(),
|
||||
value: "empty string".into(),
|
||||
span: value_span,
|
||||
}),
|
||||
"scheme" => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "non-empty string".into(),
|
||||
value: "empty string".into(),
|
||||
span: value_span,
|
||||
}),
|
||||
_ => Ok(false),
|
||||
}
|
||||
}
|
||||
|
@ -82,12 +82,16 @@ impl Command for Du {
|
||||
let max_depth: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "max-depth")?;
|
||||
if let Some(ref max_depth) = max_depth {
|
||||
if max_depth.item < 0 {
|
||||
return Err(ShellError::NeedsPositiveValue(max_depth.span));
|
||||
return Err(ShellError::NeedsPositiveValue {
|
||||
span: max_depth.span,
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(ref min_size) = min_size {
|
||||
if min_size.item < 0 {
|
||||
return Err(ShellError::NeedsPositiveValue(min_size.span));
|
||||
return Err(ShellError::NeedsPositiveValue {
|
||||
span: min_size.span,
|
||||
});
|
||||
}
|
||||
}
|
||||
let current_dir = current_dir(engine_state, stack)?;
|
||||
|
Reference in New Issue
Block a user