Skip to content

Commit

Permalink
Migrate Errors to thiserror (#68)
Browse files Browse the repository at this point in the history
Migrates the majority of errors to `thiserror`, save for the places
where it doesn't make sense (mostly more complex ErrorKind-type errors
and macros). Solves #71.
  • Loading branch information
nikki-aranya authored Jan 29, 2025
1 parent 857cc1f commit c9dfd2b
Show file tree
Hide file tree
Showing 64 changed files with 463 additions and 1,596 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/aranya-afc-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ std = [
"serde/std",
"siphasher?/std",
"spin?/std",
"thiserror/std",
"tracing/std",
]

Expand All @@ -52,6 +53,7 @@ postcard = { workspace = true, default-features = false, features = ["heapless"]
serde = { workspace = true, default-features = false, features = ["derive"] }
siphasher = { version = "1", default-features = false, optional = true }
spin = { version = "0.9", default-features = false, features = ["mutex", "spin_mutex"], optional = true }
thiserror = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
Expand Down
69 changes: 13 additions & 56 deletions crates/aranya-afc-util/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern crate alloc;

use alloc::vec::Vec;
use core::{fmt, result::Result};
use core::result::Result;

use aranya_crypto::{
self,
Expand Down Expand Up @@ -171,50 +171,31 @@ function create_uni_channel(
}

/// An error returned by [`Ffi`].
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub(crate) enum FfiError {
/// The [`aranya_crypto`] crate failed.
Crypto(aranya_crypto::Error),
#[error("crypto error: {0}")]
Crypto(#[from] aranya_crypto::Error),
/// An error occurred while manipulating the [`Stack`].
Stack(MachineErrorType),
#[error("unable to manipulate stack: {0}")]
Stack(#[from] MachineErrorType),
/// Unable to find a particular key.
#[error("unable to find key")]
KeyNotFound,
/// Unable to encode/decode some input.
#[error("unable to decode type")]
Encoding,
/// AFC failed.
Afc(aranya_fast_channels::Error),
#[error("AFC error: {0}")]
Afc(#[from] aranya_fast_channels::Error),
/// Unable to wrap a key.
Wrap(WrapError),
#[error(transparent)]
Wrap(#[from] WrapError),
/// The keystore failed.
#[error("keystore failure")]
KeyStore,
}

impl fmt::Display for FfiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Crypto(err) => write!(f, "crypto error: {err}"),
Self::Stack(err) => write!(f, "unable to manipulate stack: {err}"),
Self::KeyNotFound => write!(f, "unable to find key"),
Self::Encoding => write!(f, "unable to decode type"),
Self::Afc(err) => write!(f, "AFC error: {err}"),
Self::Wrap(err) => write!(f, "{err}"),
Self::KeyStore => write!(f, "keystore failure"),
}
}
}

impl core::error::Error for FfiError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Crypto(err) => Some(err),
Self::Stack(err) => Some(err),
Self::Afc(err) => Some(err),
Self::Wrap(err) => Some(err),
_ => None,
}
}
}

impl From<FfiError> for MachineError {
fn from(err: FfiError) -> Self {
error!("{err}");
Expand All @@ -225,12 +206,6 @@ impl From<FfiError> for MachineError {
}
}

impl From<aranya_crypto::Error> for FfiError {
fn from(err: aranya_crypto::Error) -> Self {
Self::Crypto(err)
}
}

impl From<ImportError> for FfiError {
fn from(err: ImportError) -> Self {
Self::Crypto(err.into())
Expand All @@ -243,24 +218,6 @@ impl From<UnwrapError> for FfiError {
}
}

impl From<MachineErrorType> for FfiError {
fn from(err: MachineErrorType) -> Self {
Self::Stack(err)
}
}

impl From<aranya_fast_channels::Error> for FfiError {
fn from(err: aranya_fast_channels::Error) -> Self {
Self::Afc(err)
}
}

impl From<WrapError> for FfiError {
fn from(err: WrapError) -> Self {
Self::Wrap(err)
}
}

/// An AFC label.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) struct Label(u32);
Expand Down
42 changes: 9 additions & 33 deletions crates/aranya-afc-util/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! An effect handler for AFC.
use core::fmt;

use aranya_crypto::{
afc::{
BidiAuthorSecret, BidiChannel, BidiPeerEncap, UniAuthorSecret, UniChannel, UniPeerEncap,
Expand Down Expand Up @@ -380,49 +378,27 @@ impl<S, O> From<UniKey<S, O>> for Directed<S, O> {
}

/// An error returned by [`Handler`].
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// The current user is not the author of the command.
#[error("not command author")]
NotAuthor,
/// The current user is not the recipient of the command.
#[error("not command recipient")]
NotRecipient,
/// The keystore failed.
#[error("keystore failure")]
KeyStore,
/// Unable to find a particular key.
#[error("unable to find key")]
KeyNotFound,
/// Unable to transform key.
#[error("unable to transform key")]
Transform,
/// Unable to encode/decode a key.
#[error("unable to encode/decode")]
Encoding,
/// A `crypto` crate error.
Crypto(aranya_crypto::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotAuthor => write!(f, "not command author"),
Self::NotRecipient => write!(f, "not command recipient"),
Self::KeyStore => write!(f, "keystore failure"),
Self::KeyNotFound => write!(f, "unable to find key"),
Self::Transform => write!(f, "unable to transform key"),
Self::Encoding => write!(f, "unable to encode/decode"),
Self::Crypto(err) => write!(f, "{err}"),
}
}
}

impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Crypto(err) => Some(err),
_ => None,
}
}
}

impl From<aranya_crypto::Error> for Error {
fn from(err: aranya_crypto::Error) -> Self {
Self::Crypto(err)
}
#[error(transparent)]
Crypto(#[from] aranya_crypto::Error),
}
2 changes: 2 additions & 0 deletions crates/aranya-capi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ std = [
"aranya-libc/std",
"buggy/std",
"ciborium-io?/std",
"thiserror/std",
]

[dependencies]
Expand All @@ -36,6 +37,7 @@ buggy = { version = "0.1.0", default-features = false }

cfg-if = { workspace = true, default-features = false }
const_format = { version = "0.2.31", default-features = false, features = ["assertcp"] }
thiserror = { workspace = true }
tracing = { workspace = true, default-features = false }

ciborium-io = { version = "0.2.2", default-features = false, optional = true }
Expand Down
25 changes: 5 additions & 20 deletions crates/aranya-capi-core/src/cstr.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
use core::{cmp, error, ffi::c_char, fmt, fmt::Write, mem::MaybeUninit};
use core::{cmp, ffi::c_char, fmt, fmt::Write, mem::MaybeUninit};

use buggy::{Bug, BugExt};

/// The error returned by [`write_c_str`].
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
pub enum WriteCStrError {
/// An internal bug was discovered.
Bug(Bug),
#[error(transparent)]
Bug(#[from] Bug),
/// The provided buffer is too small.
#[error("buffer is too small")]
BufferTooSmall,
}

impl error::Error for WriteCStrError {}

impl fmt::Display for WriteCStrError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bug(err) => write!(f, "{err}"),
Self::BufferTooSmall => write!(f, "buffer is too small"),
}
}
}

impl From<Bug> for WriteCStrError {
fn from(err: Bug) -> Self {
Self::Bug(err)
}
}

/// Writes `src` as a null-terminated C string to `dst`.
///
/// If `dst` is long enough to fit the entirety of `src`, it
Expand Down
Loading

0 comments on commit c9dfd2b

Please sign in to comment.