-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add test when url is invalid and panic * Initial error handling ref #22 * Rename ErrorKind::HttpClient => ErrorKind::Http * Implement std::error::Error and rename to Error
- Loading branch information
Showing
6 changed files
with
73 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
use reqwest::blocking::{Client, Request}; | ||
use reqwest::{Method, Error}; | ||
use reqwest::Method; | ||
use reqwest::header::HeaderValue; | ||
use url::{Origin, Url}; | ||
use reqwest::header::USER_AGENT; | ||
use crate::http::{RobotsTxtClient, DEFAULT_USER_AGENT}; | ||
use crate::parser::{ParseResult, parse_fetched_robots_txt}; | ||
use crate::model::FetchedRobotsTxt; | ||
use crate::model::{Error, ErrorKind}; | ||
|
||
impl RobotsTxtClient for Client { | ||
type Result = Result<ParseResult<FetchedRobotsTxt>, Error>; | ||
fn fetch_robots_txt(&self, origin: Origin) -> Self::Result { | ||
let url = format!("{}/robots.txt", origin.unicode_serialization()); | ||
let url = Url::parse(&url).expect("Unable to parse robots.txt url"); | ||
let url = Url::parse(&url).map_err(|err| Error {kind: ErrorKind::Url(err)})?; | ||
let mut request = Request::new(Method::GET, url); | ||
let _ = request.headers_mut().insert(USER_AGENT, HeaderValue::from_static(DEFAULT_USER_AGENT)); | ||
let response = self.execute(request)?; | ||
let response = self.execute(request).map_err(|err| Error {kind: ErrorKind::Http(err)})?; | ||
let status_code = response.status().as_u16(); | ||
let text = response.text()?; | ||
let text = response.text().map_err(|err| Error {kind: ErrorKind::Http(err)})?; | ||
let robots_txt = parse_fetched_robots_txt(origin, status_code, &text); | ||
return Ok(robots_txt); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub struct Error { | ||
pub kind: ErrorKind, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ErrorKind { | ||
Url(url::ParseError), | ||
Http(reqwest::Error), | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self.kind { | ||
ErrorKind::Url(ref err) => err.fmt(f), | ||
ErrorKind::Http(ref err) => err.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters