Hi,
I try to create my own error that keep the stacktrace. Here is the code :
fn from(content: Value) -> Result<Self, CustomError>
where
Self: Sized + for<'a> Deserialize<'a>,
{
match from_value::<Self>(content) {
Ok(value) => Ok(value),
Err(error) => Err(CustomError::from(error)),
}
}
#[derive(Debug, Clone, Default)]
pub struct CustomError {
error_type: ErrorType, // enum
cause: Option<Rc<dyn Error>>,
}
impl CustomError {
pub fn new() -> Self {
Self {
..Default::default()
}
}
}
impl Error for CustomError {}
impl Display for CustomError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.error_type, self.cause.as_ref().map(|c| format!("{}", c)).unwrap_or_default())
}
}
impl From<ErrorType> for CustomError {
fn from(error_type: ErrorType) -> Self {
Self {
error_type,
..Default::default()
}
}
}
impl From<Rc<dyn Error>> for CustomError {
fn from(cause: Rc<dyn Error>) -> Self {
Self {
cause: Some(cause),
..Default::default()
}
}
}
But I can't put the serde_json::error
into my custom error. I get the error :
error[E0277]: the trait bound `CustomError: From<serde_json::Error>` is not satisfied
--> lib\src\domain\component.rs:13:31
|
13 | Err(error) => Err(CustomError::from(error)),
| ^^^^^^^^^^^ the trait `From<serde_json::Error>` is not implemented for `CustomError`
|
= help: the following other types implement trait `From<T>`:
<CustomError as From<ErrorType>>
<CustomError as From<Rc<(dyn StdError + 'static)>>>
With some research, I found that the error is caused because serde_json error does not implement std
.
But in the documentation, that is not wath they said : As long as there is a memory allocator, it is possible to use serde_json without the rest of the Rust standard library. Disable the default “std” feature and enable the “alloc” feature
So what did I misunderstood ?
Thanks in advance