Skip to content

Commit

Permalink
improve source error message
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Aug 5, 2024
1 parent b6ba64c commit 148e175
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/server/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,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.as_response().into_response())?;
let resp = feed.into_response();
Ok(resp)
}
Expand Down Expand Up @@ -242,7 +243,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
17 changes: 17 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,26 @@ 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 as_response(self) -> (StatusCode, String) {
match self {
Error::HttpStatus(status, url) => {
let body = format!("{}: {}", status, url);
(status, body)
}
Error::Message(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
Error::FeedParse(msg) => (StatusCode::BAD_REQUEST, msg.to_string()),
}
}
}

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

0 comments on commit 148e175

Please sign in to comment.