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

fix: some document error messages didnt specify the corresponding property #1873

Open
wants to merge 1 commit into
base: v1.0-dev
Choose a base branch
from
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
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 @@ -550,7 +550,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 @@ -604,7 +604,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 @@ -640,7 +642,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 @@ -655,7 +657,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 @@ -676,7 +678,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 @@ -716,7 +720,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 @@ -726,7 +732,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 @@ -768,7 +774,7 @@ impl DocumentPropertyType {
})?;
Ok(r_vec)
} else {
Err(get_field_type_matching_error().into())
Err(get_field_type_matching_error(value).into())
}
}

Expand All @@ -787,7 +793,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 @@ -814,7 +822,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 @@ -1034,8 +1044,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
Loading