Skip to content

Commit 06ee9bf

Browse files
Add std::error::Error impls to Error enums of parameter module (#413)
1 parent ad9667f commit 06ee9bf

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

rclrs/src/parameter.rs

+33
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ pub enum ParameterValueError {
624624
ReadOnly,
625625
}
626626

627+
impl std::fmt::Display for ParameterValueError {
628+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
629+
match self {
630+
ParameterValueError::OutOfRange => write!(f, "parameter value was out of the parameter's range"),
631+
ParameterValueError::TypeMismatch => write!(f, "parameter was stored in a static type and an operation on a different type was attempted"),
632+
ParameterValueError::ReadOnly => write!(f, "a write on a read-only parameter was attempted"),
633+
}
634+
}
635+
}
636+
637+
impl std::error::Error for ParameterValueError {}
638+
627639
/// Error that can be generated when doing operations on parameters.
628640
#[derive(Debug)]
629641
pub enum DeclarationError {
@@ -644,6 +656,27 @@ pub enum DeclarationError {
644656
InvalidRange,
645657
}
646658

659+
impl std::fmt::Display for DeclarationError {
660+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
661+
match self {
662+
DeclarationError::AlreadyDeclared => write!(f, "parameter was already declared, but a new declaration was attempted"),
663+
DeclarationError::NoValueAvailable => {
664+
write!(f, "parameter was declared as non-optional but no value was available, either through a user specified default, a command-line override, or a previously set value")
665+
},
666+
DeclarationError::OverrideValueTypeMismatch => {
667+
write!(f, "the override value that was provided has the wrong type")
668+
},
669+
DeclarationError::PriorValueTypeMismatch => {
670+
write!(f, "the value that the parameter was already set to has the wrong type")
671+
},
672+
DeclarationError::InitialValueOutOfRange => write!(f, "the initial value that was selected is out of range"),
673+
DeclarationError::InvalidRange => write!(f, "an invalid range was provided to a parameter declaration (i.e. lower bound > higher bound)"),
674+
}
675+
}
676+
}
677+
678+
impl std::error::Error for DeclarationError {}
679+
647680
impl<'a> Parameters<'a> {
648681
/// Tries to read a parameter of the requested type.
649682
///

rclrs/src/parameter/value.rs

+13
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,24 @@ impl From<ParameterValue> for RmwParameterValue {
376376

377377
/// An error that occured when trying to convert a parameter from an
378378
/// `rcl_interfaces::msg::ParameterValue`
379+
#[derive(Debug)]
379380
pub enum RmwParameterConversionError {
380381
/// The parameter type was not valid.
381382
InvalidParameterType,
382383
}
383384

385+
impl std::fmt::Display for RmwParameterConversionError {
386+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
387+
match self {
388+
RmwParameterConversionError::InvalidParameterType => {
389+
write!(f, "the parameter type was not valid")
390+
}
391+
}
392+
}
393+
}
394+
395+
impl std::error::Error for RmwParameterConversionError {}
396+
384397
impl TryFrom<RmwParameterValue> for ParameterValue {
385398
type Error = RmwParameterConversionError;
386399

0 commit comments

Comments
 (0)