Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
fix: handle error from reading SQL column value without panicing
Lars Wirzenius committed 1 year ago
commit 42b1265a43ca2b3fb461db8d7f49ab71263cb31c
parent ba999b971957fdbdeb48ccd1acb4917a4780d7c1
1 file changed +15 -2
modified src/db.rs
@@ -147,13 +147,19 @@ impl Db {

    /// Return the current value of the counter, if any.
    pub fn get_counter(&self) -> Result<Option<i64>, DbError> {
-
        let mut select = self.prepare("SELECT counter FROM counter_test")?;
+
        let sql = "SELECT counter FROM counter_test";
+
        let mut select = self.prepare(sql)?;
        let mut counter = None;

        loop {
            match select.stmt.next() {
                Ok(State::Row) => {
-
                    counter = Some(select.stmt.read("counter").unwrap());
+
                    counter = Some(
+
                        select
+
                            .stmt
+
                            .read("counter")
+
                            .map_err(|e| DbError::read(sql, e))?,
+
                    );
                }
                Ok(State::Done) => {
                    break;
@@ -529,6 +535,9 @@ pub enum DbError {
    #[error("failed to bind a value in SQL statement in SQLite: {0}")]
    Bind(String, #[source] sqlite::Error),

+
    #[error("failed to read a column value output of SQL statement in SQLite: {0}")]
+
    Read(String, #[source] sqlite::Error),
+

    #[error("failed to insert a counter into database")]
    InsertCounter(String, #[source] sqlite::Error),

@@ -600,6 +609,10 @@ impl DbError {
        Self::Bind(sql.into(), e)
    }

+
    fn read(sql: &str, e: sqlite::Error) -> Self {
+
        Self::Read(sql.into(), e)
+
    }
+

    fn insert_counter(sql: &str, e: sqlite::Error) -> Self {
        Self::InsertCounter(sql.into(), e)
    }