Skip to content

Commit 9d27ff0

Browse files
First pass: add ClarityTypeError and cleanup RuntimeError a bit
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 13480cf commit 9d27ff0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2805
-1453
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clarity-types/src/errors/analysis.rs

Lines changed: 200 additions & 18 deletions
Large diffs are not rendered by default.

clarity-types/src/errors/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ pub enum RuntimeError {
160160
MaxStackDepthReached,
161161
/// The execution context depth exceeded the virtual machine's limit.
162162
MaxContextDepthReached,
163-
/// Attempt to construct an invalid or unsupported type at runtime (e.g., malformed data structure).
164-
BadTypeConstruction,
165163
/// Reference to an invalid or out-of-bounds block height.
166164
/// The `String` represents the string representation of the queried block height that was invalid.
167165
BadBlockHeight(String),
@@ -173,9 +171,6 @@ pub enum RuntimeError {
173171
NoCallerInContext,
174172
/// No sender principal available in the current execution context.
175173
NoSenderInContext,
176-
/// Invalid name-value pair in contract data (e.g., map keys).
177-
/// The `&'static str` represents the name of the invalid pair, and the `String` represents the offending value.
178-
BadNameValue(&'static str, String),
179174
/// Reference to a non-existent block header hash.
180175
/// The `BlockHeaderHash` represents the unknown block header hash.
181176
UnknownBlockHeaderHash(BlockHeaderHash),

clarity-types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub mod types;
3333

3434
pub use errors::VmExecutionError;
3535
pub use representations::{ClarityName, ContractName};
36-
pub use types::Value;
36+
pub use types::{ClarityTypeError, Value};
3737

3838
pub const MAX_CALL_STACK_DEPTH: usize = 64;
3939

clarity-types/src/representations.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use regex::Regex;
2323
use stacks_common::codec::{Error as codec_error, StacksMessageCodec, read_next, write_next};
2424

2525
use crate::Value;
26-
use crate::errors::RuntimeError;
27-
use crate::types::TraitIdentifier;
26+
use crate::types::{ClarityTypeError, TraitIdentifier};
2827

2928
pub const CONTRACT_MIN_NAME_LENGTH: usize = 1;
3029
pub const CONTRACT_MAX_NAME_LENGTH: usize = 40;
@@ -63,20 +62,18 @@ lazy_static! {
6362

6463
guarded_string!(
6564
ClarityName,
66-
"ClarityName",
6765
CLARITY_NAME_REGEX,
6866
MAX_STRING_LEN,
69-
RuntimeError,
70-
RuntimeError::BadNameValue
67+
ClarityTypeError,
68+
ClarityTypeError::InvalidClarityName
7169
);
7270

7371
guarded_string!(
7472
ContractName,
75-
"ContractName",
7673
CONTRACT_NAME_REGEX,
7774
MAX_STRING_LEN,
78-
RuntimeError,
79-
RuntimeError::BadNameValue
75+
ClarityTypeError,
76+
ClarityTypeError::InvalidContractName
8077
);
8178

8279
impl StacksMessageCodec for ClarityName {

clarity-types/src/tests/representations.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
use rstest::rstest;
1717

18-
use crate::errors::RuntimeError;
1918
use crate::representations::{
2019
CONTRACT_MAX_NAME_LENGTH, CONTRACT_MIN_NAME_LENGTH, ClarityName, ContractName, MAX_STRING_LEN,
2120
};
2221
use crate::stacks_common::codec::StacksMessageCodec;
22+
use crate::types::ClarityTypeError;
2323

2424
#[rstest]
2525
#[case::valid_name("hello")]
@@ -73,7 +73,7 @@ fn test_clarity_name_invalid(#[case] name: &str) {
7373
assert!(result.is_err());
7474
assert!(matches!(
7575
result.unwrap_err(),
76-
RuntimeError::BadNameValue(_, _)
76+
ClarityTypeError::InvalidClarityName(_)
7777
));
7878
}
7979

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

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

0 commit comments

Comments
 (0)