Skip to content

Commit

Permalink
feat: make error types clonable
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed Jun 3, 2024
1 parent 55abbe9 commit a9bd127
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 80 deletions.
251 changes: 190 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ic-asset-certification/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub type AssetCertificationResult<T = ()> = Result<T, AssetCertificationError>;

/// Asset certification error type.
#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, Clone)]
pub enum AssetCertificationError {
/// Thrown when a suitable asset cannot be found for a given request url.
#[error(r#"No asset was found matching the current request url: {request_url}"#)]
Expand Down
2 changes: 1 addition & 1 deletion packages/ic-cbor/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub type CborResult<T = ()> = Result<T, CborError>;

#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, Clone)]
pub enum CborError {
/// The CBOR was malformed and could not be parsed correctly
#[error("Invalid cbor: {0}")]
Expand Down
5 changes: 2 additions & 3 deletions packages/ic-certificate-verification/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use candid::Principal;
use ic_cbor::CborError;

/// Convenience type that represents the Result of performing certificate verification
pub type CertificateVerificationResult<T = ()> = Result<T, CertificateVerificationError>;

#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, Clone)]
pub enum CertificateVerificationError {
/// Unexpected public key length
#[error(
Expand Down Expand Up @@ -86,7 +85,7 @@ pub enum CertificateVerificationError {

/// Failed to decode CBOR
#[error("CBOR decoding failed")]
CborDecodingFailed(#[from] CborError),
CborDecodingFailed(#[from] ic_cbor::CborError),

/// The certificate contained more than one delegation.
#[error("The certificate contained more than one delegation")]
Expand Down
11 changes: 6 additions & 5 deletions packages/ic-certification/src/rb_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
};
use std::borrow::Cow;
use std::cmp::Ordering::{self, Equal, Greater, Less};
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Color {
Expand Down Expand Up @@ -990,15 +989,17 @@ fn is_balanced<K, V>(root: &NodeRef<K, V>) -> bool {
go(root, num_black)
}

#[cfg(test)]
struct DebugView<'a, K, V>(&'a NodeRef<K, V>);

impl<'a, K: AsRef<[u8]>, V> fmt::Debug for DebugView<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(test)]
impl<'a, K: AsRef<[u8]>, V> std::fmt::Debug for DebugView<'a, K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn go<K: AsRef<[u8]>, V>(
f: &mut fmt::Formatter<'_>,
f: &mut std::fmt::Formatter<'_>,
node: &NodeRef<K, V>,
offset: usize,
) -> fmt::Result {
) -> std::fmt::Result {
match node {
None => writeln!(f, "{:width$}[B] <null>", "", width = offset),
Some(ref h) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ic-http-certification/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pub type HttpCertificationResult<T = ()> = Result<T, HttpCertificationError>;

/// HTTP certification error type.
#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, Clone)]
pub enum HttpCertificationError {
/// The URL was malformed and could not be parsed correctly.
#[error(r#"Failed to parse url: "{0}""#)]
Expand Down
2 changes: 1 addition & 1 deletion packages/ic-response-verification/src/cel/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) type CelParserResult<T = ()> = Result<T, CelParserError>;

/// CEL expression parsing error.
#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, Clone)]
pub enum CelParserError {
/// The CEL parser encountered an unsupported CEL function.
#[error(r#""{0}" is not a supported CEL function, only default_certification is currently supported"#)]
Expand Down
22 changes: 15 additions & 7 deletions packages/ic-response-verification/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Various error types for response verification failure scenarios

use ic_cbor::CborError;
use ic_certificate_verification::CertificateVerificationError;
#[cfg(all(target_arch = "wasm32", feature = "js"))]
use wasm_bindgen::prelude::*;

Expand All @@ -11,11 +9,11 @@ use crate::cel;
pub type ResponseVerificationResult<T = ()> = Result<T, ResponseVerificationError>;

/// The primary container for response verification errors
#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, Clone)]
pub enum ResponseVerificationError {
/// Error converting UTF-8 string
#[error(r#"IO error: "{0}""#)]
IoError(#[from] std::io::Error),
IoError(String),

/// An unsupported verification version was requested
#[error(r#"The requested verification version {requested_version:?} is not supported, the current supported range is {min_supported_version:?}-{max_supported_version:?}"#)]
Expand Down Expand Up @@ -167,17 +165,25 @@ pub enum ResponseVerificationError {

/// Failed to decode CBOR
#[error("CBOR decoding failed")]
CborDecodingFailed(#[from] CborError),
CborDecodingFailed(#[from] ic_cbor::CborError),

/// Failed to verify certificate
#[error("Certificate verification failed")]
CertificateVerificationFailed(#[from] CertificateVerificationError),
CertificateVerificationFailed(
#[from] ic_certificate_verification::CertificateVerificationError,
),

/// HTTP Certification error
#[error(r#"HTTP Certification error: "{0}""#)]
HttpCertificationError(#[from] ic_http_certification::HttpCertificationError),
}

impl From<std::io::Error> for ResponseVerificationError {
fn from(error: std::io::Error) -> Self {
ResponseVerificationError::IoError(error.to_string())
}
}

/// JS Representation of the ResponseVerificationError code
#[cfg(all(target_arch = "wasm32", feature = "js"))]
#[wasm_bindgen(js_name = ResponseVerificationErrorCode)]
Expand Down Expand Up @@ -350,6 +356,8 @@ mod tests {
use super::*;
use crate::cel::CelParserError;
use base64::{engine::general_purpose, Engine as _};
use ic_cbor::CborError;
use ic_certificate_verification::CertificateVerificationError;
use ic_http_certification::HttpCertificationError;
use ic_response_verification_test_utils::hex_decode;
use wasm_bindgen_test::wasm_bindgen_test;
Expand All @@ -375,7 +383,7 @@ mod tests {
let inner_error = std::fs::File::open("foo.txt").expect_err("Expected error");
let error_msg = inner_error.to_string();

let error = ResponseVerificationError::IoError(inner_error);
let error = ResponseVerificationError::IoError(inner_error.to_string());

let result = ResponseVerificationJsError::from(error);

Expand Down

0 comments on commit a9bd127

Please sign in to comment.