Skip to content

Commit

Permalink
proper error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw committed Nov 1, 2024
1 parent 0bcd9f5 commit e8c631c
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions py-rattler/src/networking/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use pyo3::{pyclass, pymethods, FromPyObject};
use pyo3::{pyclass, pymethods, FromPyObject, PyResult};
use rattler_networking::{
mirror_middleware::Mirror, AuthenticationMiddleware, AuthenticationStorage, MirrorMiddleware,
};
use std::collections::HashMap;
use url::Url;

use crate::error::PyRattlerError;

#[derive(FromPyObject)]
pub enum PyMiddleware {
MirrorMiddleware(PyMirrorMiddleware),
Expand All @@ -21,26 +23,28 @@ pub struct PyMirrorMiddleware {
#[pymethods]
impl PyMirrorMiddleware {
#[new]
pub fn __init__(inner: HashMap<String, Vec<String>>) -> Self {
let map = inner
.into_iter()
.map(|(k, v)| {
(
Url::parse(&k).unwrap(),
v.into_iter()
pub fn __init__(inner: HashMap<String, Vec<String>>) -> PyResult<Self> {
let mut map = HashMap::new();
for (k, v) in inner {
let key = Url::parse(&k).map_err(PyRattlerError::from)?;
let value = v
.into_iter()
.map(|url| {
Url::parse(&url)
.map(|url| Mirror {
url: Url::parse(&url).unwrap(),
url,
no_zstd: false,
no_bz2: false,
no_jlap: false,
max_failures: None,
})
.collect(),
)
})
.collect();
.map_err(PyRattlerError::from)
})
.collect::<Result<Vec<Mirror>, PyRattlerError>>()?;
map.insert(key, value);
}

Self { inner: map }
Ok(Self { inner: map })
}
}

Expand Down

0 comments on commit e8c631c

Please sign in to comment.