feat(registry): log errors with more details

This commit is contained in:
daimond113 2024-11-10 20:11:10 +01:00
parent f1c9cbc9fd
commit 19aa5eb52c
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
6 changed files with 75 additions and 8 deletions

View file

@ -1,4 +1,7 @@
use crate::auth::{get_token_from_req, AuthImpl, UserId}; use crate::{
auth::{get_token_from_req, AuthImpl, UserId},
error::ReqwestErrorExt,
};
use actix_web::{dev::ServiceRequest, Error as ActixError}; use actix_web::{dev::ServiceRequest, Error as ActixError};
use reqwest::StatusCode; use reqwest::StatusCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -41,8 +44,11 @@ impl AuthImpl for GitHubAuth {
Err(e) if e.status().is_some_and(|s| s == StatusCode::UNAUTHORIZED) => { Err(e) if e.status().is_some_and(|s| s == StatusCode::UNAUTHORIZED) => {
return Ok(None); return Ok(None);
} }
Err(e) => { Err(_) => {
log::error!("failed to get user: {e}"); log::error!(
"failed to get user: {}",
response.into_error().await.unwrap_err()
);
return Ok(None); return Ok(None);
} }
}, },

View file

@ -14,6 +14,7 @@ use actix_web::{
web, HttpMessage, HttpResponse, web, HttpMessage, HttpResponse,
}; };
use pesde::source::pesde::IndexConfig; use pesde::source::pesde::IndexConfig;
use sentry::add_breadcrumb;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::fmt::Display; use std::fmt::Display;
@ -114,6 +115,13 @@ pub async fn write_mw(
} }
}; };
add_breadcrumb(sentry::Breadcrumb {
category: Some("auth".into()),
message: Some(format!("write request authorized as {}", user_id.0)),
level: sentry::Level::Info,
..Default::default()
});
req.extensions_mut().insert(user_id); req.extensions_mut().insert(user_id);
next.call(req).await.map(|res| res.map_into_left_body()) next.call(req).await.map(|res| res.map_into_left_body())
@ -134,6 +142,13 @@ pub async fn read_mw(
} }
}; };
add_breadcrumb(sentry::Breadcrumb {
category: Some("auth".into()),
message: Some(format!("read request authorized as {}", user_id.0)),
level: sentry::Level::Info,
..Default::default()
});
req.extensions_mut().insert(Some(user_id)); req.extensions_mut().insert(Some(user_id));
} else { } else {
req.extensions_mut().insert(None::<UserId>); req.extensions_mut().insert(None::<UserId>);

View file

@ -24,6 +24,7 @@ use pesde::{
}, },
MANIFEST_FILE_NAME, MANIFEST_FILE_NAME,
}; };
use sentry::add_breadcrumb;
use serde::Deserialize; use serde::Deserialize;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::{ use std::{
@ -268,6 +269,20 @@ pub async fn publish_package(
return Err(Error::InvalidArchive); return Err(Error::InvalidArchive);
}; };
add_breadcrumb(sentry::Breadcrumb {
category: Some("publish".into()),
message: Some(format!(
"publish request for {}@{} {}. has readme: {}. docs: {}",
manifest.name,
manifest.version,
manifest.target,
readme.is_some(),
docs_pages.len()
)),
level: sentry::Level::Info,
..Default::default()
});
{ {
let dependencies = manifest let dependencies = manifest
.all_dependencies() .all_dependencies()

View file

@ -15,6 +15,9 @@ pub enum Error {
#[error("error deserializing file")] #[error("error deserializing file")]
Deserialize(#[from] toml::de::Error), Deserialize(#[from] toml::de::Error),
#[error("failed to send request: {1}\nserver response: {0}")]
ReqwestResponse(String, #[source] reqwest::Error),
#[error("error sending request")] #[error("error sending request")]
Reqwest(#[from] reqwest::Error), Reqwest(#[from] reqwest::Error),
@ -69,3 +72,18 @@ impl ResponseError for Error {
} }
} }
} }
pub trait ReqwestErrorExt {
async fn into_error(self) -> Result<Self, Error>
where
Self: Sized;
}
impl ReqwestErrorExt for reqwest::Response {
async fn into_error(self) -> Result<Self, Error> {
match self.error_for_status_ref() {
Ok(_) => Ok(self),
Err(e) => Err(Error::ReqwestResponse(self.text().await?, e)),
}
}
}

View file

@ -144,7 +144,7 @@ async fn run() -> std::io::Result<()> {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap(sentry_actix::Sentry::new()) .wrap(sentry_actix::Sentry::with_transaction())
.wrap(NormalizePath::new(TrailingSlash::Trim)) .wrap(NormalizePath::new(TrailingSlash::Trim))
.wrap(Cors::permissive()) .wrap(Cors::permissive())
.wrap(Logger::default()) .wrap(Logger::default())
@ -205,11 +205,18 @@ fn main() -> std::io::Result<()> {
let guard = sentry::init(sentry::ClientOptions { let guard = sentry::init(sentry::ClientOptions {
release: sentry::release_name!(), release: sentry::release_name!(),
dsn: benv!(parse "SENTRY_DSN").ok(),
session_mode: sentry::SessionMode::Request,
traces_sample_rate: 1.0,
debug: true,
..Default::default() ..Default::default()
}); });
if guard.is_enabled() { if guard.is_enabled() {
std::env::set_var("RUST_BACKTRACE", "full"); std::env::set_var("RUST_BACKTRACE", "full");
info!("sentry initialized");
} else {
info!("sentry **NOT** initialized");
} }
System::new().block_on(run()) System::new().block_on(run())

View file

@ -1,4 +1,7 @@
use crate::{error::Error, storage::StorageImpl}; use crate::{
error::{Error, ReqwestErrorExt},
storage::StorageImpl,
};
use actix_web::{http::header::LOCATION, HttpResponse}; use actix_web::{http::header::LOCATION, HttpResponse};
use pesde::{names::PackageName, source::version_id::VersionId}; use pesde::{names::PackageName, source::version_id::VersionId};
use reqwest::header::{CONTENT_ENCODING, CONTENT_TYPE}; use reqwest::header::{CONTENT_ENCODING, CONTENT_TYPE};
@ -42,7 +45,8 @@ impl StorageImpl for S3Storage {
.body(contents) .body(contents)
.send() .send()
.await? .await?
.error_for_status()?; .into_error()
.await?;
Ok(()) Ok(())
} }
@ -92,7 +96,8 @@ impl StorageImpl for S3Storage {
.body(contents) .body(contents)
.send() .send()
.await? .await?
.error_for_status()?; .into_error()
.await?;
Ok(()) Ok(())
} }
@ -134,7 +139,8 @@ impl StorageImpl for S3Storage {
.body(contents) .body(contents)
.send() .send()
.await? .await?
.error_for_status()?; .into_error()
.await?;
Ok(()) Ok(())
} }