fix: handle status codes when not 200

This commit is contained in:
Anna 2018-09-05 00:57:02 -04:00
parent db3c36ab87
commit 118910f69d
4 changed files with 25 additions and 24 deletions

View File

@ -86,12 +86,7 @@ impl<'a> CharacterSearchBuilder<'a> {
}
pub fn send(&self) -> Result<Paginated<CharacterSearchItem>> {
let text = self.scraper.client
.get(self.as_url())
.send()
.map_err(Error::Net)?
.text()
.map_err(Error::Net)?;
let text = self.scraper.text(self.as_url())?;
lodestone_parser::parse_character_search(&text).map_err(Error::Parse)
}

View File

@ -58,12 +58,7 @@ impl<'a> FreeCompanySearchBuilder<'a> {
}
pub fn send(&self) -> Result<Paginated<FreeCompanySearchItem>> {
let text = self.scraper.client
.get(self.as_url())
.send()
.map_err(Error::Net)?
.text()
.map_err(Error::Net)?;
let text = self.scraper.text(self.as_url())?;
lodestone_parser::parse_free_company_search(&text).map_err(Error::Parse)
}

View File

@ -1,7 +1,13 @@
use reqwest::StatusCode;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "not found")]
NotFound,
#[fail(display = "Lodestone responded with an unexpected code: {}", _0)]
UnexpectedResponse(StatusCode),
#[fail(display = "network error: {}", _0)]
Net(reqwest::Error),
#[fail(display = "url parse error: {}", _0)]

View File

@ -7,7 +7,7 @@ use lodestone_parser::models::{
free_company::FreeCompany,
};
use reqwest::Client;
use reqwest::{Client, StatusCode};
use url::Url;
@ -41,14 +41,24 @@ impl LodestoneScraper {
LODESTONE_URL.join(s).map_err(Error::Url)
}
pub fn character(&self, id: u64) -> Result<Character> {
let url = LodestoneScraper::route(&format!("character/{}", id))?;
let text = self.client
crate fn text(&self, url: Url) -> Result<String> {
let mut res = self.client
.get(url)
.send()
.map_err(Error::Net)?
.text()
.map_err(Error::Net)?;
match res.status() {
StatusCode::Ok => {},
StatusCode::NotFound => return Err(Error::NotFound),
x => return Err(Error::UnexpectedResponse(x)),
}
res
.text()
.map_err(Error::Net)
}
pub fn character(&self, id: u64) -> Result<Character> {
let url = LodestoneScraper::route(&format!("character/{}", id))?;
let text = self.text(url)?;
lodestone_parser::parse_character(id, &text).map_err(Error::Parse)
}
@ -58,12 +68,7 @@ impl LodestoneScraper {
pub fn free_company(&self, id: u64) -> Result<FreeCompany> {
let url = LodestoneScraper::route(&format!("freecompany/{}", id))?;
let text = self.client
.get(url)
.send()
.map_err(Error::Net)?
.text()
.map_err(Error::Net)?;
let text = self.text(url)?;
lodestone_parser::parse_free_company(id, &text).map_err(Error::Parse)
}