Skip to content

Commit

Permalink
fix: Refactor error handling to leverage thiserror's automatic Displa…
Browse files Browse the repository at this point in the history
…y implementation (#329)
  • Loading branch information
mdqst authored Dec 18, 2024
1 parent 02930bd commit 315938c
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions sdk/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,37 @@
use std::fmt::Display;
use thiserror::Error;

/// Errors that occur during dynamic compilation of guest programs.
#[derive(Debug, Error)]
pub enum BuildError {
/// The compile options are invalid for the memory limit.
#[error("invalid memory configuration for selected prover")]
InvalidMemoryConfiguration,

/// An error occurred reading or writing to the file system.
#[error(transparent)]
IOError(#[from] std::io::Error),

/// The compilation process failed.
#[error("unable to compile using rustc")]
CompilerError,
}

impl Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidMemoryConfiguration => {
write!(f, "invalid memory configuration for selected prover")
}
Self::IOError(error) => write!(f, "{}", error),
Self::CompilerError => write!(f, "unable to compile using rustc"),
}
}
}

/// Errors that occur while reading from or writing to the input/output tapes of the zkVM.
#[derive(Debug, Error)]
pub enum TapeError {
/// Error serializing to or deserializing from the zkVM input/output tapes.
#[error("serialization error: {0}")]
SerializationError(#[from] postcard::Error),

/// Error parsing logging tape.
#[error("encoding error: {0}")]
EncodingError(#[from] std::string::FromUtf8Error),
}

impl Display for TapeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SerializationError(error) => write!(f, "{}", error),
Self::EncodingError(error) => write!(f, "{}", error),
}
}
}

/// Errors that occur when processing file paths.
#[derive(Debug, Error)]
pub enum PathError {
/// Invalid encoding used for path.
#[error("provided path has invalid encoding for use with filesystem")]
EncodingError,
}

impl Display for PathError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::EncodingError => write!(
f,
"provided path has invalid encoding for use with filesystem"
),
}
}
}

0 comments on commit 315938c

Please sign in to comment.