2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-11-18 12:20:16 +00:00
|
|
|
|
2020-01-10 11:33:03 +00:00
|
|
|
use thiserror::Error;
|
2018-11-18 12:20:16 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::ErrorMessage;
|
|
|
|
use crate::FlowReturn;
|
2018-11-18 12:20:16 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2020-12-20 15:09:22 +00:00
|
|
|
macro_rules! panic_to_error(
|
2018-11-18 12:20:16 +00:00
|
|
|
($element:expr, $panicked:expr, $ret:expr, $code:block) => {{
|
|
|
|
use std::panic::{self, AssertUnwindSafe};
|
|
|
|
use std::sync::atomic::Ordering;
|
2021-04-24 20:41:35 +00:00
|
|
|
use $crate::prelude::ElementExtManual;
|
2018-11-18 12:20:16 +00:00
|
|
|
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::unused_unit)]
|
2019-02-21 17:30:36 +00:00
|
|
|
{
|
|
|
|
if $panicked.load(Ordering::Relaxed) {
|
2020-12-20 15:09:22 +00:00
|
|
|
$element.post_error_message($crate::error_msg!($crate::LibraryError::Failed, ["Panicked"]));
|
2019-02-21 17:30:36 +00:00
|
|
|
$ret
|
|
|
|
} else {
|
|
|
|
let result = panic::catch_unwind(AssertUnwindSafe(|| $code));
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(result) => result,
|
|
|
|
Err(err) => {
|
|
|
|
$panicked.store(true, Ordering::Relaxed);
|
|
|
|
if let Some(cause) = err.downcast_ref::<&str>() {
|
2020-12-20 15:09:22 +00:00
|
|
|
$element.post_error_message($crate::error_msg!($crate::LibraryError::Failed, ["Panicked: {}", cause]));
|
2019-02-21 17:30:36 +00:00
|
|
|
} else if let Some(cause) = err.downcast_ref::<String>() {
|
2020-12-20 15:09:22 +00:00
|
|
|
$element.post_error_message($crate::error_msg!($crate::LibraryError::Failed, ["Panicked: {}", cause]));
|
2019-02-21 17:30:36 +00:00
|
|
|
} else {
|
2020-12-20 15:09:22 +00:00
|
|
|
$element.post_error_message($crate::error_msg!($crate::LibraryError::Failed, ["Panicked"]));
|
2019-02-21 17:30:36 +00:00
|
|
|
}
|
|
|
|
$ret
|
2018-11-18 12:20:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
2020-01-10 11:33:03 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Error)]
|
2018-11-18 12:20:16 +00:00
|
|
|
pub enum FlowError {
|
2020-01-10 11:33:03 +00:00
|
|
|
#[error("Flushing")]
|
2018-11-18 12:20:16 +00:00
|
|
|
Flushing,
|
2020-01-10 11:33:03 +00:00
|
|
|
#[error("Eos")]
|
2018-11-18 12:20:16 +00:00
|
|
|
Eos,
|
2020-01-10 11:33:03 +00:00
|
|
|
#[error("Not Negotiated")]
|
2018-11-18 12:20:16 +00:00
|
|
|
NotNegotiated(ErrorMessage),
|
2020-01-10 11:33:03 +00:00
|
|
|
#[error("Error")]
|
2018-11-18 12:20:16 +00:00
|
|
|
Error(ErrorMessage),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FlowError> for FlowReturn {
|
|
|
|
fn from(err: FlowError) -> Self {
|
|
|
|
FlowReturn::from(&err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a FlowError> for FlowReturn {
|
|
|
|
fn from(err: &FlowError) -> FlowReturn {
|
|
|
|
match *err {
|
|
|
|
FlowError::Flushing => FlowReturn::Flushing,
|
|
|
|
FlowError::Eos => FlowReturn::Eos,
|
|
|
|
FlowError::NotNegotiated(..) => FlowReturn::NotNegotiated,
|
|
|
|
FlowError::Error(..) => FlowReturn::Error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|