Skip to content

Commit

Permalink
Make RelativePathError implemened stdError, make it part of OpcUaError
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier committed Feb 4, 2025
1 parent 97ee872 commit 56786bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
28 changes: 26 additions & 2 deletions async-opcua-types/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,41 @@
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<VariantScalarTypeId>,
message: String,
},
#[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<StatusCode> for OpcUaError {
fn from(value: StatusCode) -> Self {
OpcUaError::StatusCodeError(value)
}
}

impl From<crate::Error> for OpcUaError {
fn from(value: crate::Error) -> Self {
OpcUaError::Error(value)
}
}

impl From<RelativePathError> for OpcUaError {
fn from(value: RelativePathError) -> Self {
OpcUaError::RelativePathError(value)
}
}
6 changes: 3 additions & 3 deletions async-opcua-types/src/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -28,15 +28,15 @@ 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<Self, OpcUAError> {
pub fn new_from_variant_array(array: &[Variant]) -> Result<Self, OpcUaError> {
let known_namespaces: HashMap<String, u16> = array
.iter()
.enumerate()
.map(|(idx, v)| {
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(),
})
Expand Down
20 changes: 11 additions & 9 deletions async-opcua-types/src/relative_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::LazyLock;

use log::error;
use regex::Regex;
use thiserror::Error;

use crate::{
node_id::{Identifier, NodeId},
Expand Down Expand Up @@ -105,23 +106,23 @@ 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<Self, Self::Error> {
RelativePath::from_str(value, &RelativePathElement::default_node_resolver)
}
}

impl TryFrom<&String> for RelativePath {
type Error = OpcUaError;
type Error = RelativePathError;

fn try_from(value: &String) -> Result<Self, Self::Error> {
RelativePath::from_str(value, &RelativePathElement::default_node_resolver)
}
}

impl TryFrom<String> for RelativePath {
type Error = OpcUaError;
type Error = RelativePathError;

fn try_from(value: String) -> Result<Self, Self::Error> {
RelativePath::from_str(&value, &RelativePathElement::default_node_resolver)
Expand Down Expand Up @@ -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,
}

Expand Down

0 comments on commit 56786bc

Please sign in to comment.