Skip to content

Commit

Permalink
fix: some document error messages didnt specify the corresponding pro…
Browse files Browse the repository at this point in the history
…perty (#1873)
  • Loading branch information
pauldelucia authored Jul 16, 2024
1 parent 9f4026f commit 5405794
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
39 changes: 25 additions & 14 deletions packages/rs-dpp/src/data_contract/document_type/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl DocumentPropertyType {
r_vec.extend(vec);
Ok(r_vec)
} else {
Err(get_field_type_matching_error().into())
Err(get_field_type_matching_error(&value).into())
}
}
DocumentPropertyType::Date => {
Expand Down Expand Up @@ -616,7 +616,9 @@ impl DocumentPropertyType {
Ok(r_vec)
}
DocumentPropertyType::Boolean => {
let value_as_boolean = value.as_bool().ok_or_else(get_field_type_matching_error)?;
let value_as_boolean = value
.as_bool()
.ok_or_else(|| get_field_type_matching_error(&value))?;
// 0 means does not exist
if value_as_boolean {
Ok(vec![1]) // 1 is true
Expand Down Expand Up @@ -652,7 +654,7 @@ impl DocumentPropertyType {
len_prepended_vec.append(&mut r_vec);
Ok(len_prepended_vec)
} else {
Err(get_field_type_matching_error().into())
Err(get_field_type_matching_error(&value).into())
}
}
DocumentPropertyType::Array(array_field_type) => {
Expand All @@ -667,7 +669,7 @@ impl DocumentPropertyType {
})?;
Ok(r_vec)
} else {
Err(get_field_type_matching_error().into())
Err(get_field_type_matching_error(&value).into())
}
}
DocumentPropertyType::VariableTypeArray(_) => Err(ProtocolError::DataContractError(
Expand All @@ -688,7 +690,9 @@ impl DocumentPropertyType {
}
return match self {
DocumentPropertyType::String(_) => {
let value_as_text = value.as_text().ok_or_else(get_field_type_matching_error)?;
let value_as_text = value
.as_text()
.ok_or_else(|| get_field_type_matching_error(value))?;
let vec = value_as_text.as_bytes().to_vec();
let mut r_vec = vec.len().encode_var_vec();
r_vec.extend(vec);
Expand Down Expand Up @@ -728,7 +732,9 @@ impl DocumentPropertyType {
},
DocumentPropertyType::Identifier => Ok(value.to_identifier_bytes()?),
DocumentPropertyType::Boolean => {
let value_as_boolean = value.as_bool().ok_or_else(get_field_type_matching_error)?;
let value_as_boolean = value
.as_bool()
.ok_or_else(|| get_field_type_matching_error(value))?;
// 0 means does not exist
if value_as_boolean {
Ok(vec![1]) // 1 is true
Expand All @@ -738,7 +744,7 @@ impl DocumentPropertyType {
}
DocumentPropertyType::Object(inner_fields) => {
let Some(value_map) = value.as_map() else {
return Err(get_field_type_matching_error().into());
return Err(get_field_type_matching_error(value).into());
};
let value_map = Value::map_ref_into_btree_string_map(value_map)?;
let mut r_vec = vec![];
Expand Down Expand Up @@ -780,7 +786,7 @@ impl DocumentPropertyType {
})?;
Ok(r_vec)
} else {
Err(get_field_type_matching_error().into())
Err(get_field_type_matching_error(value).into())
}
}

Expand All @@ -799,7 +805,9 @@ impl DocumentPropertyType {
}
match self {
DocumentPropertyType::String(_) => {
let value_as_text = value.as_text().ok_or_else(get_field_type_matching_error)?;
let value_as_text = value
.as_text()
.ok_or_else(|| get_field_type_matching_error(value))?;
let vec = value_as_text.as_bytes().to_vec();
if vec.is_empty() {
// we don't want to collide with the definition of an empty string
Expand All @@ -826,7 +834,9 @@ impl DocumentPropertyType {
.to_identifier_bytes()
.map_err(ProtocolError::ValueError),
DocumentPropertyType::Boolean => {
let value_as_boolean = value.as_bool().ok_or_else(get_field_type_matching_error)?;
let value_as_boolean = value
.as_bool()
.ok_or_else(|| get_field_type_matching_error(value))?;
if value_as_boolean {
Ok(vec![1])
} else {
Expand Down Expand Up @@ -1205,8 +1215,9 @@ impl DocumentPropertyType {
}
}

fn get_field_type_matching_error() -> DataContractError {
DataContractError::ValueWrongType(
"document field type doesn't match document value".to_string(),
)
fn get_field_type_matching_error(value: &Value) -> DataContractError {
DataContractError::ValueWrongType(format!(
"document field type doesn't match \"{}\" document value",
value
))
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use platform_value::Identifier;
PlatformSerialize,
PlatformDeserialize,
)]
#[error("missing position is not present for updated document type")]
#[error(
"position field is not present for document type \"{}\"",
document_type_name
)]
#[platform_serialize(unversioned)]
pub struct MissingPositionsInDocumentTypePropertiesError {
/*
Expand Down

0 comments on commit 5405794

Please sign in to comment.