chore(rust): correctly format values in errors

This commit is contained in:
alyssa 2025-05-17 15:05:37 +00:00
parent 347add8998
commit 7737850afb
15 changed files with 54 additions and 62 deletions

View file

@ -38,8 +38,8 @@ async fn real_main() -> anyhow::Result<()> {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
match cleanup_job(pool.clone(), bucket.clone()).await {
Ok(()) => {}
Err(err) => {
error!("failed to run avatar cleanup job: {}", err);
Err(error) => {
error!(?error, "failed to run avatar cleanup job");
// sentry
}
}

View file

@ -232,26 +232,11 @@ async fn real_main() -> anyhow::Result<()> {
Ok(())
}
struct AppError(anyhow::Error);
#[derive(Serialize)]
struct ErrorResponse {
error: String,
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
error!("error handling request: {}", self.0);
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
error: self.0.to_string(),
}),
)
.into_response()
}
}
impl IntoResponse for PKAvatarError {
fn into_response(self) -> Response {
let status_code = match self {
@ -278,12 +263,3 @@ impl IntoResponse for PKAvatarError {
.into_response()
}
}
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}

View file

@ -129,9 +129,9 @@ pub async fn worker(worker_id: u32, state: Arc<AppState>) {
Ok(()) => {}
Err(e) => {
error!(
"error in migrate worker {}: {}",
worker_id,
e.source().unwrap_or(&e)
error = e.source().unwrap_or(&e)
?worker_id,
"error in migrate worker",
);
tokio::time::sleep(Duration::from_secs(5)).await;
}

View file

@ -84,7 +84,7 @@ pub fn process(data: &[u8], kind: ImageKind) -> Result<ProcessOutput, PKAvatarEr
} else {
reader.decode().map_err(|e| {
// print the ugly error, return the nice error
error!("error decoding image: {}", e);
error!(error = format!("{e:#?}"), "error decoding image");
PKAvatarError::ImageFormatError(e)
})?
};

View file

@ -41,7 +41,11 @@ pub async fn pull(
}
}
error!("network error for {}: {}", parsed_url.full_url, s);
error!(
url = parsed_url.full_url,
error = s,
"network error pulling image"
);
PKAvatarError::NetworkErrorString(s)
})?;
let time_after_headers = Instant::now();
@ -82,7 +86,22 @@ pub async fn pull(
.map(|x| x.to_string());
let body = response.bytes().await.map_err(|e| {
error!("network error for {}: {}", parsed_url.full_url, e);
// terrible
let mut s = format!("{}", e);
if let Some(src) = e.source() {
let _ = write!(s, ": {}", src);
let mut err = src;
while let Some(src) = err.source() {
let _ = write!(s, ": {}", src);
err = src;
}
}
error!(
url = parsed_url.full_url,
error = s,
"network error pulling image"
);
PKAvatarError::NetworkError(e)
})?;
if body.len() != size as usize {