Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update error type #22

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dyn-abi/src/eip712/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Resolver {
let this_type = self
.nodes
.get(root_type.as_str())
.ok_or_else(|| DynAbiError::missing_type(root_type))?;
.ok_or_else(|| DynAbiError::missing_type(root_type.as_str()))?;

let edges: &Vec<String> = self.edges.get(root_type.as_str()).unwrap();

Expand Down Expand Up @@ -364,7 +364,7 @@ impl Resolver {
let ty = self
.nodes
.get(root_type.as_str())
.ok_or_else(|| DynAbiError::missing_type(root_type))?;
.ok_or_else(|| DynAbiError::missing_type(root_type.as_str()))?;

let prop_names = ty.prop_names().map(str::to_string).collect();
let tuple = ty
Expand Down
1 change: 0 additions & 1 deletion dyn-abi/src/eip712/typed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ impl Eip712Types {
/// "required": ["types", "primaryType", "domain", "message"]
/// }
/// ```
///
#[derive(Debug, Clone, serde::Serialize)]
pub struct TypedData {
/// Signing domain metadata. The signing domain is the intended context for
Expand Down
55 changes: 27 additions & 28 deletions dyn-abi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,36 @@ pub enum DynAbiError {
actual: serde_json::Value,
},
/// Invalid size for a primitive type (intX, uintX, or bytesX)
InvalidSize(Cow<'static, str>),
InvalidSize(String),
/// Invalid type string, extra chars, or invalid structure
InvalidTypeString(Cow<'static, str>),
InvalidTypeString(String),
/// Unknown type referenced from another type
MissingType(Cow<'static, str>),
MissingType(String),
/// Detected circular dep during typegraph resolution
CircularDependency(Cow<'static, str>),
CircularDependency(String),
/// Invalid Property definition
InvalidPropertyDefinition(Cow<'static, str>),
InvalidPropertyDefinition(String),
/// Hex
HexError(hex::FromHexError),
}

impl core::fmt::Display for DynAbiError {
#[cfg(feature = "std")]
impl std::error::Error for DynAbiError {}

impl fmt::Display for DynAbiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DynAbiError::TypeMismatch { expected, actual } => write!(
f,
"Type mismatch, expected: {:?}, actual: {}",
expected, actual
),
DynAbiError::InvalidSize(ty) => write!(f, "Invalid size for type: {}", ty),
DynAbiError::InvalidTypeString(ty) => write!(f, "Invalid type string: {}", ty),
DynAbiError::MissingType(name) => {
write!(f, "Missing type in type resolution: {}", name)
DynAbiError::TypeMismatch { expected, actual } => {
write!(f, "Type mismatch, expected: {expected:?}, actual: {actual}")
}
DynAbiError::CircularDependency(dep) => write!(f, "Circular dependency: {}", dep),
DynAbiError::InvalidSize(ty) => write!(f, "Invalid size for type: {ty}"),
DynAbiError::InvalidTypeString(ty) => write!(f, "Invalid type string: {ty}"),
DynAbiError::MissingType(name) => write!(f, "Missing type in type resolution: {name}"),
DynAbiError::CircularDependency(dep) => write!(f, "Circular dependency: {dep}"),
DynAbiError::InvalidPropertyDefinition(def) => {
write!(f, "Invalid property definition: {}", def)
write!(f, "Invalid property definition: {def}")
}
DynAbiError::HexError(h) => write!(f, "Hex error: {}", h),
DynAbiError::HexError(h) => h.fmt(f),
}
}
}
Expand All @@ -62,23 +61,23 @@ impl DynAbiError {
}
}

pub(crate) fn invalid_property_def(def: impl Borrow<str>) -> DynAbiError {
DynAbiError::InvalidPropertyDefinition(def.borrow().to_owned().into())
pub(crate) fn invalid_property_def(def: impl ToString) -> DynAbiError {
DynAbiError::InvalidPropertyDefinition(def.to_string())
}

pub(crate) fn invalid_size(ty: impl Borrow<str>) -> DynAbiError {
DynAbiError::InvalidSize(ty.borrow().to_owned().into())
pub(crate) fn invalid_size(ty: impl ToString) -> DynAbiError {
DynAbiError::InvalidSize(ty.to_string())
}

pub(crate) fn invalid_type_string(ty: impl Borrow<str>) -> DynAbiError {
DynAbiError::InvalidTypeString(ty.borrow().to_owned().into())
pub(crate) fn invalid_type_string(ty: impl ToString) -> DynAbiError {
DynAbiError::InvalidTypeString(ty.to_string())
}

pub(crate) fn missing_type(name: impl Borrow<str>) -> DynAbiError {
DynAbiError::MissingType(name.borrow().to_owned().into())
pub(crate) fn missing_type(name: impl ToString) -> DynAbiError {
DynAbiError::MissingType(name.to_string())
}

pub(crate) fn circular_dependency(dep: impl Borrow<str>) -> DynAbiError {
DynAbiError::CircularDependency(dep.borrow().to_owned().into())
pub(crate) fn circular_dependency(dep: impl ToString) -> DynAbiError {
DynAbiError::CircularDependency(dep.to_string())
}
}
6 changes: 6 additions & 0 deletions dyn-abi/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use crate::{no_std_prelude::*, DynAbiError, DynSolType};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct RootType<'a>(&'a str);

impl fmt::Display for RootType<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.0)
}
}

impl RootType<'_> {
/// The string underlying this type. The type name
pub const fn as_str(&self) -> &str {
Expand Down