start to convert ad-hoc errors to explicit types
This commit is contained in:
75
src/core/error.rs
Normal file
75
src/core/error.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use std::any::TypeId;
|
||||
|
||||
use color_eyre::eyre::Report;
|
||||
use stardust_xr::{
|
||||
messenger::MessengerError,
|
||||
schemas::flex::{
|
||||
flexbuffers::{DeserializationError, ReaderError},
|
||||
FlexSerializeError,
|
||||
},
|
||||
};
|
||||
use stereokit_rust::StereoKitError;
|
||||
use thiserror::Error;
|
||||
|
||||
pub type Result<T, E = ServerError> = std::result::Result<T, E>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ServerError {
|
||||
#[error("Internal: Unable to get client")]
|
||||
NoClient,
|
||||
#[error("Messenger does not exist for this node")]
|
||||
NoMessenger,
|
||||
#[error("Messenger error: {0}")]
|
||||
MessengerError(#[from] MessengerError),
|
||||
#[error("Remote method error: {0}")]
|
||||
RemoteMethodError(String),
|
||||
#[error("Serialization error: {0}")]
|
||||
SerializationError(#[from] FlexSerializeError),
|
||||
#[error("Deserialization error: {0}")]
|
||||
DeserializationError(#[from] DeserializationError),
|
||||
#[error("Reader error: {0}")]
|
||||
ReaderError(#[from] ReaderError),
|
||||
#[error("StereoKit error: {0}")]
|
||||
StereoKitError(#[from] StereoKitError),
|
||||
#[error("Aspect {} does not exist for node", 0.to_string())]
|
||||
NoAspect(TypeId),
|
||||
#[error("{0}")]
|
||||
Report(#[from] Report),
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! bail {
|
||||
($msg:literal $(,)?) => {
|
||||
return Err($crate::core::error::ServerError::from(color_eyre::eyre::eyre!($msg)));
|
||||
};
|
||||
($err:expr $(,)?) => {
|
||||
return Err($crate::core::error::ServerError::from(color_eyre::eyre::eyre!($err)));
|
||||
};
|
||||
($fmt:expr, $($arg:tt)*) => {
|
||||
return Err($crate::core::error::ServerError::from(color_eyre::eyre::eyre!($fmt, $($arg)*)));
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! ensure {
|
||||
($cond:expr $(,)?) => {
|
||||
if !$cond {
|
||||
$crate::ensure!($cond, concat!("Condition failed: `", stringify!($cond), "`"))
|
||||
}
|
||||
};
|
||||
($cond:expr, $msg:literal $(,)?) => {
|
||||
if !$cond {
|
||||
return Err($crate::core::error::ServerError::from(color_eyre::eyre::eyre!($msg)));
|
||||
}
|
||||
};
|
||||
($cond:expr, $err:expr $(,)?) => {
|
||||
if !$cond {
|
||||
return Err($crate::core::error::ServerError::from(color_eyre::eyre::eyre!($err)));
|
||||
}
|
||||
};
|
||||
($cond:expr, $fmt:expr, $($arg:tt)*) => {
|
||||
if !$cond {
|
||||
return Err($crate::core::error::ServerError::from(color_eyre::eyre::eyre!($fmt, $($arg)*)));
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,7 @@ pub mod client;
|
||||
pub mod client_state;
|
||||
pub mod delta;
|
||||
pub mod destroy_queue;
|
||||
pub mod error;
|
||||
pub mod registry;
|
||||
pub mod resource;
|
||||
pub mod scenegraph;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::core::error::Result;
|
||||
use crate::nodes::alias::get_original;
|
||||
use crate::nodes::Node;
|
||||
use crate::{core::client::Client, nodes::Message};
|
||||
use color_eyre::eyre::Result;
|
||||
use once_cell::sync::OnceCell;
|
||||
use parking_lot::Mutex;
|
||||
use rustc_hash::FxHashMap;
|
||||
@@ -59,20 +59,20 @@ impl MethodResponseSender {
|
||||
// ) {
|
||||
// let _ = self.0.send(map_method_return(result));
|
||||
// }
|
||||
pub fn wrap_sync<F: FnOnce() -> color_eyre::eyre::Result<Message>>(self, f: F) {
|
||||
pub fn wrap_sync<F: FnOnce() -> crate::core::error::Result<Message>>(self, f: F) {
|
||||
self.send(f().map_err(|e| ScenegraphError::MemberError {
|
||||
error: e.to_string(),
|
||||
}))
|
||||
}
|
||||
pub fn wrap_async<T: Serialize>(
|
||||
self,
|
||||
f: impl Future<Output = color_eyre::eyre::Result<(T, Vec<OwnedFd>)>> + Send + 'static,
|
||||
f: impl Future<Output = Result<(T, Vec<OwnedFd>)>> + Send + 'static,
|
||||
) {
|
||||
tokio::task::spawn(async move { self.0.send(map_method_return(f.await)) });
|
||||
}
|
||||
}
|
||||
fn map_method_return<T: Serialize>(
|
||||
result: color_eyre::eyre::Result<(T, Vec<OwnedFd>)>,
|
||||
result: Result<(T, Vec<OwnedFd>)>,
|
||||
) -> Result<(Vec<u8>, Vec<OwnedFd>), ScenegraphError> {
|
||||
let (value, fds) = result.map_err(|e| ScenegraphError::MemberError {
|
||||
error: e.to_string(),
|
||||
|
||||
Reference in New Issue
Block a user