use crate::{
global_context::current_scope_id, innerlude::provide_context, use_hook, Element, IntoDynNode,
Properties, ScopeId, Template, TemplateAttribute, TemplateNode, VNode,
};
use std::{
any::{Any, TypeId},
backtrace::Backtrace,
cell::{Ref, RefCell},
error::Error,
fmt::{Debug, Display},
rc::Rc,
str::FromStr,
};
pub struct CapturedPanic {
#[allow(dead_code)]
pub error: Box<dyn Any + 'static>,
}
impl Debug for CapturedPanic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CapturedPanic").finish()
}
}
impl Display for CapturedPanic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("Encountered panic: {:?}", self.error))
}
}
impl Error for CapturedPanic {}
pub fn provide_error_boundary() -> ErrorContext {
provide_context(ErrorContext::new(
Vec::new(),
current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
))
}
pub trait AnyError {
fn as_any(&self) -> &dyn Any;
fn as_error(&self) -> &dyn Error;
}
struct DisplayError(DisplayErrorInner);
impl<E: Display + 'static> From<E> for DisplayError {
fn from(e: E) -> Self {
Self(DisplayErrorInner(Box::new(e)))
}
}
struct DisplayErrorInner(Box<dyn Display>);
impl Display for DisplayErrorInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Debug for DisplayErrorInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Error for DisplayErrorInner {}
impl AnyError for DisplayError {
fn as_any(&self) -> &dyn Any {
&self.0 .0
}
fn as_error(&self) -> &dyn Error {
&self.0
}
}
pub trait Context<T, E>: private::Sealed {
fn show(self, display_error: impl FnOnce(&E) -> Element) -> Result<T>;
fn context<C: Display + 'static>(self, context: C) -> Result<T>;
fn with_context<C: Display + 'static>(self, context: impl FnOnce() -> C) -> Result<T>;
}
impl<T, E> Context<T, E> for std::result::Result<T, E>
where
E: Error + 'static,
{
fn show(self, display_error: impl FnOnce(&E) -> Element) -> Result<T> {
match self {
std::result::Result::Ok(value) => Ok(value),
Err(error) => {
let render = display_error(&error).unwrap_or_default();
let mut error: CapturedError = error.into();
error.render = render;
Err(error)
}
}
}
fn context<C: Display + 'static>(self, context: C) -> Result<T> {
self.with_context(|| context)
}
fn with_context<C: Display + 'static>(self, context: impl FnOnce() -> C) -> Result<T> {
match self {
std::result::Result::Ok(value) => Ok(value),
Err(error) => {
let mut error: CapturedError = error.into();
error.context.push(Rc::new(AdditionalErrorContext {
backtrace: Backtrace::capture(),
context: Box::new(context()),
scope: current_scope_id().ok(),
}));
Err(error)
}
}
}
}
impl<T> Context<T, CapturedError> for Option<T> {
fn show(self, display_error: impl FnOnce(&CapturedError) -> Element) -> Result<T> {
match self {
Some(value) => Ok(value),
None => {
let mut error = CapturedError::from_display("Value was none");
let render = display_error(&error).unwrap_or_default();
error.render = render;
Err(error)
}
}
}
fn context<C: Display + 'static>(self, context: C) -> Result<T> {
self.with_context(|| context)
}
fn with_context<C: Display + 'static>(self, context: impl FnOnce() -> C) -> Result<T> {
match self {
Some(value) => Ok(value),
None => {
let error = CapturedError::from_display(context());
Err(error)
}
}
}
}
pub(crate) mod private {
use super::*;
pub trait Sealed {}
impl<T, E> Sealed for std::result::Result<T, E> where E: Error {}
impl<T> Sealed for Option<T> {}
}
impl<T: Any + Error> AnyError for T {
fn as_any(&self) -> &dyn Any {
self
}
fn as_error(&self) -> &dyn Error {
self
}
}
#[derive(Debug, Clone)]
pub struct ErrorContext {
errors: Rc<RefCell<Vec<CapturedError>>>,
id: ScopeId,
}
impl PartialEq for ErrorContext {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.errors, &other.errors)
}
}
impl ErrorContext {
pub(crate) fn new(errors: Vec<CapturedError>, id: ScopeId) -> Self {
Self {
errors: Rc::new(RefCell::new(errors)),
id,
}
}
pub fn errors(&self) -> Ref<[CapturedError]> {
Ref::map(self.errors.borrow(), |errors| errors.as_slice())
}
pub fn show(&self) -> Option<Element> {
self.errors.borrow().iter().find_map(|task| task.show())
}
pub fn insert_error(&self, error: CapturedError) {
self.errors.borrow_mut().push(error);
self.id.needs_update();
}
pub fn clear_errors(&self) {
self.errors.borrow_mut().clear();
}
}
struct AdditionalErrorContext {
backtrace: Backtrace,
context: Box<dyn Display>,
scope: Option<ScopeId>,
}
impl Debug for AdditionalErrorContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ErrorContext")
.field("backtrace", &self.backtrace)
.field("context", &self.context.to_string())
.finish()
}
}
impl Display for AdditionalErrorContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let AdditionalErrorContext {
backtrace,
context,
scope,
} = self;
write!(f, "{context} (from ")?;
if let Some(scope) = scope {
write!(f, "scope {scope:?} ")?;
}
write!(f, "at {backtrace:?})")
}
}
pub type Result<T = ()> = std::result::Result<T, CapturedError>;
#[allow(non_snake_case)]
pub fn Ok<T>(value: T) -> Result<T> {
Result::Ok(value)
}
#[derive(Clone)]
pub struct CapturedError {
error: Rc<dyn AnyError + 'static>,
backtrace: Rc<Backtrace>,
scope: ScopeId,
pub(crate) render: VNode,
context: Vec<Rc<AdditionalErrorContext>>,
}
impl FromStr for CapturedError {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
std::result::Result::Ok(Self::from_display(s.to_string()))
}
}
#[cfg(feature = "serialize")]
#[derive(serde::Serialize, serde::Deserialize)]
struct SerializedCapturedError {
error: String,
context: Vec<String>,
}
#[cfg(feature = "serialize")]
impl serde::Serialize for CapturedError {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
let serialized = SerializedCapturedError {
error: self.error.as_error().to_string(),
context: self
.context
.iter()
.map(|context| context.to_string())
.collect(),
};
serialized.serialize(serializer)
}
}
#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for CapturedError {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Self, D::Error> {
let serialized = SerializedCapturedError::deserialize(deserializer)?;
let error = DisplayError::from(serialized.error);
let context = serialized
.context
.into_iter()
.map(|context| {
Rc::new(AdditionalErrorContext {
scope: None,
backtrace: Backtrace::disabled(),
context: Box::new(context),
})
})
.collect();
std::result::Result::Ok(Self {
error: Rc::new(error),
context,
backtrace: Rc::new(Backtrace::disabled()),
scope: ScopeId::ROOT,
render: VNode::placeholder(),
})
}
}
impl Debug for CapturedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CapturedError")
.field("error", &self.error.as_error())
.field("backtrace", &self.backtrace)
.field("scope", &self.scope)
.finish()
}
}
impl<E: AnyError + 'static> From<E> for CapturedError {
fn from(error: E) -> Self {
Self {
error: Rc::new(error),
backtrace: Rc::new(Backtrace::capture()),
scope: current_scope_id()
.expect("Cannot create an error boundary outside of a component's scope."),
render: Default::default(),
context: Default::default(),
}
}
}
impl CapturedError {
pub fn new(error: impl AnyError + 'static) -> Self {
Self {
error: Rc::new(error),
backtrace: Rc::new(Backtrace::capture()),
scope: current_scope_id().unwrap_or(ScopeId::ROOT),
render: Default::default(),
context: Default::default(),
}
}
pub fn from_display(error: impl Display + 'static) -> Self {
Self {
error: Rc::new(DisplayError::from(error)),
backtrace: Rc::new(Backtrace::capture()),
scope: current_scope_id().unwrap_or(ScopeId::ROOT),
render: Default::default(),
context: Default::default(),
}
}
pub fn with_origin(mut self, scope: ScopeId) -> Self {
self.scope = scope;
self
}
pub fn show(&self) -> Option<Element> {
if self.render == VNode::placeholder() {
None
} else {
Some(std::result::Result::Ok(self.render.clone()))
}
}
pub(crate) fn deep_clone(&self) -> Self {
Self {
render: self.render.deep_clone(),
..self.clone()
}
}
}
impl PartialEq for CapturedError {
fn eq(&self, other: &Self) -> bool {
format!("{:?}", self) == format!("{:?}", other)
}
}
impl Display for CapturedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"Encountered error: {:?}\nIn scope: {:?}\nBacktrace: {}\nContext: ",
self.error.as_error(),
self.scope,
self.backtrace
))?;
for context in &*self.context {
f.write_fmt(format_args!("{}\n", context))?;
}
std::result::Result::Ok(())
}
}
impl CapturedError {
pub fn downcast<T: 'static>(&self) -> Option<&T> {
if TypeId::of::<T>() == (*self.error).type_id() {
self.error.as_any().downcast_ref::<T>()
} else {
None
}
}
}
pub(crate) fn throw_into(error: impl Into<CapturedError>, scope: ScopeId) {
let error = error.into();
if let Some(cx) = scope.consume_context::<ErrorContext>() {
cx.insert_error(error)
} else {
tracing::error!(
"Tried to throw an error into an error boundary, but failed to locate a boundary: {:?}",
error
)
}
}
#[allow(clippy::type_complexity)]
#[derive(Clone)]
pub struct ErrorHandler(Rc<dyn Fn(ErrorContext) -> Element>);
impl<F: Fn(ErrorContext) -> Element + 'static> From<F> for ErrorHandler {
fn from(value: F) -> Self {
Self(Rc::new(value))
}
}
fn default_handler(errors: ErrorContext) -> Element {
static TEMPLATE: Template = Template {
roots: &[TemplateNode::Element {
tag: "div",
namespace: None,
attrs: &[TemplateAttribute::Static {
name: "color",
namespace: Some("style"),
value: "red",
}],
children: &[TemplateNode::Dynamic { id: 0usize }],
}],
node_paths: &[&[0u8, 0u8]],
attr_paths: &[],
};
std::result::Result::Ok(VNode::new(
None,
TEMPLATE,
Box::new([errors
.errors()
.iter()
.map(|e| {
static TEMPLATE: Template = Template {
roots: &[TemplateNode::Element {
tag: "pre",
namespace: None,
attrs: &[],
children: &[TemplateNode::Dynamic { id: 0usize }],
}],
node_paths: &[&[0u8, 0u8]],
attr_paths: &[],
};
VNode::new(
None,
TEMPLATE,
Box::new([e.to_string().into_dyn_node()]),
Default::default(),
)
})
.into_dyn_node()]),
Default::default(),
))
}
#[derive(Clone)]
pub struct ErrorBoundaryProps {
children: Element,
handle_error: ErrorHandler,
}
impl ErrorBoundaryProps {
#[allow(dead_code)]
pub fn builder() -> ErrorBoundaryPropsBuilder<((), ())> {
ErrorBoundaryPropsBuilder { fields: ((), ()) }
}
}
#[must_use]
#[doc(hidden)]
#[allow(dead_code, non_camel_case_types, non_snake_case)]
pub struct ErrorBoundaryPropsBuilder<TypedBuilderFields> {
fields: TypedBuilderFields,
}
impl<TypedBuilderFields> Clone for ErrorBoundaryPropsBuilder<TypedBuilderFields>
where
TypedBuilderFields: Clone,
{
fn clone(&self) -> Self {
Self {
fields: self.fields.clone(),
}
}
}
impl Properties for ErrorBoundaryProps {
type Builder = ErrorBoundaryPropsBuilder<((), ())>;
fn builder() -> Self::Builder {
ErrorBoundaryProps::builder()
}
fn memoize(&mut self, other: &Self) -> bool {
*self = other.clone();
false
}
}
#[doc(hidden)]
#[allow(dead_code, non_camel_case_types, non_snake_case)]
pub trait ErrorBoundaryPropsBuilder_Optional<T> {
fn into_value<F: FnOnce() -> T>(self, default: F) -> T;
}
impl<T> ErrorBoundaryPropsBuilder_Optional<T> for () {
fn into_value<F: FnOnce() -> T>(self, default: F) -> T {
default()
}
}
impl<T> ErrorBoundaryPropsBuilder_Optional<T> for (T,) {
fn into_value<F: FnOnce() -> T>(self, _: F) -> T {
self.0
}
}
#[allow(dead_code, non_camel_case_types, missing_docs)]
impl<__handle_error> ErrorBoundaryPropsBuilder<((), __handle_error)> {
pub fn children(
self,
children: Element,
) -> ErrorBoundaryPropsBuilder<((Element,), __handle_error)> {
let children = (children,);
let (_, handle_error) = self.fields;
ErrorBoundaryPropsBuilder {
fields: (children, handle_error),
}
}
}
#[doc(hidden)]
#[allow(dead_code, non_camel_case_types, non_snake_case)]
pub enum ErrorBoundaryPropsBuilder_Error_Repeated_field_children {}
#[doc(hidden)]
#[allow(dead_code, non_camel_case_types, missing_docs)]
impl<__handle_error> ErrorBoundaryPropsBuilder<((Element,), __handle_error)> {
#[deprecated(note = "Repeated field children")]
pub fn children(
self,
_: ErrorBoundaryPropsBuilder_Error_Repeated_field_children,
) -> ErrorBoundaryPropsBuilder<((Element,), __handle_error)> {
self
}
}
#[allow(dead_code, non_camel_case_types, missing_docs)]
impl<__children> ErrorBoundaryPropsBuilder<(__children, ())> {
pub fn handle_error(
self,
handle_error: impl ::core::convert::Into<ErrorHandler>,
) -> ErrorBoundaryPropsBuilder<(__children, (ErrorHandler,))> {
let handle_error = (handle_error.into(),);
let (children, _) = self.fields;
ErrorBoundaryPropsBuilder {
fields: (children, handle_error),
}
}
}
#[doc(hidden)]
#[allow(dead_code, non_camel_case_types, non_snake_case)]
pub enum ErrorBoundaryPropsBuilder_Error_Repeated_field_handle_error {}
#[doc(hidden)]
#[allow(dead_code, non_camel_case_types, missing_docs)]
impl<__children> ErrorBoundaryPropsBuilder<(__children, (ErrorHandler,))> {
#[deprecated(note = "Repeated field handle_error")]
pub fn handle_error(
self,
_: ErrorBoundaryPropsBuilder_Error_Repeated_field_handle_error,
) -> ErrorBoundaryPropsBuilder<(__children, (ErrorHandler,))> {
self
}
}
#[allow(dead_code, non_camel_case_types, missing_docs)]
impl<
__handle_error: ErrorBoundaryPropsBuilder_Optional<ErrorHandler>,
__children: ErrorBoundaryPropsBuilder_Optional<Element>,
> ErrorBoundaryPropsBuilder<(__children, __handle_error)>
{
pub fn build(self) -> ErrorBoundaryProps {
let (children, handle_error) = self.fields;
let children = ErrorBoundaryPropsBuilder_Optional::into_value(children, VNode::empty);
let handle_error = ErrorBoundaryPropsBuilder_Optional::into_value(handle_error, || {
ErrorHandler(Rc::new(default_handler))
});
ErrorBoundaryProps {
children,
handle_error,
}
}
}
#[allow(non_upper_case_globals, non_snake_case)]
pub fn ErrorBoundary(props: ErrorBoundaryProps) -> Element {
let error_boundary = use_hook(provide_error_boundary);
let errors = error_boundary.errors();
if errors.is_empty() {
std::result::Result::Ok({
static TEMPLATE: Template = Template {
roots: &[TemplateNode::Dynamic { id: 0usize }],
node_paths: &[&[0u8]],
attr_paths: &[],
};
VNode::new(
None,
TEMPLATE,
Box::new([(props.children).into_dyn_node()]),
Default::default(),
)
})
} else {
tracing::trace!("scope id: {:?}", current_scope_id());
tracing::trace!("handling errors: {:?}", errors);
(props.handle_error.0)(error_boundary.clone())
}
}