Skip to content

Commit

Permalink
feat: make error clonable
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed May 31, 2024
1 parent 7755301 commit 01b4605
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions rust/candid/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//! `candid::Result<T> = Result<T, candid::Error>>`
use serde::{de, ser};
use std::{io, num::ParseIntError};
use std::{io, num::ParseIntError, sync::Arc};
use thiserror::Error;

pub type Result<T = ()> = std::result::Result<T, Error>;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Label {
pos: usize,
message: String,
}

#[derive(Debug, Error)]
#[derive(Debug, Clone, Error)]
pub enum Error {
#[error("binary parser error: {}", .0.first().map_or_else(|| "io error".to_string(), |f| format!("{} at byte offset {}", f.message, f.pos/2)))]
Binread(Vec<Label>),
Expand All @@ -21,12 +21,12 @@ pub enum Error {
Subtype(String),

#[error(transparent)]
Custom(#[from] anyhow::Error),
Custom(#[from] Arc<anyhow::Error>),
}

impl Error {
pub fn msg<T: ToString>(msg: T) -> Self {
Error::Custom(anyhow::anyhow!(msg.to_string()))
anyhow::anyhow!(msg.to_string()).into()
}
pub fn subtype<T: ToString>(msg: T) -> Self {
Error::Subtype(msg.to_string())
Expand Down Expand Up @@ -133,3 +133,9 @@ impl From<ParseIntError> for Error {
Error::msg(format!("ParseIntError: {e}"))
}
}

impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Error {
Error::Custom(Arc::new(e))
}
}

0 comments on commit 01b4605

Please sign in to comment.