summaryrefslogtreecommitdiff
path: root/src/channel
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-11 22:57:56 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-11 22:57:56 -0400
commit756863f298f9e4277863f9e8758e253c5ae95923 (patch)
tree82a9c6643b360b035f4630f2b1db138d9937c8d8 /src/channel
parent812f1cafe3f8a68bf45b677fade7417c30d92eac (diff)
Return a distinct error when an invite username is in use.
I've also aligned channel creation with this (it's 409 Conflict). To make server setup more distinct, it now returns 503 Service Unavailable if setup has not been completed.
Diffstat (limited to 'src/channel')
-rw-r--r--src/channel/app.rs16
-rw-r--r--src/channel/routes.rs2
2 files changed, 3 insertions, 15 deletions
diff --git a/src/channel/app.rs b/src/channel/app.rs
index 7c0b107..5d6cada 100644
--- a/src/channel/app.rs
+++ b/src/channel/app.rs
@@ -5,7 +5,7 @@ use sqlx::sqlite::SqlitePool;
use super::{repo::Provider as _, Channel, History, Id};
use crate::{
clock::DateTime,
- db::NotFound,
+ db::{Duplicate as _, NotFound as _},
event::{repo::Provider as _, Broadcaster, Event, Sequence},
message::repo::Provider as _,
};
@@ -27,7 +27,7 @@ impl<'a> Channels<'a> {
.channels()
.create(name, &created)
.await
- .map_err(|err| CreateError::from_duplicate_name(err, name))?;
+ .duplicate(|| CreateError::DuplicateName(name.into()))?;
tx.commit().await?;
self.events
@@ -133,18 +133,6 @@ pub enum Error {
DatabaseError(#[from] sqlx::Error),
}
-impl CreateError {
- fn from_duplicate_name(error: sqlx::Error, name: &str) -> Self {
- if let Some(error) = error.as_database_error() {
- if error.is_unique_violation() {
- return Self::DuplicateName(name.into());
- }
- }
-
- Self::from(error)
- }
-}
-
#[derive(Debug, thiserror::Error)]
pub enum InternalError {
#[error(transparent)]
diff --git a/src/channel/routes.rs b/src/channel/routes.rs
index e97c447..eaf7962 100644
--- a/src/channel/routes.rs
+++ b/src/channel/routes.rs
@@ -53,7 +53,7 @@ impl IntoResponse for CreateError {
let Self(error) = self;
match error {
duplicate @ app::CreateError::DuplicateName(_) => {
- (StatusCode::BAD_REQUEST, duplicate.to_string()).into_response()
+ (StatusCode::CONFLICT, duplicate.to_string()).into_response()
}
other => Internal::from(other).into_response(),
}