Expose errors
parent: tbd commit: 6b2125c
Showing 9 changed files with 94 insertions and 48 deletions
src/backend/user.rs
@@ -95,8 +95,7 @@ impl UserBackend for UserAuth { | ||
95 | 95 | request.user.username |
96 | 96 | ) |
97 | 97 | .fetch_one(&self.pg_pool.clone()) |
98 | .await | |
99 | .unwrap(); | |
98 | .await?; | |
100 | 99 | |
101 | 100 | Ok(UserBioResponse { bio: db_row.bio }) |
102 | 101 | } |
src/connection/authentication.rs
@@ -8,30 +8,36 @@ use crate::model::authenticated::{AuthenticatedInstance, NetworkMessage, State}; | ||
8 | 8 | use crate::model::authenticated::{Message, MessageHandler}; |
9 | 9 | |
10 | 10 | use super::wrapper::ConnectionState; |
11 | use super::HandlerUnhandled; | |
11 | ||
12 | 12 | |
13 | 13 | pub async fn authentication_handle( |
14 | 14 | message_type: &str, |
15 | 15 | message: &NetworkMessage, |
16 | 16 | state: &ConnectionState, |
17 | ) -> Result<(), Error> { | |
17 | ) -> Result<bool, Error> { | |
18 | 18 | match message_type { |
19 | 19 | "&giterated_daemon::messages::authentication::RegisterAccountRequest" => { |
20 | 20 | register_account_request |
21 | 21 | .handle_message(&message, state) |
22 | .await | |
22 | .await?; | |
23 | ||
24 | Ok(true) | |
23 | 25 | } |
24 | 26 | "&giterated_daemon::messages::authentication::AuthenticationTokenRequest" => { |
25 | 27 | authentication_token_request |
26 | 28 | .handle_message(&message, state) |
27 | .await | |
29 | .await?; | |
30 | ||
31 | Ok(true) | |
28 | 32 | } |
29 | 33 | "&giterated_daemon::messages::authentication::TokenExtensionRequest" => { |
30 | 34 | token_extension_request |
31 | 35 | .handle_message(&message, state) |
32 | .await | |
36 | .await?; | |
37 | ||
38 | Ok(true) | |
33 | 39 | } |
34 | _ => Err(Error::from(HandlerUnhandled)), | |
40 | _ => Ok(false), | |
35 | 41 | } |
36 | 42 | } |
37 | 43 |
src/connection/handshake.rs
@@ -6,7 +6,7 @@ use semver::Version; | ||
6 | 6 | use crate::{ |
7 | 7 | connection::ConnectionError, |
8 | 8 | messages::handshake::{HandshakeFinalize, HandshakeResponse, InitiateHandshake}, |
9 | model::authenticated::{AuthenticatedInstance, Message, MessageHandler, NetworkMessage, State}, | |
9 | model::authenticated::{Message, MessageHandler, NetworkMessage, State}, | |
10 | 10 | validate_version, version, |
11 | 11 | }; |
12 | 12 |
src/connection/repository.rs
@@ -9,35 +9,47 @@ use crate::{ | ||
9 | 9 | model::authenticated::{AuthenticatedUser, Message, MessageHandler, NetworkMessage, State}, |
10 | 10 | }; |
11 | 11 | |
12 | use super::{wrapper::ConnectionState, HandlerUnhandled}; | |
12 | use super::{wrapper::ConnectionState}; | |
13 | 13 | |
14 | 14 | pub async fn repository_handle( |
15 | 15 | message_type: &str, |
16 | 16 | message: &NetworkMessage, |
17 | 17 | state: &ConnectionState, |
18 | ) -> Result<(), Error> { | |
18 | ) -> Result<bool, Error> { | |
19 | 19 | match message_type { |
20 | 20 | "&giterated_daemon::messages::repository::RepositoryCreateRequest" => { |
21 | create_repository.handle_message(&message, state).await | |
21 | create_repository.handle_message(&message, state).await?; | |
22 | ||
23 | Ok(true) | |
22 | 24 | } |
23 | 25 | "&giterated_daemon::messages::repository::RepositoryFileInspectRequest" => { |
24 | 26 | repository_file_inspect |
25 | 27 | .handle_message(&message, state) |
26 | .await | |
28 | .await?; | |
29 | ||
30 | Ok(true) | |
27 | 31 | } |
28 | 32 | "&giterated_daemon::messages::repository::RepositoryInfoRequest" => { |
29 | repository_info.handle_message(&message, state).await | |
33 | repository_info.handle_message(&message, state).await?; | |
34 | ||
35 | Ok(true) | |
30 | 36 | } |
31 | 37 | "&giterated_daemon::messages::repository::RepositoryIssuesCountRequest" => { |
32 | issues_count.handle_message(&message, state).await | |
38 | issues_count.handle_message(&message, state).await?; | |
39 | ||
40 | Ok(true) | |
33 | 41 | } |
34 | 42 | "&giterated_daemon::messages::repository::RepositoryIssueLabelsRequest" => { |
35 | issue_labels.handle_message(&message, state).await | |
43 | issue_labels.handle_message(&message, state).await?; | |
44 | ||
45 | Ok(true) | |
36 | 46 | } |
37 | 47 | "&giterated_daemon::messages::repository::RepositoryIssuesRequest" => { |
38 | issues.handle_message(&message, state).await | |
48 | issues.handle_message(&message, state).await?; | |
49 | ||
50 | Ok(true) | |
39 | 51 | } |
40 | _ => Err(Error::from(HandlerUnhandled)), | |
52 | _ => Ok(false), | |
41 | 53 | } |
42 | 54 | } |
43 | 55 |
src/connection/user.rs
@@ -1,5 +1,5 @@ | ||
1 | 1 | use anyhow::Error; |
2 | use serde_json::Value; | |
2 | ||
3 | 3 | |
4 | 4 | use crate::model::authenticated::AuthenticatedUser; |
5 | 5 | use crate::model::user::User; |
@@ -11,27 +11,35 @@ use crate::{ | ||
11 | 11 | model::authenticated::{Message, MessageHandler, NetworkMessage, State}, |
12 | 12 | }; |
13 | 13 | |
14 | use super::{wrapper::ConnectionState, HandlerUnhandled}; | |
14 | use super::{wrapper::ConnectionState}; | |
15 | 15 | |
16 | 16 | pub async fn user_handle( |
17 | 17 | message_type: &str, |
18 | 18 | message: &NetworkMessage, |
19 | 19 | state: &ConnectionState, |
20 | ) -> Result<(), Error> { | |
20 | ) -> Result<bool, Error> { | |
21 | 21 | match message_type { |
22 | 22 | "&giterated_daemon::messages::user::UserDisplayNameRequest" => { |
23 | display_name.handle_message(&message, state).await | |
23 | display_name.handle_message(&message, state).await?; | |
24 | ||
25 | Ok(true) | |
24 | 26 | } |
25 | 27 | "&giterated_daemon::messages::user::UserDisplayImageRequest" => { |
26 | display_image.handle_message(&message, state).await | |
28 | display_image.handle_message(&message, state).await?; | |
29 | ||
30 | Ok(true) | |
27 | 31 | } |
28 | 32 | "&giterated_daemon::messages::user::UserBioRequest" => { |
29 | bio.handle_message(&message, state).await | |
33 | bio.handle_message(&message, state).await?; | |
34 | ||
35 | Ok(true) | |
30 | 36 | } |
31 | 37 | "&giterated_daemon::messages::user::UserRepositoriesRequest" => { |
32 | repositories.handle_message(&message, state).await | |
38 | repositories.handle_message(&message, state).await?; | |
39 | ||
40 | Ok(true) | |
33 | 41 | } |
34 | _ => Err(Error::from(HandlerUnhandled)), | |
42 | _ => Ok(false), | |
35 | 43 | } |
36 | 44 | } |
37 | 45 |
src/connection/wrapper.rs
@@ -16,6 +16,7 @@ use tokio_tungstenite::{tungstenite::Message, WebSocketStream}; | ||
16 | 16 | use crate::{ |
17 | 17 | authentication::AuthenticationTokenGranter, |
18 | 18 | backend::{RepositoryBackend, UserBackend}, |
19 | messages::error::ConnectionError, | |
19 | 20 | model::{authenticated::NetworkMessage, instance::Instance}, |
20 | 21 | }; |
21 | 22 | |
@@ -77,28 +78,42 @@ pub async fn connection_wrapper( | ||
77 | 78 | let raw = serde_json::from_slice::<Value>(&payload).unwrap(); |
78 | 79 | let message_type = raw.get("message_type").unwrap().as_str().unwrap(); |
79 | 80 | |
80 | if authentication_handle(message_type, &message, &connection_state) | |
81 | .await | |
82 | .is_ok() | |
83 | { | |
84 | continue; | |
85 | } else if repository_handle(message_type, &message, &connection_state) | |
86 | .await | |
87 | .is_ok() | |
88 | { | |
89 | continue; | |
90 | } else if user_handle(message_type, &message, &connection_state) | |
91 | .await | |
92 | .is_ok() | |
93 | { | |
94 | continue; | |
95 | } else { | |
96 | error!( | |
97 | "Message completely unhandled: {}", | |
98 | std::str::from_utf8(&payload).unwrap() | |
99 | ); | |
100 | continue; | |
81 | match authentication_handle(message_type, &message, &connection_state).await { | |
82 | Err(e) => { | |
83 | let _ = connection_state.send(ConnectionError(e.to_string())).await; | |
84 | } | |
85 | Ok(true) => continue, | |
86 | Ok(false) => {} | |
87 | } | |
88 | ||
89 | match repository_handle(message_type, &message, &connection_state).await { | |
90 | Err(e) => { | |
91 | let _ = connection_state.send(ConnectionError(e.to_string())).await; | |
92 | } | |
93 | Ok(true) => continue, | |
94 | Ok(false) => {} | |
101 | 95 | } |
96 | ||
97 | match user_handle(message_type, &message, &connection_state).await { | |
98 | Err(e) => { | |
99 | let _ = connection_state.send(ConnectionError(e.to_string())).await; | |
100 | } | |
101 | Ok(true) => continue, | |
102 | Ok(false) => {} | |
103 | } | |
104 | ||
105 | match authentication_handle(message_type, &message, &connection_state).await { | |
106 | Err(e) => { | |
107 | let _ = connection_state.send(ConnectionError(e.to_string())).await; | |
108 | } | |
109 | Ok(true) => continue, | |
110 | Ok(false) => {} | |
111 | } | |
112 | ||
113 | error!( | |
114 | "Message completely unhandled: {}", | |
115 | std::str::from_utf8(&payload).unwrap() | |
116 | ); | |
102 | 117 | } |
103 | 118 | } |
104 | 119 | Some(Err(e)) => { |
src/messages/error.rs
@@ -0,0 +1,5 @@ | ||
1 | use serde::{Deserialize, Serialize}; | |
2 | ||
3 | #[derive(Debug, Serialize, Deserialize, thiserror::Error)] | |
4 | #[error("error from connection: {0}")] | |
5 | pub struct ConnectionError(pub String); |
src/messages/mod.rs
@@ -5,6 +5,7 @@ use crate::model::user::User; | ||
5 | 5 | |
6 | 6 | pub mod authentication; |
7 | 7 | pub mod discovery; |
8 | pub mod error; | |
8 | 9 | pub mod handshake; |
9 | 10 | pub mod issues; |
10 | 11 | pub mod repository; |
src/model/authenticated.rs
@@ -239,7 +239,7 @@ impl FromMessage<ConnectionState> for AuthenticatedInstance { | ||
239 | 239 | let message: Authenticated<HashMap<String, Value>> = |
240 | 240 | serde_json::from_slice(&network_message).map_err(|e| Error::from(e))?; |
241 | 241 | |
242 | let (instance, signature) = message | |
242 | let (instance, _signature) = message | |
243 | 243 | .source |
244 | 244 | .iter() |
245 | 245 | .filter_map(|auth| { |