From 5ad7e0b47237ec78569f445b02ac6cdb438037cb Mon Sep 17 00:00:00 2001 From: Trim21 Date: Wed, 25 Dec 2024 09:25:15 +0800 Subject: [PATCH] fix: exception name in python (#5453) fix exception name in python --- bindings/python/src/errors.rs | 67 +++++++++++++++++++++------------ bindings/python/src/lib.rs | 20 +++++----- bindings/python/src/operator.rs | 8 ++-- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/bindings/python/src/errors.rs b/bindings/python/src/errors.rs index e3657f3c47db..6a8be442a8ef 100644 --- a/bindings/python/src/errors.rs +++ b/bindings/python/src/errors.rs @@ -20,36 +20,55 @@ use pyo3::exceptions::PyException; use crate::*; -create_exception!(opendal, Error, PyException, "OpenDAL Base Exception"); -create_exception!(opendal, UnexpectedError, Error, "Unexpected errors"); -create_exception!(opendal, UnsupportedError, Error, "Unsupported operation"); -create_exception!(opendal, ConfigInvalidError, Error, "Config is invalid"); -create_exception!(opendal, NotFoundError, Error, "Not found"); -create_exception!(opendal, PermissionDeniedError, Error, "Permission denied"); -create_exception!(opendal, IsADirectoryError, Error, "Is a directory"); -create_exception!(opendal, NotADirectoryError, Error, "Not a directory"); -create_exception!(opendal, AlreadyExistsError, Error, "Already exists"); -create_exception!(opendal, IsSameFileError, Error, "Is same file"); create_exception!( - opendal, - ConditionNotMatchError, + opendal.exceptions, + Error, + PyException, + "OpenDAL Base Exception" +); +create_exception!(opendal.exceptions, Unexpected, Error, "Unexpected errors"); +create_exception!( + opendal.exceptions, + Unsupported, + Error, + "Unsupported operation" +); +create_exception!( + opendal.exceptions, + ConfigInvalid, + Error, + "Config is invalid" +); +create_exception!(opendal.exceptions, NotFound, Error, "Not found"); +create_exception!( + opendal.exceptions, + PermissionDenied, + Error, + "Permission denied" +); +create_exception!(opendal.exceptions, IsADirectory, Error, "Is a directory"); +create_exception!(opendal.exceptions, NotADirectory, Error, "Not a directory"); +create_exception!(opendal.exceptions, AlreadyExists, Error, "Already exists"); +create_exception!(opendal.exceptions, IsSameFile, Error, "Is same file"); +create_exception!( + opendal.exceptions, + ConditionNotMatch, Error, "Condition not match" ); pub fn format_pyerr(err: ocore::Error) -> PyErr { - use ocore::ErrorKind::*; match err.kind() { - Unexpected => UnexpectedError::new_err(err.to_string()), - Unsupported => UnsupportedError::new_err(err.to_string()), - ConfigInvalid => ConfigInvalidError::new_err(err.to_string()), - NotFound => NotFoundError::new_err(err.to_string()), - PermissionDenied => PermissionDeniedError::new_err(err.to_string()), - IsADirectory => IsADirectoryError::new_err(err.to_string()), - NotADirectory => NotADirectoryError::new_err(err.to_string()), - AlreadyExists => AlreadyExistsError::new_err(err.to_string()), - IsSameFile => IsSameFileError::new_err(err.to_string()), - ConditionNotMatch => ConditionNotMatchError::new_err(err.to_string()), - _ => UnexpectedError::new_err(err.to_string()), + ocore::ErrorKind::Unexpected => Unexpected::new_err(err.to_string()), + ocore::ErrorKind::Unsupported => Unsupported::new_err(err.to_string()), + ocore::ErrorKind::ConfigInvalid => ConfigInvalid::new_err(err.to_string()), + ocore::ErrorKind::NotFound => NotFound::new_err(err.to_string()), + ocore::ErrorKind::PermissionDenied => PermissionDenied::new_err(err.to_string()), + ocore::ErrorKind::IsADirectory => IsADirectory::new_err(err.to_string()), + ocore::ErrorKind::NotADirectory => NotADirectory::new_err(err.to_string()), + ocore::ErrorKind::AlreadyExists => AlreadyExists::new_err(err.to_string()), + ocore::ErrorKind::IsSameFile => IsSameFile::new_err(err.to_string()), + ocore::ErrorKind::ConditionNotMatch => ConditionNotMatch::new_err(err.to_string()), + _ => Unexpected::new_err(err.to_string()), } } diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 578826add64d..8284aab80af8 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -98,16 +98,16 @@ fn _opendal(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { let exception_module = PyModule::new(py, "exceptions")?; exception_module.add("Error", py.get_type::())?; - exception_module.add("Unexpected", py.get_type::())?; - exception_module.add("Unsupported", py.get_type::())?; - exception_module.add("ConfigInvalid", py.get_type::())?; - exception_module.add("NotFound", py.get_type::())?; - exception_module.add("PermissionDenied", py.get_type::())?; - exception_module.add("IsADirectory", py.get_type::())?; - exception_module.add("NotADirectory", py.get_type::())?; - exception_module.add("AlreadyExists", py.get_type::())?; - exception_module.add("IsSameFile", py.get_type::())?; - exception_module.add("ConditionNotMatch", py.get_type::())?; + exception_module.add("Unexpected", py.get_type::())?; + exception_module.add("Unsupported", py.get_type::())?; + exception_module.add("ConfigInvalid", py.get_type::())?; + exception_module.add("NotFound", py.get_type::())?; + exception_module.add("PermissionDenied", py.get_type::())?; + exception_module.add("IsADirectory", py.get_type::())?; + exception_module.add("NotADirectory", py.get_type::())?; + exception_module.add("AlreadyExists", py.get_type::())?; + exception_module.add("IsSameFile", py.get_type::())?; + exception_module.add("ConditionNotMatch", py.get_type::())?; m.add_submodule(&exception_module)?; py.import("sys")? .getattr("modules")? diff --git a/bindings/python/src/operator.rs b/bindings/python/src/operator.rs index 76cdb09f8653..cfa58fe5a619 100644 --- a/bindings/python/src/operator.rs +++ b/bindings/python/src/operator.rs @@ -102,7 +102,7 @@ impl Operator { let w = this.writer(&path).map_err(format_pyerr)?; Ok(File::new_writer(w)) } else { - Err(UnsupportedError::new_err(format!( + Err(Unsupported::new_err(format!( "OpenDAL doesn't support mode: {mode}" ))) } @@ -304,7 +304,7 @@ impl AsyncOperator { let w = this.writer(&path).await.map_err(format_pyerr)?; Ok(AsyncFile::new_writer(w)) } else { - Err(UnsupportedError::new_err(format!( + Err(Unsupported::new_err(format!( "OpenDAL doesn't support mode: {mode}" ))) } @@ -578,9 +578,9 @@ impl PresignedRequest { let k = k.as_str(); let v = v .to_str() - .map_err(|err| UnexpectedError::new_err(err.to_string()))?; + .map_err(|err| Unexpected::new_err(err.to_string()))?; if headers.insert(k, v).is_some() { - return Err(UnexpectedError::new_err("duplicate header")); + return Err(Unexpected::new_err("duplicate header")); } } Ok(headers)