Skip to content

Commit

Permalink
Make Env::Error: Into<crate::Error>, use for errors in bytes.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Sep 7, 2023
1 parent 164d403 commit 2b7d6ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
19 changes: 12 additions & 7 deletions soroban-env-common/src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
declare_tag_based_object_wrapper, ConversionError, Env, Error, TryFromVal, TryIntoVal, Val,
declare_tag_based_object_wrapper,
xdr::{ScErrorCode, ScErrorType},
Env, Error, TryFromVal, TryIntoVal, Val,
};

declare_tag_based_object_wrapper!(BytesObject);
Expand All @@ -8,14 +10,17 @@ impl<E: Env, const N: usize> TryFromVal<E, BytesObject> for [u8; N] {
type Error = Error;

fn try_from_val(env: &E, val: &BytesObject) -> Result<Self, Self::Error> {
let len: u32 = env.bytes_len(*val).map_err(|_| ConversionError)?.into();
let len: u32 = env.bytes_len(*val).map_err(Into::into)?.into();
let len = len as usize;
if len != N {
return Err(ConversionError.into());
return Err(Error::from_type_and_code(
ScErrorType::Value,
ScErrorCode::UnexpectedSize,
));
}
let mut arr = [0u8; N];
env.bytes_copy_to_slice(*val, Val::U32_ZERO, &mut arr)
.map_err(|_| ConversionError)?;
.map_err(Into::into)?;
Ok(arr)
}
}
Expand All @@ -34,11 +39,11 @@ impl<E: Env> TryFromVal<E, BytesObject> for Vec<u8> {
type Error = Error;

fn try_from_val(env: &E, val: &BytesObject) -> Result<Self, Self::Error> {
let len: u32 = env.bytes_len(*val).map_err(|_| ConversionError)?.into();
let len: u32 = env.bytes_len(*val).map_err(Into::into)?.into();
let len = len as usize;
let mut vec = vec![0u8; len];
env.bytes_copy_to_slice(*val, Val::U32_ZERO, &mut vec)
.map_err(|_| ConversionError)?;
.map_err(Into::into)?;
Ok(vec)
}
}
Expand All @@ -57,7 +62,7 @@ impl<E: Env> TryFromVal<E, &[u8]> for BytesObject {
type Error = Error;
#[inline(always)]
fn try_from_val(env: &E, v: &&[u8]) -> Result<BytesObject, Self::Error> {
Ok(env.bytes_new_from_slice(v).map_err(|_| ConversionError)?)
env.bytes_new_from_slice(v).map_err(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait EnvBase: Sized + Clone {
/// environment-interface level, and then either directly handle or escalate
/// the contained `Error` code to the user as a `Error` or `Result<>` of
/// some other type, depending on the API.
type Error: core::fmt::Debug;
type Error: core::fmt::Debug + Into<crate::Error>;

/// Reject an error from the environment, turning it into a panic but on
/// terms that the environment controls (eg. transforming or logging it).
Expand Down
3 changes: 3 additions & 0 deletions soroban-env-common/src/val.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This permits globals prouced by derive(num_enum::TryFromPrimitive) below.
#![cfg_attr(test, allow(non_upper_case_globals))]

use crate::{
declare_tag_based_object_wrapper, declare_tag_based_wrapper, impl_rawval_wrapper_base,
impl_tryfroms_and_tryfromvals_delegating_to_rawvalconvertible, Compare, I32Val, SymbolSmall,
Expand Down
6 changes: 6 additions & 0 deletions soroban-env-host/src/host/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub struct HostError {

impl std::error::Error for HostError {}

impl Into<Error> for HostError {
fn into(self) -> Error {
self.error
}
}

impl Debug for HostError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// We do a little trimming here, skipping the first two frames (which
Expand Down

0 comments on commit 2b7d6ea

Please sign in to comment.