1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Better anticipation of errors

This commit is contained in:
Pierre HUBERT 2020-06-01 17:08:57 +02:00
parent f9a8cd4e82
commit faa31f599a

View File

@ -117,7 +117,7 @@ impl QueryInfo {
/// Query just a row
pub fn query_row<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(self, process_function: F)
-> Result<E, Box<dyn Error>> {
-> Result<E, Box<dyn Error>> {
query_row(self, process_function)
}
@ -230,7 +230,7 @@ impl<'a> RowResult<'a> {
/// Get a MYSQL date as a timestamp
pub fn get_date_as_time(&self, name: &str) -> ResultBoxError<u64> {
let value = self.row.get_opt(self.find_col(name)?);
let value : Value = value.ok_or(ExecError(format!("Could not find date field {} !", name)))??;
let value: Value = value.ok_or(ExecError(format!("Could not find date field {} !", name)))??;
// Check if it is a date
if let Value::Date(year, month, day, hour, minutes, seconds, mic_secs) = value {
@ -248,11 +248,14 @@ impl<'a> RowResult<'a> {
/// Query a single row of the database
pub fn query_row<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(mut info: QueryInfo,
process_function: F) -> Result<E, Box<dyn Error>> {
let table = info.table.clone();
info.limit = 1;
let mut list = query(info, process_function)?;
match list.len() {
0 => Err(ExecError::boxed_new("Request did not return a result!")),
0 => Err(ExecError::boxed_string(
format!("Database query did not return a result: (table: {}) !", table))),
_ => Ok(list.remove(0))
}
}
@ -434,7 +437,7 @@ pub fn delete(query: DeleteQuery) -> ResultBoxError<()> {
get_connection()?.exec_drop(
query_sql,
query.conditions.values().collect::<Vec<&Value>>()
query.conditions.values().collect::<Vec<&Value>>(),
)?;
Ok(())