Skip to content

Commit

Permalink
fix(bonfire): make error handling consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Nov 11, 2024
1 parent bf0fc50 commit b9ae333
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
9 changes: 7 additions & 2 deletions crates/bonfire/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
// Try to authenticate the user.
let Some(token) = config.get_session_token().as_ref() else {
write
.send(config.encode(&create_error!(InvalidSession)))
.send(config.encode(&EventV1::Error {
data: create_error!(InvalidSession),
}))
.await
.ok();
return;
Expand All @@ -88,7 +90,10 @@ pub async fn client(db: &'static Database, stream: TcpStream, addr: SocketAddr)
let (user, session_id) = match User::from_token(db, token, UserHint::Any).await {
Ok(user) => user,
Err(err) => {
write.send(config.encode(&err)).await.ok();
write
.send(config.encode(&EventV1::Error { data: err }))
.await
.ok();
return;
}
};
Expand Down
24 changes: 3 additions & 21 deletions crates/core/database/src/events/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use authifier::AuthifierEvent;
use revolt_result::Error;
use serde::{Deserialize, Serialize};

use revolt_models::v0::{
Expand All @@ -7,22 +8,9 @@ use revolt_models::v0::{
PartialChannel, PartialMember, PartialMessage, PartialRole, PartialServer, PartialUser,
PartialWebhook, RemovalIntention, Report, Server, User, UserSettings, Webhook,
};
use revolt_result::Error;

use crate::Database;

/// WebSocket Client Errors
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "error")]
pub enum WebSocketError {
LabelMe,
InternalError { at: String },
InvalidSession,
OnboardingNotFinished,
AlreadyAuthenticated,
MalformedData { msg: String },
}

/// Ping Packet
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
Expand All @@ -31,14 +19,6 @@ pub enum Ping {
Number(usize),
}

/// Untagged Error
#[derive(Serialize)]
#[serde(untagged)]
pub enum ErrorEvent {
Error(WebSocketError),
APIError(Error),
}

/// Fields provided in Ready payload
#[derive(PartialEq)]
pub enum ReadyPayloadFields {
Expand All @@ -58,6 +38,8 @@ pub enum ReadyPayloadFields {
pub enum EventV1 {
/// Multiple events
Bulk { v: Vec<EventV1> },
/// Error event
Error { data: Error },

/// Successfully authenticated
Authenticated,
Expand Down

0 comments on commit b9ae333

Please sign in to comment.