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

@@ -3,8 +3,17 @@ use std::sync::Arc;
use crate::{profile, util};
use data_url::DataUrlError;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use tracing_error::InstrumentError;
#[derive(Serialize, Deserialize, Debug, Display)]
#[display("{description}")]
pub struct LabrinthError {
pub error: String,
pub description: String,
}
#[derive(thiserror::Error, Debug)]
pub enum ErrorKind {
#[error("Filesystem error: {0}")]
@@ -56,6 +65,9 @@ pub enum ErrorKind {
#[error("Error fetching URL: {0}")]
FetchError(#[from] reqwest::Error),
#[error("{0}")]
LabrinthError(LabrinthError),
#[error("Websocket error: {0}")]
WSError(#[from] async_tungstenite::tungstenite::Error),

View File

@@ -1,3 +1,4 @@
use crate::ErrorKind;
use crate::data::ModrinthCredentials;
use crate::event::FriendPayload;
use crate::event::emit::emit_friend;
@@ -322,7 +323,7 @@ impl FriendsSocket {
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
semaphore: &FetchSemaphore,
) -> crate::Result<()> {
fetch_advanced(
let result = fetch_advanced(
Method::POST,
&format!("{}friend/{user_id}", env!("MODRINTH_API_URL_V3")),
None,
@@ -332,7 +333,18 @@ impl FriendsSocket {
semaphore,
exec,
)
.await?;
.await;
if let Err(ref e) = result
&& let ErrorKind::LabrinthError(e) = &*e.raw
&& e.error == "not_found"
{
return Err(ErrorKind::OtherError(format!(
"No user found with username \"{user_id}\""
))
.into());
}
result?;
Ok(())
}

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();