From cce6662d635bb2115f9f2a7bab92cc105166e761 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 18 Sep 2024 01:27:47 -0400 Subject: App methods now return errors that allow not-found cases to be distinguished. --- src/index/app.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/index') diff --git a/src/index/app.rs b/src/index/app.rs index a4ef57f..a3456c0 100644 --- a/src/index/app.rs +++ b/src/index/app.rs @@ -1,8 +1,8 @@ use sqlx::sqlite::SqlitePool; -use crate::{ - error::BoxedError, - repo::channel::{self, Channel, Provider as _}, +use crate::repo::{ + channel::{self, Channel, Provider as _}, + error::NotFound as _, }; pub struct Index<'a> { @@ -14,11 +14,23 @@ impl<'a> Index<'a> { Self { db } } - pub async fn channel(&self, channel: &channel::Id) -> Result { + pub async fn channel(&self, channel: &channel::Id) -> Result { let mut tx = self.db.begin().await?; - let channel = tx.channels().by_id(channel).await?; + let channel = tx + .channels() + .by_id(channel) + .await + .not_found(|| Error::ChannelNotFound(channel.clone()))?; tx.commit().await?; Ok(channel) } } + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("channel {0} not found")] + ChannelNotFound(channel::Id), + #[error("database error: {0}")] + DatabaseError(#[from] sqlx::Error), +} -- cgit v1.2.3