h-data/source.rs: Reduce boilerplate.
This commit is contained in:
parent
4ed70a8011
commit
2d7ba7b246
@ -102,6 +102,14 @@ impl Source {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_err(self, context: &str, code: StatusCode) -> DataError {
|
||||||
|
DataError::HttpStatusError {
|
||||||
|
url: self.uri,
|
||||||
|
status_code: code,
|
||||||
|
context: context.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO match on more stuff
|
// TODO match on more stuff
|
||||||
// 301: Moved Permanently
|
// 301: Moved Permanently
|
||||||
// 304: Up to date Feed, checked with the Etag
|
// 304: Up to date Feed, checked with the Etag
|
||||||
@ -115,75 +123,21 @@ impl Source {
|
|||||||
fn match_status(mut self, res: Response) -> Result<(Self, Response), DataError> {
|
fn match_status(mut self, res: Response) -> Result<(Self, Response), DataError> {
|
||||||
self.update_etag(&res)?;
|
self.update_etag(&res)?;
|
||||||
let code = res.status();
|
let code = res.status();
|
||||||
match code {
|
|
||||||
StatusCode::NotModified => {
|
|
||||||
let err = DataError::HttpStatusError {
|
|
||||||
url: self.uri,
|
|
||||||
status_code: code,
|
|
||||||
context: "304: skipping..".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Err(err);
|
match code {
|
||||||
}
|
StatusCode::NotModified => return Err(self.make_err("304: skipping..", code)),
|
||||||
StatusCode::MovedPermanently => {
|
StatusCode::MovedPermanently => {
|
||||||
error!("Feed was moved permanently.");
|
error!("Feed was moved permanently.");
|
||||||
self.handle_301(&res)?;
|
self.handle_301(&res)?;
|
||||||
|
return Err(self.make_err("301: Feed was moved permanently.", code));
|
||||||
let err = DataError::HttpStatusError {
|
|
||||||
url: self.uri,
|
|
||||||
status_code: code,
|
|
||||||
context: "301: Feed was moved permanently.".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Err(err);
|
|
||||||
}
|
}
|
||||||
StatusCode::TemporaryRedirect => debug!("307: Temporary Redirect."),
|
StatusCode::TemporaryRedirect => debug!("307: Temporary Redirect."),
|
||||||
StatusCode::PermanentRedirect => warn!("308: Permanent Redirect."),
|
StatusCode::PermanentRedirect => warn!("308: Permanent Redirect."),
|
||||||
StatusCode::Unauthorized => {
|
StatusCode::Unauthorized => return Err(self.make_err("401: Unauthorized.", code)),
|
||||||
let err = DataError::HttpStatusError {
|
StatusCode::Forbidden => return Err(self.make_err("403: Forbidden.", code)),
|
||||||
url: self.uri,
|
StatusCode::NotFound => return Err(self.make_err("404: Not found.", code)),
|
||||||
status_code: code,
|
StatusCode::RequestTimeout => return Err(self.make_err("408: Request Timeout.", code)),
|
||||||
context: "401: Unauthorized.".into(),
|
StatusCode::Gone => return Err(self.make_err("410: Feed was deleted..", code)),
|
||||||
};
|
|
||||||
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
StatusCode::Forbidden => {
|
|
||||||
let err = DataError::HttpStatusError {
|
|
||||||
url: self.uri,
|
|
||||||
status_code: code,
|
|
||||||
context: "403: Forbidden.".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
StatusCode::NotFound => {
|
|
||||||
let err = DataError::HttpStatusError {
|
|
||||||
url: self.uri,
|
|
||||||
status_code: code,
|
|
||||||
context: "404: Not found.".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
StatusCode::RequestTimeout => {
|
|
||||||
let err = DataError::HttpStatusError {
|
|
||||||
url: self.uri,
|
|
||||||
status_code: code,
|
|
||||||
context: "408: Request Timeout.".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
StatusCode::Gone => {
|
|
||||||
let err = DataError::HttpStatusError {
|
|
||||||
url: self.uri,
|
|
||||||
status_code: code,
|
|
||||||
context: "410: Feed was deleted..".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
_ => info!("HTTP StatusCode: {}", code),
|
_ => info!("HTTP StatusCode: {}", code),
|
||||||
};
|
};
|
||||||
Ok((self, res))
|
Ok((self, res))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user