Skip to content

Commit

Permalink
Added new SerdeError
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCAB0 committed Jul 27, 2024
1 parent 8330a0b commit afe9eed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub enum CookieError {

#[derive(Error, Debug)]
pub enum VintedWrapperError {
#[error(transparent)]
SerdeError(#[from] serde_json::Error),
#[error(transparent)]
CookiesError(#[from] CookieError),
#[error("Number of items must be non-zero value")]
Expand Down Expand Up @@ -757,12 +759,14 @@ impl<'a> VintedWrapper<'a> {
match json.status() {
StatusCode::OK => {
let response_text = json.text().await?;
let items: AdvancedItems =
serde_json::from_str(&response_text).unwrap_or_else(|err| {
log::error!("Failed to deserialize JSON: {:?}", response_text);
// Handle the error as needed (e.g., rethrow, return a default value, etc.)
panic!("Deserialization error: {:?}", err);
});
let result: Result<AdvancedItems, serde_json::Error> =
serde_json::from_str(&response_text);

if let Err(_) = result {

Check failure on line 765 in src/queries.rs

View workflow job for this annotation

GitHub Actions / Test

redundant pattern matching, consider using `is_err()`

Check failure on line 765 in src/queries.rs

View workflow job for this annotation

GitHub Actions / Test

redundant pattern matching, consider using `is_err()`
log::error!("{}", &response_text)
}

let items = result?;
Ok(items.item)
}
code => {
Expand Down
11 changes: 11 additions & 0 deletions src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async fn test_get_item_query_text() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand All @@ -68,6 +69,7 @@ async fn test_get_item_brands() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand All @@ -91,6 +93,7 @@ async fn test_get_items_brands() {
Err(err) => match err {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::SerdeError(_) => (),
VintedWrapperError::CookiesError(_) => (),
},
};
Expand Down Expand Up @@ -127,6 +130,7 @@ async fn test_get_items_catalogs_no_db() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand Down Expand Up @@ -156,6 +160,7 @@ async fn test_get_items_by_price() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand All @@ -179,6 +184,7 @@ async fn test_get_items_by_size_no_db() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand Down Expand Up @@ -214,6 +220,7 @@ async fn test_get_items_by_size() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand All @@ -237,6 +244,7 @@ async fn test_get_items_by_material() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand Down Expand Up @@ -264,6 +272,7 @@ async fn test_get_items_by_color() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand Down Expand Up @@ -293,6 +302,7 @@ async fn test_get_items_by_currency() {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::ItemError(_, _, _) => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
VintedWrapperError::SerdeError(_) => (),
},
};
}
Expand Down Expand Up @@ -331,6 +341,7 @@ async fn test_get_advanced_items() {
}
Err(err) => match err {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::SerdeError(_) => (),
VintedWrapperError::ItemError(_, _, _) => (),
VintedWrapperError::CookiesError(_) => (),
},
Expand Down

0 comments on commit afe9eed

Please sign in to comment.