Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

199 changes: 181 additions & 18 deletions clarity-types/src/errors/analysis.rs

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions clarity-types/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use lexer::LexerError;
use rusqlite::Error as SqliteError;
use stacks_common::types::chainstate::BlockHeaderHash;

use crate::ClarityTypeError;
use crate::representations::SymbolicExpression;
use crate::types::{FunctionIdentifier, Value};

Expand Down Expand Up @@ -160,8 +161,6 @@ pub enum RuntimeError {
MaxStackDepthReached,
/// The execution context depth exceeded the virtual machine's limit.
MaxContextDepthReached,
/// Attempt to construct an invalid or unsupported type at runtime (e.g., malformed data structure).
BadTypeConstruction,
/// Reference to an invalid or out-of-bounds block height.
/// The `String` represents the string representation of the queried block height that was invalid.
BadBlockHeight(String),
Expand All @@ -173,9 +172,6 @@ pub enum RuntimeError {
NoCallerInContext,
/// No sender principal available in the current execution context.
NoSenderInContext,
/// Invalid name-value pair in contract data (e.g., map keys).
/// The `&'static str` represents the name of the invalid pair, and the `String` represents the offending value.
BadNameValue(&'static str, String),
/// Reference to a non-existent block header hash.
/// The `BlockHeaderHash` represents the unknown block header hash.
UnknownBlockHeaderHash(BlockHeaderHash),
Expand Down Expand Up @@ -261,6 +257,12 @@ impl error::Error for RuntimeError {
}
}

impl From<ClarityTypeError> for VmExecutionError {
fn from(err: ClarityTypeError) -> Self {
Self::from(CheckErrorKind::from(err))
}
}

impl From<ParseError> for VmExecutionError {
fn from(err: ParseError) -> Self {
match *err.err {
Expand Down
2 changes: 1 addition & 1 deletion clarity-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod types;

pub use errors::VmExecutionError;
pub use representations::{ClarityName, ContractName};
pub use types::Value;
pub use types::{ClarityTypeError, Value};

pub const MAX_CALL_STACK_DEPTH: usize = 64;

Expand Down
13 changes: 5 additions & 8 deletions clarity-types/src/representations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use regex::Regex;
use stacks_common::codec::{Error as codec_error, StacksMessageCodec, read_next, write_next};

use crate::Value;
use crate::errors::RuntimeError;
use crate::types::TraitIdentifier;
use crate::types::{ClarityTypeError, TraitIdentifier};

pub const CONTRACT_MIN_NAME_LENGTH: usize = 1;
pub const CONTRACT_MAX_NAME_LENGTH: usize = 40;
Expand Down Expand Up @@ -63,20 +62,18 @@ lazy_static! {

guarded_string!(
ClarityName,
"ClarityName",
CLARITY_NAME_REGEX,
MAX_STRING_LEN,
RuntimeError,
RuntimeError::BadNameValue
ClarityTypeError,
ClarityTypeError::InvalidClarityName
);

guarded_string!(
ContractName,
"ContractName",
CONTRACT_NAME_REGEX,
MAX_STRING_LEN,
RuntimeError,
RuntimeError::BadNameValue
ClarityTypeError,
ClarityTypeError::InvalidContractName
);

impl StacksMessageCodec for ClarityName {
Expand Down
10 changes: 5 additions & 5 deletions clarity-types/src/tests/representations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

use rstest::rstest;

use crate::errors::RuntimeError;
use crate::representations::{
CONTRACT_MAX_NAME_LENGTH, CONTRACT_MIN_NAME_LENGTH, ClarityName, ContractName, MAX_STRING_LEN,
};
use crate::stacks_common::codec::StacksMessageCodec;
use crate::types::ClarityTypeError;

#[rstest]
#[case::valid_name("hello")]
Expand Down Expand Up @@ -73,7 +73,7 @@ fn test_clarity_name_invalid(#[case] name: &str) {
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
RuntimeError::BadNameValue(_, _)
ClarityTypeError::InvalidClarityName(_)
));
}

Expand All @@ -99,7 +99,7 @@ fn test_clarity_name_serialization(#[case] name: &str) {
// the first byte is the length of the buffer.
#[rstest]
#[case::invalid_utf8(vec![4, 0xFF, 0xFE, 0xFD, 0xFC], "Failed to parse Clarity name: could not contruct from utf8")]
#[case::invalid_name(vec![2, b'2', b'i'], "Failed to parse Clarity name: BadNameValue(\"ClarityName\", \"2i\")")] // starts with number
#[case::invalid_name(vec![2, b'2', b'i'], "Failed to parse Clarity name: InvalidClarityName(\"2i\")")] // starts with number
#[case::too_long(vec![MAX_STRING_LEN + 1], "Failed to deserialize clarity name: too long")]
#[case::wrong_length(vec![3, b'a'], "failed to fill whole buffer")]
fn test_clarity_name_deserialization_errors(#[case] buffer: Vec<u8>, #[case] error_message: &str) {
Expand Down Expand Up @@ -157,7 +157,7 @@ fn test_contract_name_invalid(#[case] name: &str) {
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
RuntimeError::BadNameValue(_, _)
ClarityTypeError::InvalidContractName(_)
));
}

Expand Down Expand Up @@ -201,7 +201,7 @@ fn test_contract_name_serialization_too_long() {
// the first byte is the length of the buffer.
#[rstest]
#[case::invalid_utf8(vec![4, 0xFF, 0xFE, 0xFD, 0xFC], "Failed to parse Contract name: could not construct from utf8")]
#[case::invalid_name(vec![2, b'2', b'i'], "Failed to parse Contract name: BadNameValue(\"ContractName\", \"2i\")")] // starts with number
#[case::invalid_name(vec![2, b'2', b'i'], "Failed to parse Contract name: InvalidContractName(\"2i\")")] // starts with number
#[case::too_long(vec![MAX_STRING_LEN + 1], &format!("Failed to deserialize contract name: too short or too long: {}", MAX_STRING_LEN + 1))]
#[case::wrong_length(vec![3, b'a'], "failed to fill whole buffer")]
fn test_contract_name_deserialization_errors(#[case] buffer: Vec<u8>, #[case] error_message: &str) {
Expand Down
Loading
Loading