[fractal] api: Log every channel sender send error
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] api: Log every channel sender send error
- Date: Tue, 10 Sep 2019 15:03:24 +0000 (UTC)
commit c3f299c6177f8d4fa236a469881ef34012a39062
Author: Daniel GarcĂa Moreno <dani danigm net>
Date: Fri Sep 6 09:41:48 2019 +0200
api: Log every channel sender send error
We were ignoring channel send errors and it could be interesting to have
a log about this not sent messages.
This patch adds a new trait to extend the Result<(), SendError<T>> type
and provide a new method called expect_log, that doesn't panic on error
but logs the message in that case.
fractal-matrix-api/src/backend/directory.rs | 13 ++-
fractal-matrix-api/src/backend/media.rs | 28 +++---
fractal-matrix-api/src/backend/mod.rs | 10 ++-
fractal-matrix-api/src/backend/register.rs | 34 +++++---
fractal-matrix-api/src/backend/room.rs | 129 ++++++++++++++++++----------
fractal-matrix-api/src/backend/stickers.rs | 23 +++--
fractal-matrix-api/src/backend/sync.rs | 34 +++++---
fractal-matrix-api/src/backend/user.rs | 102 ++++++++++++++--------
fractal-matrix-api/src/util.rs | 15 +++-
9 files changed, 259 insertions(+), 129 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index b54b8cda..e40e501f 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -9,6 +9,7 @@ use std::thread;
use crate::util::cache_path;
use crate::util::media;
+use crate::util::ResultExpectLog;
use crate::util::HTTP_CLIENT;
use crate::r0::directory::post_public_rooms::request as post_public_rooms;
@@ -47,10 +48,12 @@ pub fn protocols(bk: &Backend) {
.flat_map(|(_, protocol)| protocol.instances.into_iter())
.collect();
- let _ = tx.send(BKResponse::DirectoryProtocols(protocols));
+ tx.send(BKResponse::DirectoryProtocols(protocols))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::DirectoryError(err));
+ tx.send(BKResponse::DirectoryError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -134,10 +137,12 @@ pub fn room_search(
})
.collect();
- let _ = tx.send(BKResponse::DirectorySearch(rooms));
+ tx.send(BKResponse::DirectorySearch(rooms))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::DirectoryError(err));
+ tx.send(BKResponse::DirectoryError(err))
+ .expect_log("Connection closed");
}
}
});
diff --git a/fractal-matrix-api/src/backend/media.rs b/fractal-matrix-api/src/backend/media.rs
index 0af58edc..0fcaf318 100644
--- a/fractal-matrix-api/src/backend/media.rs
+++ b/fractal-matrix-api/src/backend/media.rs
@@ -12,6 +12,7 @@ use crate::util::get_room_media_list;
use crate::util::resolve_media_url;
use crate::util::semaphore;
use crate::util::thumb;
+use crate::util::ResultExpectLog;
use crate::types::Message;
@@ -21,10 +22,10 @@ pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Resul
semaphore(bk.limit_threads.clone(), move || {
match thumb(&baseu, &media, None) {
Ok(fname) => {
- let _ = tx.send(fname);
+ tx.send(fname).expect_log("Connection closed");
}
Err(_) => {
- let _ = tx.send(String::new());
+ tx.send(String::new()).expect_log("Connection closed");
}
};
});
@@ -38,10 +39,10 @@ pub fn get_media_async(bk: &Backend, media: String, tx: Sender<String>) -> Resul
semaphore(bk.limit_threads.clone(), move || {
match util::media(&baseu, &media, None) {
Ok(fname) => {
- let _ = tx.send(fname);
+ tx.send(fname).expect_log("Connection closed");
}
Err(_) => {
- let _ = tx.send(String::new());
+ tx.send(String::new()).expect_log("Connection closed");
}
};
});
@@ -69,10 +70,11 @@ pub fn get_media_list_async(
&prev_batch,
) {
Ok(media_list) => {
- let _ = tx.send(media_list);
+ tx.send(media_list).expect_log("Connection closed");
}
Err(_) => {
- let _ = tx.send((Vec::new(), String::new()));
+ tx.send((Vec::new(), String::new()))
+ .expect_log("Connection closed");
}
});
@@ -86,10 +88,12 @@ pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
thread::spawn(move || {
match util::media(&baseu, &media, None) {
Ok(fname) => {
- let _ = tx.send(BKResponse::Media(fname));
+ tx.send(BKResponse::Media(fname))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::MediaError(err));
+ tx.send(BKResponse::MediaError(err))
+ .expect_log("Connection closed");
}
};
});
@@ -103,10 +107,10 @@ pub fn get_media_url(bk: &Backend, media: String, tx: Sender<String>) -> Result<
semaphore(bk.limit_threads.clone(), move || {
match resolve_media_url(&baseu, &media, false, 0, 0) {
Ok(uri) => {
- let _ = tx.send(uri.to_string());
+ tx.send(uri.to_string()).expect_log("Connection closed");
}
Err(_) => {
- let _ = tx.send(String::new());
+ tx.send(String::new()).expect_log("Connection closed");
}
};
});
@@ -124,10 +128,10 @@ pub fn get_file_async(url: String, tx: Sender<String>) -> Result<(), Error> {
thread::spawn(move || {
match download_file(&url, fname, None) {
Ok(fname) => {
- let _ = tx.send(fname);
+ tx.send(fname).expect_log("Connection closed");
}
Err(_) => {
- let _ = tx.send(String::new());
+ tx.send(String::new()).expect_log("Connection closed");
}
};
});
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index 28495f68..c2f493bb 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -7,6 +7,7 @@ use std::thread;
use url::Url;
use crate::util::client_url;
+use crate::util::ResultExpectLog;
use crate::error::Error;
@@ -212,13 +213,15 @@ impl Backend {
Ok(BKCommand::NewRoom(name, privacy, internalid)) => {
let r = room::new_room(self, &name, privacy, internalid.clone());
if let Err(e) = r {
- let _ = tx.send(BKResponse::NewRoomError(e, internalid));
+ tx.send(BKResponse::NewRoomError(e, internalid))
+ .expect_log("Connection closed");
}
}
Ok(BKCommand::DirectChat(user, internalid)) => {
let r = room::direct_chat(self, &user, internalid.clone());
if let Err(e) = r {
- let _ = tx.send(BKResponse::NewRoomError(e, internalid));
+ tx.send(BKResponse::NewRoomError(e, internalid))
+ .expect_log("Connection closed");
}
}
Ok(BKCommand::AddToFav(roomid, tofav)) => {
@@ -302,7 +305,8 @@ impl Backend {
// Internal commands
Ok(BKCommand::ShutDown) => {
- let _ = tx.send(BKResponse::ShutDown);
+ tx.send(BKResponse::ShutDown)
+ .expect_log("Connection closed");
return false;
}
Err(_) => {
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index d2b7add2..83e0cfc8 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -20,6 +20,7 @@ use crate::r0::account::UserIdentifier;
use crate::r0::server::domain_info::request as domain_info;
use crate::r0::server::domain_info::Response as DomainInfoResponse;
use crate::r0::Medium;
+use crate::util::ResultExpectLog;
use crate::util::HTTP_CLIENT;
use crate::backend::types::BKResponse;
@@ -57,11 +58,14 @@ pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone();
data.lock().unwrap().since = None;
- let _ = tx.send(BKResponse::Token(uid, tk, dev));
- let _ = tx.send(BKResponse::Rooms(vec![], None));
+ tx.send(BKResponse::Token(uid, tk, dev))
+ .expect_log("Connection closed");
+ tx.send(BKResponse::Rooms(vec![], None))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::GuestLoginError(err));
+ tx.send(BKResponse::GuestLoginError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -113,16 +117,19 @@ pub fn login(bk: &Backend, user: String, password: String, server: &str) -> Resu
let dev = response.device_id;
if uid.is_empty() || tk.is_empty() {
- let _ = tx.send(BKResponse::LoginError(Error::BackendError));
+ tx.send(BKResponse::LoginError(Error::BackendError))
+ .expect_log("Connection closed");
} else {
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone();
data.lock().unwrap().since = None;
- let _ = tx.send(BKResponse::Token(uid, tk, dev));
+ tx.send(BKResponse::Token(uid, tk, dev))
+ .expect_log("Connection closed");
}
}
Err(err) => {
- let _ = tx.send(BKResponse::LoginError(err));
+ tx.send(BKResponse::LoginError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -135,7 +142,9 @@ pub fn set_token(bk: &Backend, token: String, uid: String, server: &str) -> Resu
bk.data.lock().unwrap().access_token = token.clone();
bk.data.lock().unwrap().user_id = uid.clone();
bk.data.lock().unwrap().since = None;
- let _ = bk.tx.send(BKResponse::Token(uid, token, None));
+ bk.tx
+ .send(BKResponse::Token(uid, token, None))
+ .expect_log("Connection closed");
Ok(())
}
@@ -164,10 +173,11 @@ pub fn logout(bk: &Backend) {
data.lock().unwrap().user_id = Default::default();
data.lock().unwrap().access_token = Default::default();
data.lock().unwrap().since = None;
- let _ = tx.send(BKResponse::Logout);
+ tx.send(BKResponse::Logout).expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::LogoutError(err));
+ tx.send(BKResponse::LogoutError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -206,10 +216,12 @@ pub fn register(bk: &Backend, user: String, password: String, server: &str) -> R
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone();
data.lock().unwrap().since = None;
- let _ = tx.send(BKResponse::Token(uid, tk, dev));
+ tx.send(BKResponse::Token(uid, tk, dev))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::LoginError(err));
+ tx.send(BKResponse::LoginError(err))
+ .expect_log("Connection closed");
}
}
});
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 683f3e1c..55d1ae62 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -18,6 +18,7 @@ use crate::util::cache_path;
use crate::util::json_q;
use crate::util::put_media;
use crate::util::thumb;
+use crate::util::ResultExpectLog;
use crate::util::{client_url, media_url};
use crate::backend::types::BKCommand;
@@ -55,10 +56,12 @@ pub fn get_room_detail(bk: &Backend, roomid: String, key: String) -> Result<(),
let k = keys.split('.').last().unwrap();
let value = String::from(r[&k].as_str().unwrap_or_default());
- let _ = tx.send(BKResponse::RoomDetail(roomid, key, value));
+ tx.send(BKResponse::RoomDetail(roomid, key, value))
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::RoomDetailError(err));
+ tx.send(BKResponse::RoomDetailError(err))
+ .expect_log("Connection closed");
}
);
@@ -77,16 +80,19 @@ pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
if let Some(ref avatar) = avatar {
let _ = thumb(&baseu, avatar.as_str(), dest.as_ref().map(String::as_str));
}
- let _ = tx.send(BKResponse::RoomAvatar(roomid, avatar));
+ tx.send(BKResponse::RoomAvatar(roomid, avatar))
+ .expect_log("Connection closed");
},
|err: Error| match err {
Error::MatrixError(ref js)
if js["errcode"].as_str().unwrap_or_default() == "M_NOT_FOUND" =>
{
- let _ = tx.send(BKResponse::RoomAvatar(roomid, None));
+ tx.send(BKResponse::RoomAvatar(roomid, None))
+ .expect_log("Connection closed");
}
_ => {
- let _ = tx.send(BKResponse::RoomAvatarError(err));
+ tx.send(BKResponse::RoomAvatarError(err))
+ .expect_log("Connection closed");
}
}
);
@@ -110,10 +116,12 @@ pub fn get_room_members(bk: &Backend, roomid: String) -> Result<(), Error> {
member
})
.collect();
- let _ = tx.send(BKResponse::RoomMembers(roomid, ms));
+ tx.send(BKResponse::RoomMembers(roomid, ms))
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::RoomMembersError(err));
+ tx.send(BKResponse::RoomMembersError(err))
+ .expect_log("Connection closed");
}
);
@@ -146,10 +154,12 @@ pub fn get_room_messages(bk: &Backend, roomid: String, from: String) -> Result<(
let evs = array.unwrap().iter().rev();
let list = Message::from_json_events_iter(&roomid, evs);
let prev_batch = r["end"].as_str().map(String::from);
- let _ = tx.send(BKResponse::RoomMessagesTo(list, roomid, prev_batch));
+ tx.send(BKResponse::RoomMessagesTo(list, roomid, prev_batch))
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::RoomMembersError(err));
+ tx.send(BKResponse::RoomMembersError(err))
+ .expect_log("Connection closed");
}
);
@@ -166,7 +176,8 @@ pub fn get_room_messages_from_msg(bk: &Backend, roomid: String, msg: Message) ->
thread::spawn(move || {
if let Ok(from) = util::get_prev_batch_from(&baseu, &tk, &roomid, &msg.id) {
if let Some(t) = tx {
- let _ = t.send(BKCommand::GetRoomMessages(roomid, from));
+ t.send(BKCommand::GetRoomMessages(roomid, from))
+ .expect_log("Connection closed");
}
}
});
@@ -216,14 +227,17 @@ fn parse_context(
if let Err(err) =
parse_context(tx.clone(), tk, baseu, roomid, &id.unwrap(), limit * 2)
{
- let _ = tx.send(BKResponse::RoomMessagesError(err));
+ tx.send(BKResponse::RoomMessagesError(err))
+ .expect_log("Connection closed");
}
} else {
- let _ = tx.send(BKResponse::RoomMessagesTo(ms, roomid, None));
+ tx.send(BKResponse::RoomMessagesTo(ms, roomid, None))
+ .expect_log("Connection closed");
}
},
|err| {
- let _ = tx.send(BKResponse::RoomMessagesError(err));
+ tx.send(BKResponse::RoomMessagesError(err))
+ .expect_log("Connection closed");
}
);
@@ -278,10 +292,12 @@ pub fn send_msg(bk: &Backend, msg: Message) -> Result<(), Error> {
&attrs,
move |js: JsonValue| {
let evid = js["event_id"].as_str().unwrap_or_default();
- let _ = tx.send(BKResponse::SentMsg(msg.id, evid.to_string()));
+ tx.send(BKResponse::SentMsg(msg.id, evid.to_string()))
+ .expect_log("Connection closed");
},
|_| {
- let _ = tx.send(BKResponse::SendMsgError(Error::SendMsgError(msg.id)));
+ tx.send(BKResponse::SendMsgError(Error::SendMsgError(msg.id)))
+ .expect_log("Connection closed");
}
);
@@ -299,7 +315,8 @@ pub fn send_typing(bk: &Backend, roomid: String) -> Result<(), Error> {
let tx = bk.tx.clone();
query!("put", &url, &attrs, move |_| {}, |err| {
- let _ = tx.send(BKResponse::SendTypingError(err));
+ tx.send(BKResponse::SendTypingError(err))
+ .expect_log("Connection closed");
});
Ok(())
@@ -326,12 +343,14 @@ pub fn redact_msg(bk: &Backend, msg: &Message) -> Result<(), Error> {
&attrs,
move |js: JsonValue| {
let evid = js["event_id"].as_str().unwrap_or_default();
- let _ = tx.send(BKResponse::SentMsgRedaction(msgid, evid.to_string()));
+ tx.send(BKResponse::SentMsgRedaction(msgid, evid.to_string()))
+ .expect_log("Connection closed");
},
|_| {
- let _ = tx.send(BKResponse::SendMsgRedactionError(
+ tx.send(BKResponse::SendMsgRedactionError(
Error::SendMsgRedactionError(msgid),
- ));
+ ))
+ .expect_log("Connection closed");
}
);
@@ -347,10 +366,12 @@ pub fn join_room(bk: &Backend, roomid: String) -> Result<(), Error> {
&url,
move |_: JsonValue| {
data.lock().unwrap().join_to_room = roomid.clone();
- let _ = tx.send(BKResponse::JoinRoom);
+ tx.send(BKResponse::JoinRoom)
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::JoinRoomError(err));
+ tx.send(BKResponse::JoinRoomError(err))
+ .expect_log("Connection closed");
}
);
@@ -364,10 +385,12 @@ pub fn leave_room(bk: &Backend, roomid: &str) -> Result<(), Error> {
post!(
&url,
move |_: JsonValue| {
- let _ = tx.send(BKResponse::LeaveRoom);
+ tx.send(BKResponse::LeaveRoom)
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::LeaveRoomError(err));
+ tx.send(BKResponse::LeaveRoomError(err))
+ .expect_log("Connection closed");
}
);
@@ -386,10 +409,12 @@ pub fn mark_as_read(bk: &Backend, roomid: &str, eventid: &str) -> Result<(), Err
post!(
&url,
move |_: JsonValue| {
- let _ = tx.send(BKResponse::MarkedAsRead(r, e));
+ tx.send(BKResponse::MarkedAsRead(r, e))
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::MarkAsReadError(err));
+ tx.send(BKResponse::MarkAsReadError(err))
+ .expect_log("Connection closed");
}
);
@@ -420,10 +445,12 @@ pub fn set_room_name(bk: &Backend, roomid: &str, name: &str) -> Result<(), Error
&url,
&attrs,
|_| {
- let _ = tx.send(BKResponse::SetRoomName);
+ tx.send(BKResponse::SetRoomName)
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::SetRoomNameError(err));
+ tx.send(BKResponse::SetRoomNameError(err))
+ .expect_log("Connection closed");
}
);
@@ -443,10 +470,12 @@ pub fn set_room_topic(bk: &Backend, roomid: &str, topic: &str) -> Result<(), Err
&url,
&attrs,
|_| {
- let _ = tx.send(BKResponse::SetRoomTopic);
+ tx.send(BKResponse::SetRoomTopic)
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::SetRoomTopicError(err));
+ tx.send(BKResponse::SetRoomTopicError(err))
+ .expect_log("Connection closed");
}
);
@@ -468,7 +497,8 @@ pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), E
thread::spawn(move || {
match put_media(mediaurl.as_str(), contents) {
Err(err) => {
- let _ = tx.send(BKResponse::SetRoomAvatarError(err));
+ tx.send(BKResponse::SetRoomAvatarError(err))
+ .expect_log("Connection closed");
}
Ok(js) => {
let uri = js["content_uri"].as_str().unwrap_or_default();
@@ -477,10 +507,12 @@ pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), E
&roomurl,
&attrs,
|_| {
- let _ = tx.send(BKResponse::SetRoomAvatar);
+ tx.send(BKResponse::SetRoomAvatar)
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::SetRoomAvatarError(err));
+ tx.send(BKResponse::SetRoomAvatarError(err))
+ .expect_log("Connection closed");
}
);
}
@@ -509,7 +541,8 @@ pub fn attach_file(bk: &Backend, mut msg: Message) -> Result<(), Error> {
if thumb != "" {
match upload_file(&tk, &baseu, &thumb) {
Err(err) => {
- let _ = tx.send(BKResponse::AttachFileError(err));
+ tx.send(BKResponse::AttachFileError(err))
+ .expect_log("Connection closed");
}
Ok(thumb_uri) => {
msg.thumb = Some(thumb_uri.to_string());
@@ -524,14 +557,17 @@ pub fn attach_file(bk: &Backend, mut msg: Message) -> Result<(), Error> {
match upload_file(&tk, &baseu, &fname) {
Err(err) => {
- let _ = tx.send(BKResponse::AttachFileError(err));
+ tx.send(BKResponse::AttachFileError(err))
+ .expect_log("Connection closed");
}
Ok(uri) => {
msg.url = Some(uri.to_string());
if let Some(t) = itx {
- let _ = t.send(BKCommand::SendMsg(msg.clone()));
+ t.send(BKCommand::SendMsg(msg.clone()))
+ .expect_log("Connection closed");
}
- let _ = tx.send(BKResponse::AttachedFile(msg));
+ tx.send(BKResponse::AttachedFile(msg))
+ .expect_log("Connection closed");
}
};
});
@@ -584,10 +620,12 @@ pub fn new_room(
let id = String::from(r["room_id"].as_str().unwrap_or_default());
let mut r = Room::new(id, RoomMembership::Joined(RoomTag::None));
r.name = Some(n);
- let _ = tx.send(BKResponse::NewRoom(r, internal_id));
+ tx.send(BKResponse::NewRoom(r, internal_id))
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::NewRoomError(err, internal_id));
+ tx.send(BKResponse::NewRoomError(err, internal_id))
+ .expect_log("Connection closed");
}
);
Ok(())
@@ -658,12 +696,14 @@ pub fn direct_chat(bk: &Backend, user: &Member, internal_id: String) -> Result<(
let mut r = Room::new(id.clone(), RoomMembership::Joined(RoomTag::None));
r.name = m.alias.clone();
r.direct = true;
- let _ = tx.send(BKResponse::NewRoom(r, internal_id));
+ tx.send(BKResponse::NewRoom(r, internal_id))
+ .expect_log("Connection closed");
update_direct_chats(direct_url, data, m.uid.clone(), id);
},
|err| {
- let _ = tx.send(BKResponse::NewRoomError(err, internal_id));
+ tx.send(BKResponse::NewRoomError(err, internal_id))
+ .expect_log("Connection closed");
}
);
@@ -688,10 +728,12 @@ pub fn add_to_fav(bk: &Backend, roomid: String, tofav: bool) -> Result<(), Error
&url,
&attrs,
|_| {
- let _ = tx.send(BKResponse::AddedToFav(roomid.clone(), tofav));
+ tx.send(BKResponse::AddedToFav(roomid.clone(), tofav))
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::AddToFavError(err));
+ tx.send(BKResponse::AddToFavError(err))
+ .expect_log("Connection closed");
}
);
@@ -707,7 +749,8 @@ pub fn invite(bk: &Backend, roomid: &str, userid: &str) -> Result<(), Error> {
let tx = bk.tx.clone();
post!(&url, &attrs, |_| {}, |err| {
- let _ = tx.send(BKResponse::InviteError(err));
+ tx.send(BKResponse::InviteError(err))
+ .expect_log("Connection closed");
});
Ok(())
diff --git a/fractal-matrix-api/src/backend/stickers.rs b/fractal-matrix-api/src/backend/stickers.rs
index f70bc2e7..8cab3085 100644
--- a/fractal-matrix-api/src/backend/stickers.rs
+++ b/fractal-matrix-api/src/backend/stickers.rs
@@ -3,6 +3,7 @@ use serde_json::json;
use crate::backend::BackendData;
use crate::util::json_q;
+use crate::util::ResultExpectLog;
use crate::util::{client_url, scalar_url};
use std::sync::{Arc, Mutex};
use std::thread;
@@ -42,10 +43,12 @@ pub fn list(bk: &Backend) -> Result<(), Error> {
let group = StickerGroup::from_json(sticker_group);
stickers.push(group);
}
- let _ = tx.send(BKResponse::Stickers(stickers));
+ tx.send(BKResponse::Stickers(stickers))
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::StickersError(err));
+ tx.send(BKResponse::StickersError(err))
+ .expect_log("Connection closed");
}
);
@@ -77,7 +80,7 @@ pub fn get_sticker_widget_id(bk: &Backend, then: BKCommand) -> Result<(), Error>
d.lock().unwrap().sticker_widget = widget_id;
if let Some(t) = itx {
- let _ = t.send(then);
+ t.send(then).expect_log("Connection closed");
}
},
|err| {
@@ -92,7 +95,7 @@ pub fn get_sticker_widget_id(bk: &Backend, then: BKCommand) -> Result<(), Error>
}
if let Some(t) = itx {
- let _ = t.send(then);
+ t.send(then).expect_log("Connection closed");
}
}
);
@@ -126,10 +129,12 @@ pub fn send(bk: &Backend, roomid: &str, sticker: &Sticker) -> Result<(), Error>
&attrs,
move |js: JsonValue| {
let evid = js["event_id"].as_str().unwrap_or_default();
- let _ = tx.send(BKResponse::SentMsg(id, evid.to_string()));
+ tx.send(BKResponse::SentMsg(id, evid.to_string()))
+ .expect_log("Connection closed");
},
|_| {
- let _ = tx.send(BKResponse::SendMsgError(Error::SendMsgError(id)));
+ tx.send(BKResponse::SendMsgError(Error::SendMsgError(id)))
+ .expect_log("Connection closed");
}
);
@@ -156,10 +161,12 @@ pub fn purchase(bk: &Backend, group: &StickerGroup) -> Result<(), Error> {
get!(
&url,
|_| if let Some(t) = itx {
- let _ = t.send(BKCommand::ListStickers);
+ t.send(BKCommand::ListStickers)
+ .expect_log("Connection closed");
},
|err| {
- let _ = tx.send(BKResponse::StickersError(err));
+ tx.send(BKResponse::StickersError(err))
+ .expect_log("Connection closed");
}
);
diff --git a/fractal-matrix-api/src/backend/sync.rs b/fractal-matrix-api/src/backend/sync.rs
index d55f27f0..a2c34542 100644
--- a/fractal-matrix-api/src/backend/sync.rs
+++ b/fractal-matrix-api/src/backend/sync.rs
@@ -18,6 +18,7 @@ use crate::types::Room;
use crate::types::RoomMembership;
use crate::types::RoomTag;
use crate::util::parse_m_direct;
+use crate::util::ResultExpectLog;
use log::error;
use reqwest::Client;
@@ -111,7 +112,8 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) {
// New rooms
let rs = Room::from_sync_response(&response, &userid, &base);
- let _ = tx.send(BKResponse::UpdateRooms(rs));
+ tx.send(BKResponse::UpdateRooms(rs))
+ .expect_log("Connection closed");
// Message events
let msgs = join
@@ -121,7 +123,8 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) {
Message::from_json_events_iter(&k, events).into_iter()
})
.collect();
- let _ = tx.send(BKResponse::RoomMessages(msgs));
+ tx.send(BKResponse::RoomMessages(msgs))
+ .expect_log("Connection closed");
// Room notifications
for (k, room) in join.iter() {
@@ -129,7 +132,8 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) {
highlight_count: h,
notification_count: n,
} = room.unread_notifications;
- let _ = tx.send(BKResponse::RoomNotifications(k.clone(), n, h));
+ tx.send(BKResponse::RoomNotifications(k.clone(), n, h))
+ .expect_log("Connection closed");
}
// Typing notifications
@@ -164,7 +168,8 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) {
typing_room
})
.collect();
- let _ = tx.send(BKResponse::UpdateRooms(rooms));
+ tx.send(BKResponse::UpdateRooms(rooms))
+ .expect_log("Connection closed");
// Other events
join.iter()
@@ -191,20 +196,24 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) {
.as_str()
.map(Into::into)
.unwrap_or_default();
- let _ = tx.send(BKResponse::RoomName(ev.room.clone(), name));
+ tx.send(BKResponse::RoomName(ev.room.clone(), name))
+ .expect_log("Connection closed");
}
"m.room.topic" => {
let t = ev.content["topic"]
.as_str()
.map(Into::into)
.unwrap_or_default();
- let _ = tx.send(BKResponse::RoomTopic(ev.room.clone(), t));
+ tx.send(BKResponse::RoomTopic(ev.room.clone(), t))
+ .expect_log("Connection closed");
}
"m.room.avatar" => {
- let _ = tx.send(BKResponse::NewRoomAvatar(ev.room.clone()));
+ tx.send(BKResponse::NewRoomAvatar(ev.room.clone()))
+ .expect_log("Connection closed");
}
"m.room.member" => {
- let _ = tx.send(BKResponse::RoomMemberEvent(ev));
+ tx.send(BKResponse::RoomMemberEvent(ev))
+ .expect_log("Connection closed");
}
"m.sticker" => {
// This event is managed in the room list
@@ -224,19 +233,22 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) {
} else {
None
};
- let _ = tx.send(BKResponse::Rooms(rooms, def));
+ tx.send(BKResponse::Rooms(rooms, def))
+ .expect_log("Connection closed");
}
let next_batch = response.next_batch;
data.lock().unwrap().since = Some(next_batch.clone()).filter(|s| !s.is_empty());
- let _ = tx.send(BKResponse::Sync(next_batch));
+ tx.send(BKResponse::Sync(next_batch))
+ .expect_log("Connection closed");
}
Err(err) => {
// we wait if there's an error to avoid 100% CPU
error!("Sync Error, waiting 10 seconds to respond for the next sync");
thread::sleep(time::Duration::from_secs(10));
- let _ = tx.send(BKResponse::SyncError(err));
+ tx.send(BKResponse::SyncError(err))
+ .expect_log("Connection closed");
}
}
});
diff --git a/fractal-matrix-api/src/backend/user.rs b/fractal-matrix-api/src/backend/user.rs
index 1529430a..715043a6 100644
--- a/fractal-matrix-api/src/backend/user.rs
+++ b/fractal-matrix-api/src/backend/user.rs
@@ -7,6 +7,7 @@ use crate::util::encode_uid;
use crate::util::get_user_avatar;
use crate::util::get_user_avatar_img;
use crate::util::semaphore;
+use crate::util::ResultExpectLog;
use crate::util::HTTP_CLIENT;
use reqwest::header::HeaderValue;
use std::sync::mpsc::Sender;
@@ -80,10 +81,12 @@ pub fn get_username(bk: &Backend) {
match query {
Ok(response) => {
let name = response.displayname.unwrap_or(uid);
- let _ = tx.send(BKResponse::Name(name));
+ tx.send(BKResponse::Name(name))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::UserNameError(err));
+ tx.send(BKResponse::UserNameError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -108,10 +111,10 @@ pub fn get_username_async(bk: &Backend, uid: String, tx: Sender<String>) {
match query {
Ok(response) => {
let name = response.displayname.unwrap_or(uid);
- let _ = tx.send(name);
+ tx.send(name).expect_log("Connection closed");
}
Err(_) => {
- let _ = tx.send(uid);
+ tx.send(uid).expect_log("Connection closed");
}
}
});
@@ -140,10 +143,12 @@ pub fn set_username(bk: &Backend, name: String) {
match query {
Ok(_) => {
- let _ = tx.send(BKResponse::SetUserName(name));
+ tx.send(BKResponse::SetUserName(name))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::SetUserNameError(err));
+ tx.send(BKResponse::SetUserNameError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -169,10 +174,12 @@ pub fn get_threepid(bk: &Backend) {
match query {
Ok(response) => {
- let _ = tx.send(BKResponse::GetThreePID(response.threepids));
+ tx.send(BKResponse::GetThreePID(response.threepids))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::GetThreePIDError(err));
+ tx.send(BKResponse::GetThreePIDError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -205,15 +212,18 @@ pub fn get_email_token(bk: &Backend, identity: String, email: String, client_sec
match query {
Ok(response) => {
- let _ = tx.send(BKResponse::GetTokenEmail(response.sid, client_secret));
+ tx.send(BKResponse::GetTokenEmail(response.sid, client_secret))
+ .expect_log("Connection closed");
}
Err(Error::MatrixError(ref js))
if js["errcode"].as_str().unwrap_or_default() == "M_THREEPID_IN_USE" =>
{
- let _ = tx.send(BKResponse::GetTokenEmailUsed);
+ tx.send(BKResponse::GetTokenEmailUsed)
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::GetTokenEmailError(err));
+ tx.send(BKResponse::GetTokenEmailError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -247,15 +257,18 @@ pub fn get_phone_token(bk: &Backend, identity: String, phone: String, client_sec
match query {
Ok(response) => {
- let _ = tx.send(BKResponse::GetTokenPhone(response.sid, client_secret));
+ tx.send(BKResponse::GetTokenPhone(response.sid, client_secret))
+ .expect_log("Connection closed");
}
Err(Error::MatrixError(ref js))
if js["errcode"].as_str().unwrap_or_default() == "M_THREEPID_IN_USE" =>
{
- let _ = tx.send(BKResponse::GetTokenPhoneUsed);
+ tx.send(BKResponse::GetTokenPhoneUsed)
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::GetTokenPhoneError(err));
+ tx.send(BKResponse::GetTokenPhoneError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -288,10 +301,12 @@ pub fn add_threepid(bk: &Backend, identity: String, client_secret: String, sid:
match query {
Ok(_) => {
- let _ = tx.send(BKResponse::AddThreePID(sid));
+ tx.send(BKResponse::AddThreePID(sid))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::AddThreePIDError(err));
+ tx.send(BKResponse::AddThreePIDError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -321,10 +336,12 @@ pub fn submit_phone_token(bk: &Backend, client_secret: String, sid: String, toke
match query {
Ok(response) => {
let result = Some(sid).filter(|_| response.success);
- let _ = tx.send(BKResponse::SubmitPhoneToken(result, client_secret));
+ tx.send(BKResponse::SubmitPhoneToken(result, client_secret))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::SubmitPhoneTokenError(err));
+ tx.send(BKResponse::SubmitPhoneTokenError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -350,10 +367,12 @@ pub fn delete_three_pid(bk: &Backend, medium: Medium, address: String) {
match query {
Ok(_) => {
- let _ = tx.send(BKResponse::DeleteThreePID);
+ tx.send(BKResponse::DeleteThreePID)
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::DeleteThreePIDError(err));
+ tx.send(BKResponse::DeleteThreePIDError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -386,10 +405,12 @@ pub fn change_password(bk: &Backend, user: String, old_password: String, new_pas
match query {
Ok(_) => {
- let _ = tx.send(BKResponse::ChangePassword);
+ tx.send(BKResponse::ChangePassword)
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::ChangePasswordError(err));
+ tx.send(BKResponse::ChangePasswordError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -421,10 +442,12 @@ pub fn account_destruction(bk: &Backend, user: String, password: String) {
match query {
Ok(_) => {
- let _ = tx.send(BKResponse::AccountDestruction);
+ tx.send(BKResponse::AccountDestruction)
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::AccountDestructionError(err));
+ tx.send(BKResponse::AccountDestructionError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -437,10 +460,12 @@ pub fn get_avatar(bk: &Backend) {
let tx = bk.tx.clone();
thread::spawn(move || match get_user_avatar(&base, &userid) {
Ok((_, fname)) => {
- let _ = tx.send(BKResponse::Avatar(fname));
+ tx.send(BKResponse::Avatar(fname))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::AvatarError(err));
+ tx.send(BKResponse::AvatarError(err))
+ .expect_log("Connection closed");
}
});
}
@@ -455,15 +480,15 @@ pub fn get_avatar_async(bk: &Backend, member: Option<Member>, tx: Sender<String>
bk.limit_threads.clone(),
move || match get_user_avatar_img(&base, &uid, &avatar) {
Ok(fname) => {
- let _ = tx.send(fname);
+ tx.send(fname).expect_log("Connection closed");
}
Err(_) => {
- let _ = tx.send(Default::default());
+ tx.send(Default::default()).expect_log("Connection closed");
}
},
);
} else {
- let _ = tx.send(Default::default());
+ tx.send(Default::default()).expect_log("Connection closed");
}
}
@@ -510,10 +535,12 @@ pub fn set_user_avatar(bk: &Backend, avatar: String) {
match query {
Ok(_) => {
- let _ = tx.send(BKResponse::SetUserAvatar(avatar));
+ tx.send(BKResponse::SetUserAvatar(avatar))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::SetUserAvatarError(err));
+ tx.send(BKResponse::SetUserAvatarError(err))
+ .expect_log("Connection closed");
}
}
});
@@ -529,7 +556,7 @@ pub fn get_user_info_async(bk: &mut Backend, uid: &str, tx: Option<Sender<(Strin
let info = info.clone();
thread::spawn(move || {
let i = info.lock().unwrap().clone();
- let _ = tx.send(i);
+ tx.send(i).expect_log("Connection closed");
});
}
return;
@@ -544,7 +571,7 @@ pub fn get_user_info_async(bk: &mut Backend, uid: &str, tx: Option<Sender<(Strin
match get_user_avatar(&baseu, &u) {
Ok(info) => {
if let Some(tx) = tx.clone() {
- let _ = tx.send(info.clone());
+ tx.send(info.clone()).expect_log("Connection closed");
let mut i = i0.unwrap();
i.0 = info.0;
i.1 = info.1;
@@ -552,7 +579,8 @@ pub fn get_user_info_async(bk: &mut Backend, uid: &str, tx: Option<Sender<(Strin
}
Err(_) => {
if let Some(tx) = tx.clone() {
- let _ = tx.send((String::new(), String::new()));
+ tx.send((String::new(), String::new()))
+ .expect_log("Connection closed");
}
}
};
@@ -586,10 +614,12 @@ pub fn search(bk: &Backend, search_term: String) {
match query {
Ok(response) => {
let users = response.results.into_iter().map(Into::into).collect();
- let _ = tx.send(BKResponse::UserSearch(users));
+ tx.send(BKResponse::UserSearch(users))
+ .expect_log("Connection closed");
}
Err(err) => {
- let _ = tx.send(BKResponse::CommandError(err));
+ tx.send(BKResponse::CommandError(err))
+ .expect_log("Connection closed");
}
}
});
diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs
index f310d74d..8e8e5115 100644
--- a/fractal-matrix-api/src/util.rs
+++ b/fractal-matrix-api/src/util.rs
@@ -15,6 +15,7 @@ use std::fs::create_dir_all;
use std::fs::File;
use std::io::prelude::*;
+use std::sync::mpsc::SendError;
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
@@ -97,7 +98,7 @@ macro_rules! derror {
macro_rules! bkerror {
($result: ident, $tx: ident, $type: expr) => {
if let Err(e) = $result {
- $tx.send($type(e)).unwrap();
+ $tx.send($type(e)).expect_log("Connection closed");
}
};
}
@@ -478,3 +479,15 @@ pub fn get_user_avatar_img(baseu: &Url, userid: &str, avatar: &str) -> Result<St
pub fn encode_uid(userid: &str) -> String {
utf8_percent_encode(userid, USERINFO_ENCODE_SET).collect::<String>()
}
+
+pub trait ResultExpectLog {
+ fn expect_log(&self, log: &str);
+}
+
+impl<T> ResultExpectLog for Result<(), SendError<T>> {
+ fn expect_log(&self, log: &str) {
+ if self.is_err() {
+ error!("{}", log);
+ }
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]