From e8c631c2118c3c8be9df05b13b8ba107d9e552a2 Mon Sep 17 00:00:00 2001 From: Pavel Zwerschke Date: Fri, 1 Nov 2024 13:20:27 +0100 Subject: [PATCH] proper error handling --- py-rattler/src/networking/middleware.rs | 32 ++++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/py-rattler/src/networking/middleware.rs b/py-rattler/src/networking/middleware.rs index 047382651..699ab4051 100644 --- a/py-rattler/src/networking/middleware.rs +++ b/py-rattler/src/networking/middleware.rs @@ -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), @@ -21,26 +23,28 @@ pub struct PyMirrorMiddleware { #[pymethods] impl PyMirrorMiddleware { #[new] - pub fn __init__(inner: HashMap>) -> Self { - let map = inner - .into_iter() - .map(|(k, v)| { - ( - Url::parse(&k).unwrap(), - v.into_iter() + pub fn __init__(inner: HashMap>) -> PyResult { + 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::, PyRattlerError>>()?; + map.insert(key, value); + } - Self { inner: map } + Ok(Self { inner: map }) } }