From eab31e5e913f4a05c5590cbe8a28de168400740c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 4 Apr 2021 13:03:08 +0200 Subject: [PATCH 1/2] [#9] Added general error module --- src/errors.rs | 23 +++++++++++++++++++++++ src/ivago/controller/search.rs | 3 ++- src/ivago/mod.rs | 7 +++---- src/main.rs | 1 + 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/errors.rs diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..6403235 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,23 @@ +use reqwest::Error; +use rocket::http::Status; + +pub enum FejError { + InvalidArgument, + FailedRequest, +} + +impl From for Status { + fn from(err: FejError) -> Status { + match err { + FejError::InvalidArgument => Status::BadRequest, + FejError::FailedRequest => Status::InternalServerError, + } + } +} + +// TODO make this more advanced where possible +impl From for FejError { + fn from(_: Error) -> FejError { + FejError::FailedRequest + } +} diff --git a/src/ivago/controller/search.rs b/src/ivago/controller/search.rs index be81254..926d282 100644 --- a/src/ivago/controller/search.rs +++ b/src/ivago/controller/search.rs @@ -1,3 +1,4 @@ +use crate::errors::FejError; use regex::Regex; use reqwest::blocking as reqwest; use rocket::http::RawStr; @@ -73,7 +74,7 @@ pub struct Street { /// * `street` - name of the street /// * `city` - city the street is in // TODO find out how to do this async -pub fn search_streets(street_name: &String) -> Result, Box> { +pub fn search_streets(street_name: &String) -> Result, FejError> { let client = reqwest::Client::new(); let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?; let data: Vec> = response.json()?; diff --git a/src/ivago/mod.rs b/src/ivago/mod.rs index 33ab64e..c173aab 100644 --- a/src/ivago/mod.rs +++ b/src/ivago/mod.rs @@ -2,6 +2,7 @@ mod controller; #[cfg(test)] mod tests; +use crate::errors::FejError; use rocket::http::Status; use rocket_contrib::json::Json; @@ -12,10 +13,8 @@ pub fn routes() -> Vec { // URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange #[get("/search?")] pub fn route_search_streets(street: String) -> Result>, Status> { - match controller::search_streets(&street) { - Ok(streets) => Ok(Json(streets)), - Err(_) => Err(Status::InternalServerError), - } + let result = controller::search_streets(&street)?; + Ok(Json(result)) } #[get("/?&&&")] diff --git a/src/main.rs b/src/main.rs index 8a5ea07..882db7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate rocket; // Route modules +mod errors; mod hello; mod ivago; From df6e245030e62d1bcae1f6bdfac2d56d9a5520b8 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 4 Apr 2021 14:14:14 +0200 Subject: [PATCH 2/2] [#9] Moved calendar function to new errors system --- src/ivago/controller/pickup_times.rs | 6 +++--- src/ivago/controller/search.rs | 1 - src/ivago/mod.rs | 13 ++++--------- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/ivago/controller/pickup_times.rs b/src/ivago/controller/pickup_times.rs index 58d3f29..202f8fc 100644 --- a/src/ivago/controller/pickup_times.rs +++ b/src/ivago/controller/pickup_times.rs @@ -1,14 +1,14 @@ use super::search::Street; +use crate::errors::FejError; use chrono::{FixedOffset, TimeZone}; use regex::Regex; use reqwest::blocking as reqwest; use rocket::http::RawStr; use rocket::request::FromFormValue; use serde::ser::{SerializeStruct, Serializer}; -use serde::{Deserialize, Serialize}; +use serde::Serialize; use std::collections::HashMap; use std::convert::{From, TryFrom}; -use std::error::Error; const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling"; const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/pickups"; @@ -108,7 +108,7 @@ pub fn get_pickup_times( number: u32, start_date: BasicDate, end_date: BasicDate, -) -> Result, Box> { +) -> Result, FejError> { let client = reqwest::Client::builder().cookie_store(true).build()?; // This populates the cookies with the necessary values diff --git a/src/ivago/controller/search.rs b/src/ivago/controller/search.rs index 926d282..5037bb4 100644 --- a/src/ivago/controller/search.rs +++ b/src/ivago/controller/search.rs @@ -6,7 +6,6 @@ use rocket::request::FromFormValue; use serde::ser::{Serialize, SerializeStruct, Serializer}; use std::collections::HashMap; use std::convert::TryFrom; -use std::error::Error; /// Endpoint for the search feature const SEARCH_URL: &str = "https://www.ivago.be/nl/particulier/autocomplete/garbage/streets"; diff --git a/src/ivago/mod.rs b/src/ivago/mod.rs index c173aab..541b27a 100644 --- a/src/ivago/mod.rs +++ b/src/ivago/mod.rs @@ -2,7 +2,6 @@ mod controller; #[cfg(test)] mod tests; -use crate::errors::FejError; use rocket::http::Status; use rocket_contrib::json::Json; @@ -10,11 +9,9 @@ pub fn routes() -> Vec { routes![route_search_streets, route_get_pickup_times] } -// URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange #[get("/search?")] pub fn route_search_streets(street: String) -> Result>, Status> { - let result = controller::search_streets(&street)?; - Ok(Json(result)) + Ok(Json(controller::search_streets(&street)?)) } #[get("/?&&&")] @@ -24,9 +21,7 @@ pub fn route_get_pickup_times( start_date: controller::BasicDate, end_date: controller::BasicDate, ) -> Result>, Status> { - match controller::get_pickup_times(street, number, start_date, end_date) { - // TODO provide more meaningful status codes here - Err(_) => Err(Status::InternalServerError), - Ok(times) => Ok(Json(times)), - } + Ok(Json(controller::get_pickup_times( + street, number, start_date, end_date, + )?)) }