Skip to content

Commit

Permalink
fix: improve source error message (#138)
Browse files Browse the repository at this point in the history
Previously, if an endpoint failed to fetch the source, the endpoint
would respond with a status 500 and an error message too specific. For
example, it could be a "500: bad url" which is not very clear to the
user that it's the source url that's broken or an url elsewhere.

This commit improves on the error message by providing a more
user-friendly message:

- http request errors from anywhere in the pipeline will show as a 502
error with the failed status code and the requesting url is returned in
the error message
- source errors will indicate it's a source error
  • Loading branch information
shouya authored Aug 5, 2024
1 parent b6ba64c commit 9104cdf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/server/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use axum::response::IntoResponse;
use axum::RequestExt;
use http::header::HOST;
use http::request::Parts;
use http::StatusCode;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
Expand Down Expand Up @@ -207,9 +206,10 @@ impl EndpointService {
async fn handle(self, mut req: Request) -> Result<Response, Response> {
// infallible
let param: EndpointParam = req.extract_parts().await.unwrap();
let feed = self.run(param).await.map_err(|e| {
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
})?;
let feed = self
.run(param)
.await
.map_err(|e| e.into_http().into_response())?;
let resp = feed.into_response();
Ok(resp)
}
Expand Down Expand Up @@ -242,7 +242,8 @@ impl EndpointService {
let source = self.find_source(&param.source)?;
let feed = source
.fetch_feed(Some(&self.client), param.base.as_ref())
.await?;
.await
.map_err(|e| Error::FetchSource(Box::new(e)))?;
let mut context = FilterContext::new();
if let Some(limit_filters) = param.limit_filters {
context.set_limit_filters(limit_filters);
Expand Down
20 changes: 20 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use http::StatusCode;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -118,10 +119,29 @@ pub enum Error {
#[error("Unsupported feed format: {0}")]
UnsupportedFeedFormat(String),

#[error("Failed fetching source: {0}")]
FetchSource(Box<Error>),

#[error("{0}")]
Message(String),
}

impl Error {
pub fn into_http(self) -> (StatusCode, String) {
match self {
Error::FetchSource(e) => {
let (status, body) = e.into_http();
(status, format!("Error fetching source: {body}"))
}
Error::HttpStatus(status, url) => {
let body = format!("Error requesting {url}: {status}");
(StatusCode::BAD_GATEWAY, body)
}
_ => (StatusCode::INTERNAL_SERVER_ERROR, format!("{self}")),
}
}
}

#[derive(
JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash,
)]
Expand Down

0 comments on commit 9104cdf

Please sign in to comment.