Convert Shellerror::GenericError to named fields (#11230)

# Description

Replace `.to_string()` used in `GenericError` with `.into()` as
`.into()` seems more popular

Replace `Vec::new()` used in `GenericError` with `vec![]` as `vec![]`
seems more popular

(There are so, so many)
This commit is contained in:
Eric Hodel
2023-12-06 15:40:03 -08:00
committed by GitHub
parent b03f1efac4
commit a95a4505ef
160 changed files with 2975 additions and 3228 deletions

View File

@ -155,25 +155,23 @@ fn action(
format!("CREATE TABLE IF NOT EXISTS [{table_name}] ({table_columns_creation})");
// prepare the string as a sqlite statement
let mut stmt = conn.prepare(&create_statement).map_err(|e| {
ShellError::GenericError(
"Failed to prepare SQLite statement".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?;
let mut stmt =
conn.prepare(&create_statement)
.map_err(|e| ShellError::GenericError {
error: "Failed to prepare SQLite statement".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
// execute the statement
stmt.execute([]).map_err(|e| {
ShellError::GenericError(
"Failed to execute SQLite statement".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
stmt.execute([]).map_err(|e| ShellError::GenericError {
error: "Failed to execute SQLite statement".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
// use normal sql to create the table
@ -187,25 +185,23 @@ fn action(
let insert_statement = format!("INSERT INTO [{table_name}] VALUES {table_values}");
// prepare the string as a sqlite statement
let mut stmt = conn.prepare(&insert_statement).map_err(|e| {
ShellError::GenericError(
"Failed to prepare SQLite statement".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?;
let mut stmt =
conn.prepare(&insert_statement)
.map_err(|e| ShellError::GenericError {
error: "Failed to prepare SQLite statement".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
// execute the statement
stmt.execute([]).map_err(|e| {
ShellError::GenericError(
"Failed to execute SQLite statement".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
stmt.execute([]).map_err(|e| ShellError::GenericError {
error: "Failed to execute SQLite statement".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
// and we're done

View File

@ -47,15 +47,15 @@ impl Command for SchemaDb {
let sqlite_db = SQLiteDatabase::try_from_pipeline(input, span)?;
let conn = open_sqlite_db_connection(&sqlite_db, span)?;
let tables = sqlite_db.get_tables(&conn).map_err(|e| {
ShellError::GenericError(
"Error reading tables".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
})?;
let tables = sqlite_db
.get_tables(&conn)
.map_err(|e| ShellError::GenericError {
error: "Error reading tables".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})?;
let mut tables_record = Record::new();
for table in tables {
@ -87,14 +87,12 @@ impl Command for SchemaDb {
}
fn open_sqlite_db_connection(db: &SQLiteDatabase, span: Span) -> Result<Connection, ShellError> {
db.open_connection().map_err(|e| {
ShellError::GenericError(
"Error opening file".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
db.open_connection().map_err(|e| ShellError::GenericError {
error: "Error opening file".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})
}
@ -104,15 +102,15 @@ fn get_table_columns(
table: &DbTable,
span: Span,
) -> Result<Vec<Value>, ShellError> {
let columns = db.get_columns(conn, table).map_err(|e| {
ShellError::GenericError(
"Error getting database columns".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
})?;
let columns = db
.get_columns(conn, table)
.map_err(|e| ShellError::GenericError {
error: "Error getting database columns".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})?;
// a record of column name = column value
let mut column_info = vec![];
@ -136,15 +134,15 @@ fn get_table_constraints(
table: &DbTable,
span: Span,
) -> Result<Vec<Value>, ShellError> {
let constraints = db.get_constraints(conn, table).map_err(|e| {
ShellError::GenericError(
"Error getting DB constraints".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
})?;
let constraints = db
.get_constraints(conn, table)
.map_err(|e| ShellError::GenericError {
error: "Error getting DB constraints".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})?;
let mut constraint_info = vec![];
for constraint in constraints {
constraint_info.push(Value::record(
@ -167,15 +165,15 @@ fn get_table_foreign_keys(
table: &DbTable,
span: Span,
) -> Result<Vec<Value>, ShellError> {
let foreign_keys = db.get_foreign_keys(conn, table).map_err(|e| {
ShellError::GenericError(
"Error getting DB Foreign Keys".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
})?;
let foreign_keys = db
.get_foreign_keys(conn, table)
.map_err(|e| ShellError::GenericError {
error: "Error getting DB Foreign Keys".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})?;
let mut foreign_key_info = vec![];
for fk in foreign_keys {
foreign_key_info.push(Value::record(
@ -197,15 +195,15 @@ fn get_table_indexes(
table: &DbTable,
span: Span,
) -> Result<Vec<Value>, ShellError> {
let indexes = db.get_indexes(conn, table).map_err(|e| {
ShellError::GenericError(
"Error getting DB Indexes".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
})?;
let indexes = db
.get_indexes(conn, table)
.map_err(|e| ShellError::GenericError {
error: "Error getting DB Indexes".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})?;
let mut index_info = vec![];
for index in indexes {
index_info.push(Value::record(

View File

@ -102,15 +102,14 @@ impl SQLiteDatabase {
pub fn query(&self, sql: &Spanned<String>, call_span: Span) -> Result<Value, ShellError> {
let conn = open_sqlite_db(&self.path, call_span)?;
let stream = run_sql_query(conn, sql, self.ctrlc.clone()).map_err(|e| {
ShellError::GenericError(
"Failed to query SQLite database".into(),
e.to_string(),
Some(sql.span),
None,
Vec::new(),
)
})?;
let stream =
run_sql_query(conn, sql, self.ctrlc.clone()).map_err(|e| ShellError::GenericError {
error: "Failed to query SQLite database".into(),
msg: e.to_string(),
span: Some(sql.span),
help: None,
inner: vec![],
})?;
Ok(stream)
}
@ -119,14 +118,12 @@ impl SQLiteDatabase {
if self.path == PathBuf::from(MEMORY_DB) {
open_connection_in_memory_custom()
} else {
Connection::open(&self.path).map_err(|e| {
ShellError::GenericError(
"Failed to open SQLite database from open_connection".into(),
e.to_string(),
None,
None,
Vec::new(),
)
Connection::open(&self.path).map_err(|e| ShellError::GenericError {
error: "Failed to open SQLite database from open_connection".into(),
msg: e.to_string(),
span: None,
help: None,
inner: vec![],
})
}
}
@ -362,14 +359,12 @@ impl CustomValue for SQLiteDatabase {
fn to_base_value(&self, span: Span) -> Result<Value, ShellError> {
let db = open_sqlite_db(&self.path, span)?;
read_entire_sqlite_db(db, span, self.ctrlc.clone()).map_err(|e| {
ShellError::GenericError(
"Failed to read from SQLite database".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
read_entire_sqlite_db(db, span, self.ctrlc.clone()).map_err(|e| ShellError::GenericError {
error: "Failed to read from SQLite database".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
})
}
@ -386,13 +381,13 @@ impl CustomValue for SQLiteDatabase {
let db = open_sqlite_db(&self.path, span)?;
read_single_table(db, _column_name, span, self.ctrlc.clone()).map_err(|e| {
ShellError::GenericError(
"Failed to read from SQLite database".into(),
e.to_string(),
Some(span),
None,
Vec::new(),
)
ShellError::GenericError {
error: "Failed to read from SQLite database".into(),
msg: e.to_string(),
span: Some(span),
help: None,
inner: vec![],
}
})
}
@ -410,14 +405,12 @@ pub fn open_sqlite_db(path: &Path, call_span: Span) -> Result<Connection, ShellE
open_connection_in_memory_custom()
} else {
let path = path.to_string_lossy().to_string();
Connection::open(path).map_err(|e| {
ShellError::GenericError(
"Failed to open SQLite database".into(),
e.to_string(),
Some(call_span),
None,
Vec::new(),
)
Connection::open(path).map_err(|e| ShellError::GenericError {
error: "Failed to open SQLite database".into(),
msg: e.to_string(),
span: Some(call_span),
help: None,
inner: vec![],
})
}
}
@ -611,25 +604,21 @@ mod test {
pub fn open_connection_in_memory_custom() -> Result<Connection, ShellError> {
let flags = OpenFlags::default();
Connection::open_with_flags(MEMORY_DB, flags).map_err(|err| {
ShellError::GenericError(
"Failed to open SQLite custom connection in memory".into(),
err.to_string(),
Some(Span::test_data()),
None,
Vec::new(),
)
Connection::open_with_flags(MEMORY_DB, flags).map_err(|e| ShellError::GenericError {
error: "Failed to open SQLite custom connection in memory".into(),
msg: e.to_string(),
span: Some(Span::test_data()),
help: None,
inner: vec![],
})
}
pub fn open_connection_in_memory() -> Result<Connection, ShellError> {
Connection::open_in_memory().map_err(|err| {
ShellError::GenericError(
"Failed to open SQLite standard connection in memory".into(),
err.to_string(),
Some(Span::test_data()),
None,
Vec::new(),
)
Connection::open_in_memory().map_err(|e| ShellError::GenericError {
error: "Failed to open SQLite standard connection in memory".into(),
msg: e.to_string(),
span: Some(Span::test_data()),
help: None,
inner: vec![],
})
}