|
4 | 4 | // | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ |
5 | 5 | // +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ |
6 | 6 | // |
7 | | -// Copyright (C) 2025 Bitcraze AB |
| 7 | +// Copyright (C) 2026 Bitcraze AB |
8 | 8 | // |
9 | 9 | // This program is free software: you can redistribute it and/or modify |
10 | 10 | // it under the terms of the GNU General Public License as published by |
|
21 | 21 |
|
22 | 22 | //! Error conversion utilities for Python bindings |
23 | 23 |
|
24 | | -use pyo3::exceptions::PyRuntimeError; |
25 | | -use pyo3::PyErr; |
| 24 | +use pyo3::exceptions::PyException; |
| 25 | +use pyo3::prelude::*; |
| 26 | + |
| 27 | +pyo3_stub_gen::create_exception!(cflib2._rust, CrazyflieError, PyException, "Base exception for all Crazyflie errors."); |
| 28 | +pyo3_stub_gen::create_exception!(cflib2._rust, ProtocolVersionNotSupportedError, CrazyflieError, "Protocol version not supported."); |
| 29 | +pyo3_stub_gen::create_exception!(cflib2._rust, ProtocolError, CrazyflieError, "Unexpected protocol error."); |
| 30 | +pyo3_stub_gen::create_exception!(cflib2._rust, ParamError, CrazyflieError, "Parameter subsystem error."); |
| 31 | +pyo3_stub_gen::create_exception!(cflib2._rust, LogError, CrazyflieError, "Log subsystem error."); |
| 32 | +pyo3_stub_gen::create_exception!(cflib2._rust, ConversionError, CrazyflieError, "Value conversion error."); |
| 33 | +pyo3_stub_gen::create_exception!(cflib2._rust, LinkError, CrazyflieError, "Crazyflie link error."); |
| 34 | +pyo3_stub_gen::create_exception!(cflib2._rust, DisconnectedError, CrazyflieError, "Crazyflie is disconnected."); |
| 35 | +pyo3_stub_gen::create_exception!(cflib2._rust, VariableNotFoundError, CrazyflieError, "Variable not found in TOC."); |
| 36 | +pyo3_stub_gen::create_exception!(cflib2._rust, SystemError, CrazyflieError, "Async executor error."); |
| 37 | +pyo3_stub_gen::create_exception!(cflib2._rust, AppchannelPacketTooLargeError, CrazyflieError, "App channel packet exceeds MTU."); |
| 38 | +pyo3_stub_gen::create_exception!(cflib2._rust, InvalidArgumentError, CrazyflieError, "Invalid argument."); |
| 39 | +pyo3_stub_gen::create_exception!(cflib2._rust, TimeoutError, CrazyflieError, "Operation timed out."); |
| 40 | +pyo3_stub_gen::create_exception!(cflib2._rust, MemoryError, CrazyflieError, "Memory subsystem error."); |
| 41 | +pyo3_stub_gen::create_exception!(cflib2._rust, InvalidParameterError, CrazyflieError, "Invalid parameter."); |
| 42 | + |
| 43 | +/// Register all custom exception types with the Python module |
| 44 | +pub fn register_exceptions(m: &Bound<'_, PyModule>) -> PyResult<()> { |
| 45 | + let py = m.py(); |
| 46 | + m.add("CrazyflieError", py.get_type::<CrazyflieError>())?; |
| 47 | + m.add("ProtocolVersionNotSupportedError", py.get_type::<ProtocolVersionNotSupportedError>())?; |
| 48 | + m.add("ProtocolError", py.get_type::<ProtocolError>())?; |
| 49 | + m.add("ParamError", py.get_type::<ParamError>())?; |
| 50 | + m.add("LogError", py.get_type::<LogError>())?; |
| 51 | + m.add("ConversionError", py.get_type::<ConversionError>())?; |
| 52 | + m.add("LinkError", py.get_type::<LinkError>())?; |
| 53 | + m.add("DisconnectedError", py.get_type::<DisconnectedError>())?; |
| 54 | + m.add("VariableNotFoundError", py.get_type::<VariableNotFoundError>())?; |
| 55 | + m.add("SystemError", py.get_type::<SystemError>())?; |
| 56 | + m.add("AppchannelPacketTooLargeError", py.get_type::<AppchannelPacketTooLargeError>())?; |
| 57 | + m.add("InvalidArgumentError", py.get_type::<InvalidArgumentError>())?; |
| 58 | + m.add("TimeoutError", py.get_type::<TimeoutError>())?; |
| 59 | + m.add("MemoryError", py.get_type::<MemoryError>())?; |
| 60 | + m.add("InvalidParameterError", py.get_type::<InvalidParameterError>())?; |
| 61 | + Ok(()) |
| 62 | +} |
26 | 63 |
|
27 | 64 | /// Convert Rust crazyflie_lib errors to Python exceptions |
28 | 65 | pub fn to_pyerr(err: crazyflie_lib::Error) -> PyErr { |
29 | | - PyRuntimeError::new_err(format!("Crazyflie error: {:?}", err)) |
| 66 | + use crazyflie_lib::Error::*; |
| 67 | + let msg = err.to_string(); |
| 68 | + match err { |
| 69 | + ProtocolVersionNotSupported { .. } => ProtocolVersionNotSupportedError::new_err(msg), |
| 70 | + ProtocolError(_) => self::ProtocolError::new_err(msg), |
| 71 | + ParamError(_) => self::ParamError::new_err(msg), |
| 72 | + LogError(_) => self::LogError::new_err(msg), |
| 73 | + ConversionError(_) => self::ConversionError::new_err(msg), |
| 74 | + LinkError(_) => self::LinkError::new_err(msg), |
| 75 | + Disconnected => DisconnectedError::new_err(msg), |
| 76 | + VariableNotFound => VariableNotFoundError::new_err(msg), |
| 77 | + SystemError(_) => self::SystemError::new_err(msg), |
| 78 | + AppchannelPacketTooLarge => AppchannelPacketTooLargeError::new_err(msg), |
| 79 | + InvalidArgument(_) => InvalidArgumentError::new_err(msg), |
| 80 | + Timeout => self::TimeoutError::new_err(msg), |
| 81 | + MemoryError(_) => self::MemoryError::new_err(msg), |
| 82 | + InvalidParameter(_) => InvalidParameterError::new_err(msg), |
| 83 | + } |
30 | 84 | } |
0 commit comments