summaryrefslogtreecommitdiff
path: root/src/channel
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-09-18 01:27:47 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-09-18 12:17:46 -0400
commitcce6662d635bb2115f9f2a7bab92cc105166e761 (patch)
tree9d1edfea364a3b72cf40c78d67ce05e3e68c84df /src/channel
parent921f38a73e5d58a5a6077477a8b52d2705798f55 (diff)
App methods now return errors that allow not-found cases to be distinguished.
Diffstat (limited to 'src/channel')
-rw-r--r--src/channel/app.rs40
1 files changed, 31 insertions, 9 deletions
diff --git a/src/channel/app.rs b/src/channel/app.rs
index 29d9c09..e72564d 100644
--- a/src/channel/app.rs
+++ b/src/channel/app.rs
@@ -16,6 +16,7 @@ use crate::{
error::BoxedError,
repo::{
channel::{self, Channel, Provider as _},
+ error::NotFound as _,
login::Login,
},
};
@@ -30,7 +31,7 @@ impl<'a> Channels<'a> {
Self { db, broadcaster }
}
- pub async fn create(&self, name: &str) -> Result<(), BoxedError> {
+ pub async fn create(&self, name: &str) -> Result<(), InternalError> {
let mut tx = self.db.begin().await?;
let channel = tx.channels().create(name).await?;
self.broadcaster.register_channel(&channel);
@@ -39,7 +40,7 @@ impl<'a> Channels<'a> {
Ok(())
}
- pub async fn all(&self) -> Result<Vec<Channel>, BoxedError> {
+ pub async fn all(&self) -> Result<Vec<Channel>, InternalError> {
let mut tx = self.db.begin().await?;
let channels = tx.channels().all().await?;
tx.commit().await?;
@@ -53,9 +54,13 @@ impl<'a> Channels<'a> {
channel: &channel::Id,
body: &str,
sent_at: &DateTime,
- ) -> Result<(), BoxedError> {
+ ) -> Result<(), EventsError> {
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(|| EventsError::ChannelNotFound(channel.clone()))?;
let message = tx
.broadcast()
.create(login, &channel, body, sent_at)
@@ -70,11 +75,8 @@ impl<'a> Channels<'a> {
&self,
channel: &channel::Id,
resume_at: Option<&DateTime>,
- ) -> Result<impl Stream<Item = Result<broadcast::Message, BoxedError>> + 'static, BoxedError>
+ ) -> Result<impl Stream<Item = Result<broadcast::Message, BoxedError>> + 'static, EventsError>
{
- let mut tx = self.db.begin().await?;
- let channel = tx.channels().by_id(channel).await?;
-
fn skip_stale<E>(
resume_at: Option<&DateTime>,
) -> impl for<'m> FnMut(&'m broadcast::Message) -> future::Ready<Result<bool, E>> {
@@ -86,6 +88,12 @@ impl<'a> Channels<'a> {
}))
}
}
+ let mut tx = self
+ .db
+ .begin()
+ .await
+ .not_found(|| EventsError::ChannelNotFound(channel.clone()))?;
+ let channel = tx.channels().by_id(channel).await?;
let live_messages = self
.broadcaster
@@ -102,6 +110,20 @@ impl<'a> Channels<'a> {
}
}
+#[derive(Debug, thiserror::Error)]
+pub enum InternalError {
+ #[error("database error: {0}")]
+ DatabaseError(#[from] sqlx::Error),
+}
+
+#[derive(Debug, thiserror::Error)]
+pub enum EventsError {
+ #[error("channel {0} not found")]
+ ChannelNotFound(channel::Id),
+ #[error("database error: {0}")]
+ DatabaseError(#[from] sqlx::Error),
+}
+
// Clones will share the same senders collection.
#[derive(Clone)]
pub struct Broadcaster {
@@ -112,7 +134,7 @@ pub struct Broadcaster {
}
impl Broadcaster {
- pub async fn from_database(db: &SqlitePool) -> Result<Self, BoxedError> {
+ pub async fn from_database(db: &SqlitePool) -> Result<Self, sqlx::Error> {
let mut tx = db.begin().await?;
let channels = tx.channels().all().await?;
tx.commit().await?;