[#9] Added general error module
parent
a80774b341
commit
eab31e5e91
|
@ -0,0 +1,23 @@
|
||||||
|
use reqwest::Error;
|
||||||
|
use rocket::http::Status;
|
||||||
|
|
||||||
|
pub enum FejError {
|
||||||
|
InvalidArgument,
|
||||||
|
FailedRequest,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FejError> 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<Error> for FejError {
|
||||||
|
fn from(_: Error) -> FejError {
|
||||||
|
FejError::FailedRequest
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::errors::FejError;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::blocking as reqwest;
|
use reqwest::blocking as reqwest;
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
|
@ -73,7 +74,7 @@ pub struct Street {
|
||||||
/// * `street` - name of the street
|
/// * `street` - name of the street
|
||||||
/// * `city` - city the street is in
|
/// * `city` - city the street is in
|
||||||
// TODO find out how to do this async
|
// TODO find out how to do this async
|
||||||
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error>> {
|
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, FejError> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
|
let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
|
||||||
let data: Vec<HashMap<String, String>> = response.json()?;
|
let data: Vec<HashMap<String, String>> = response.json()?;
|
||||||
|
|
|
@ -2,6 +2,7 @@ mod controller;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
use crate::errors::FejError;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket_contrib::json::Json;
|
use rocket_contrib::json::Json;
|
||||||
|
|
||||||
|
@ -12,10 +13,8 @@ pub fn routes() -> Vec<rocket::Route> {
|
||||||
// URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange
|
// URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange
|
||||||
#[get("/search?<street>")]
|
#[get("/search?<street>")]
|
||||||
pub fn route_search_streets(street: String) -> Result<Json<Vec<controller::Street>>, Status> {
|
pub fn route_search_streets(street: String) -> Result<Json<Vec<controller::Street>>, Status> {
|
||||||
match controller::search_streets(&street) {
|
let result = controller::search_streets(&street)?;
|
||||||
Ok(streets) => Ok(Json(streets)),
|
Ok(Json(result))
|
||||||
Err(_) => Err(Status::InternalServerError),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/?<street>&<number>&<start_date>&<end_date>")]
|
#[get("/?<street>&<number>&<start_date>&<end_date>")]
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
// Route modules
|
// Route modules
|
||||||
|
mod errors;
|
||||||
mod hello;
|
mod hello;
|
||||||
mod ivago;
|
mod ivago;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue