Small friends fixes (#4270)

* Ensure that fetch errors are properly propagated

* Handle user not found errors better in add_friend

* Cargo fmt

* Introduce new LabrinthError returnable by fetch_advanced

* Allow enter key to send a friend request
This commit is contained in:
Josiah Glosson
2025-08-29 07:08:26 -07:00
committed by GitHub
parent 8b98087936
commit 8fa01b937d
9 changed files with 149 additions and 135 deletions

View File

@@ -1,5 +1,6 @@
//! Functions for fetching information from the Internet
use super::io::{self, IOError};
use crate::ErrorKind;
use crate::event::LoadingBarId;
use crate::event::emit::emit_loading;
use bytes::Bytes;
@@ -108,32 +109,31 @@ pub async fn fetch_advanced(
let result = req.send().await;
match result {
Ok(x) => {
if x.status().is_server_error() {
if attempt <= FETCH_ATTEMPTS {
continue;
} else {
return Err(crate::Error::from(
crate::ErrorKind::OtherError(
"Server error when fetching content"
.to_string(),
),
));
Ok(resp) => {
if resp.status().is_server_error() && attempt <= FETCH_ATTEMPTS
{
continue;
}
if resp.status().is_client_error()
|| resp.status().is_server_error()
{
let backup_error = resp.error_for_status_ref().unwrap_err();
if let Ok(error) = resp.json().await {
return Err(ErrorKind::LabrinthError(error).into());
}
return Err(backup_error.into());
}
let bytes = if let Some((bar, total)) = &loading_bar {
let length = x.content_length();
let length = resp.content_length();
if let Some(total_size) = length {
use futures::StreamExt;
let mut stream = x.bytes_stream();
let mut stream = resp.bytes_stream();
let mut bytes = Vec::new();
while let Some(item) = stream.next().await {
let chunk = item.or(Err(
crate::error::ErrorKind::NoValueFor(
"fetch bytes".to_string(),
),
))?;
let chunk = item.or(Err(ErrorKind::NoValueFor(
"fetch bytes".to_string(),
)))?;
bytes.append(&mut chunk.to_vec());
emit_loading(
bar,
@@ -145,10 +145,10 @@ pub async fn fetch_advanced(
Ok(bytes::Bytes::from(bytes))
} else {
x.bytes().await
resp.bytes().await
}
} else {
x.bytes().await
resp.bytes().await
};
if let Ok(bytes) = bytes {
@@ -158,7 +158,7 @@ pub async fn fetch_advanced(
if attempt <= FETCH_ATTEMPTS {
continue;
} else {
return Err(crate::ErrorKind::HashError(
return Err(ErrorKind::HashError(
sha1.to_string(),
hash,
)
@@ -194,10 +194,9 @@ pub async fn fetch_mirrors(
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
) -> crate::Result<Bytes> {
if mirrors.is_empty() {
return Err(crate::ErrorKind::InputError(
"No mirrors provided!".to_string(),
)
.into());
return Err(
ErrorKind::InputError("No mirrors provided!".to_string()).into()
);
}
for (index, mirror) in mirrors.iter().enumerate() {
@@ -276,8 +275,8 @@ pub async fn write(
}
pub async fn copy(
src: impl AsRef<std::path::Path>,
dest: impl AsRef<std::path::Path>,
src: impl AsRef<Path>,
dest: impl AsRef<Path>,
semaphore: &IoSemaphore,
) -> crate::Result<()> {
let src: &Path = src.as_ref();