diff --git a/async-opcua-types/src/errors.rs b/async-opcua-types/src/errors.rs index 8f4c05f8..04bd42d3 100644 --- a/async-opcua-types/src/errors.rs +++ b/async-opcua-types/src/errors.rs @@ -5,12 +5,12 @@ use thiserror::Error; -use crate::VariantScalarTypeId; +use crate::{relative_path::RelativePathError, StatusCode, VariantScalarTypeId}; /// Rust OpcUa specific errors #[allow(missing_docs)] #[derive(Error, Debug)] -pub enum OpcUAError { +pub enum OpcUaError { #[error("Received an unexpected variant type")] UnexpectedVariantType { variant_id: Option, @@ -18,4 +18,28 @@ pub enum OpcUAError { }, #[error("The requested namespace does not exists")] NamespaceDoesNotExist(String), + #[error("Request returned a StatusCode Error.")] + StatusCodeError(StatusCode), + #[error("Generic Error.")] + Error(crate::Error), + #[error("Function returned a RelativePathError.")] + RelativePathError(RelativePathError), +} + +impl From for OpcUaError { + fn from(value: StatusCode) -> Self { + OpcUaError::StatusCodeError(value) + } +} + +impl From for OpcUaError { + fn from(value: crate::Error) -> Self { + OpcUaError::Error(value) + } +} + +impl From for OpcUaError { + fn from(value: RelativePathError) -> Self { + OpcUaError::RelativePathError(value) + } } diff --git a/async-opcua-types/src/namespaces.rs b/async-opcua-types/src/namespaces.rs index 995239cb..b119dae9 100644 --- a/async-opcua-types/src/namespaces.rs +++ b/async-opcua-types/src/namespaces.rs @@ -2,7 +2,7 @@ use hashbrown::HashMap; -use crate::{errors::OpcUAError, ExpandedNodeId, NodeId, Variant}; +use crate::{errors::OpcUaError, ExpandedNodeId, NodeId, Variant}; /// Utility for handling assignment of namespaces on server startup. #[derive(Debug, Default, Clone)] @@ -28,7 +28,7 @@ impl NamespaceMap { /// Create a new namespace map from a vec of variant as we get when reading /// the namespace array from the server - pub fn new_from_variant_array(array: &[Variant]) -> Result { + pub fn new_from_variant_array(array: &[Variant]) -> Result { let known_namespaces: HashMap = array .iter() .enumerate() @@ -36,7 +36,7 @@ impl NamespaceMap { if let Variant::String(s) = v { Ok((s.value().clone().unwrap_or(String::new()), idx as u16)) } else { - Err(OpcUAError::UnexpectedVariantType { + Err(OpcUaError::UnexpectedVariantType { variant_id: v.scalar_type_id(), message: "Namespace array on server contains invalid data".to_string(), }) diff --git a/async-opcua-types/src/relative_path.rs b/async-opcua-types/src/relative_path.rs index 7979332c..8d2d1521 100644 --- a/async-opcua-types/src/relative_path.rs +++ b/async-opcua-types/src/relative_path.rs @@ -11,6 +11,7 @@ use std::sync::LazyLock; use log::error; use regex::Regex; +use thiserror::Error; use crate::{ node_id::{Identifier, NodeId}, @@ -105,7 +106,7 @@ impl From<&[QualifiedName]> for RelativePath { // for some strange reasons so implementing all thee manually here // impl TryFrom<&str> for RelativePath { - type Error = OpcUaError; + type Error = RelativePathError; fn try_from(value: &str) -> Result { RelativePath::from_str(value, &RelativePathElement::default_node_resolver) @@ -113,7 +114,7 @@ impl TryFrom<&str> for RelativePath { } impl TryFrom<&String> for RelativePath { - type Error = OpcUaError; + type Error = RelativePathError; fn try_from(value: &String) -> Result { RelativePath::from_str(value, &RelativePathElement::default_node_resolver) @@ -121,7 +122,7 @@ impl TryFrom<&String> for RelativePath { } impl TryFrom for RelativePath { - type Error = OpcUaError; + type Error = RelativePathError; fn try_from(value: String) -> Result { RelativePath::from_str(&value, &RelativePathElement::default_node_resolver) @@ -383,18 +384,19 @@ impl RelativePathElement { } } -#[derive(Debug, Clone)] /// Error returned from parsing a relative path. +#[allow(missing_docs)] +#[derive(Error, Debug)] pub enum RelativePathError { - /// Namespace is out of range of a u16. + #[error("Namespace is out of range of a u16.")] NamespaceOutOfRange, - /// Supplied node resolver was unable to resolve a reference type. + #[error("Supplied node resolver was unable to resolve a reference type.")] UnresolvedReferenceType, - /// Path does not match a relative path. + #[error("Path does not match a relative path.")] NoMatch, - /// Path segment is unusually long and has been rejected. + #[error("Path segment is unusually long and has been rejected.")] PathSegmentTooLong, - /// Number of elements in relative path is too large. + #[error("Number of elements in relative path is too large.")] TooManyElements, }