mirror of
https://gitlab.gnome.org/World/Authenticator.git
synced 2025-03-04 08:44:40 +01:00
favicon-scrapper: Don't panic on error
This commit is contained in:
parent
c75adc536e
commit
be9502a5b8
2 changed files with 12 additions and 6 deletions
|
@ -5,6 +5,7 @@ pub enum Error {
|
|||
Io(std::io::Error),
|
||||
Image(image::ImageError),
|
||||
NoResults,
|
||||
Utf8(std::str::Utf8Error),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
|
@ -31,6 +32,12 @@ impl From<image::ImageError> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<std::str::Utf8Error> for Error {
|
||||
fn from(e: std::str::Utf8Error) -> Self {
|
||||
Self::Utf8(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
|
@ -41,6 +48,7 @@ impl std::fmt::Display for Error {
|
|||
Self::Url(e) => write!(f, "Url Parse Error{}", e),
|
||||
Self::Io(e) => write!(f, "IO Error {}", e),
|
||||
Self::Image(e) => write!(f, "Image Error {}", e),
|
||||
Self::Utf8(e) => write!(f, "String conversion error {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ impl Scrapper {
|
|||
#[allow(dead_code)]
|
||||
pub async fn from_file(path: PathBuf, base_url: Option<Url>) -> Result<Self, Error> {
|
||||
let bytes = tokio::fs::read(path).await?;
|
||||
let body = std::str::from_utf8(&bytes).unwrap();
|
||||
let body = std::str::from_utf8(&bytes)?;
|
||||
Self::from_string(body.to_owned(), base_url)
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ impl Scrapper {
|
|||
key: b"content",
|
||||
value,
|
||||
}) => {
|
||||
let mut href = String::from_utf8(value.into_owned()).unwrap();
|
||||
let mut href = String::from_utf8(value.into_owned()).ok()?;
|
||||
if href.starts_with("//") {
|
||||
href = format!("https:{}", href);
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ impl Scrapper {
|
|||
key: b"href",
|
||||
value,
|
||||
}) => {
|
||||
let mut href = String::from_utf8(value.into_owned()).unwrap();
|
||||
let mut href = String::from_utf8(value.into_owned()).ok()?;
|
||||
if href.starts_with("data:") {
|
||||
// only bitmap icons contain ';' as a separator, svgs uses ','
|
||||
let mut icon_data = if href.contains(';') {
|
||||
|
@ -233,9 +233,7 @@ impl Scrapper {
|
|||
key: b"sizes",
|
||||
value,
|
||||
}) => {
|
||||
let size_inner = String::from_utf8(value.into_owned())
|
||||
.unwrap()
|
||||
.to_lowercase();
|
||||
let size_inner = String::from_utf8(value.into_owned()).ok()?.to_lowercase();
|
||||
let mut size_inner = size_inner.split('x');
|
||||
let width = size_inner.next().and_then(|w| w.parse::<u32>().ok());
|
||||
let height = size_inner.next().and_then(|h| h.parse::<u32>().ok());
|
||||
|
|
Loading…
Add table
Reference in a new issue