Skip to content

Commit

Permalink
feat(wasm-dpp): add DataContractErrorWasm
Browse files Browse the repository at this point in the history
  • Loading branch information
markin-io committed Apr 4, 2024
1 parent fd36a6c commit e52ab2d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
8 changes: 8 additions & 0 deletions packages/rs-dpp/src/data_contract/errors/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::consensus::basic::decode::DecodingError;
use bincode::{Decode, Encode};
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
use thiserror::Error;
use crate::consensus::basic::BasicError;

use crate::consensus::basic::document::InvalidDocumentTypeError;
use crate::consensus::ConsensusError;
use crate::data_contract::errors::json_schema_error::JsonSchemaError;
use crate::ProtocolError;

Expand Down Expand Up @@ -87,3 +89,9 @@ impl From<(platform_value::Error, &str)> for DataContractError {
DataContractError::ValueDecodingError(format!("{}: {:?}", value.1, value.0))
}
}

impl From<DataContractError> for ConsensusError {
fn from(e: DataContractError) -> Self {
ConsensusError::BasicError(BasicError::ContractError(e))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use dpp::consensus::codes::ErrorWithCode;
use dpp::consensus::ConsensusError;

use wasm_bindgen::prelude::*;
use dpp::data_contract::errors::DataContractError;

#[wasm_bindgen(js_name=DataContractError)]
pub struct DataContractErrorWasm {
inner: DataContractError,
}

impl From<&DataContractError> for DataContractErrorWasm {
fn from(e: &DataContractError) -> Self {
Self { inner: e.clone() }
}
}

#[wasm_bindgen(js_class=DataContractError)]
impl DataContractErrorWasm {
#[wasm_bindgen(js_name=getCode)]
pub fn get_code(&self) -> u32 {
ConsensusError::from(self.inner.clone()).code()
}

#[wasm_bindgen(getter)]
pub fn message(&self) -> String {
self.inner.to_string()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod data_contract_error;
mod data_contract_empty_schema_error;
mod data_contract_have_new_unique_index_error;
mod data_contract_immutable_properties_update_error;
Expand All @@ -13,6 +14,7 @@ mod invalid_data_contract_version_error;
mod invalid_document_type_name_error;
mod invalid_json_schema_ref_error;

pub use data_contract_error::*;
pub use data_contract_empty_schema_error::*;
pub use data_contract_have_new_unique_index_error::*;
pub use data_contract_immutable_properties_update_error::*;
Expand Down
17 changes: 3 additions & 14 deletions packages/wasm-dpp/src/errors/consensus/consensus_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use dpp::consensus::basic::BasicError::{DuplicatedIdentityPublicKeyBasicError, D
use dpp::consensus::fee::fee_error::FeeError;
use dpp::consensus::signature::SignatureError;
use dpp::consensus::state::state_error::StateError;
use dpp::consensus::basic::decode::{DecodingError, VersionError};
use dpp::consensus::basic::decode::{VersionError};

use dpp::consensus::state::data_trigger::DataTriggerError::{
DataTriggerConditionError, DataTriggerExecutionError, DataTriggerInvalidResultError,
Expand All @@ -28,9 +28,8 @@ use dpp::consensus::basic::identity::{DataContractBoundsNotPresentError, Disabli
use dpp::consensus::state::data_contract::document_type_update_error::DocumentTypeUpdateError;
use dpp::consensus::state::identity::identity_public_key_already_exists_for_unique_contract_bounds_error::IdentityPublicKeyAlreadyExistsForUniqueContractBoundsError;
use dpp::consensus::state::identity::master_public_key_update_error::MasterPublicKeyUpdateError;
use dpp::data_contract::errors::DataContractError;

use crate::errors::consensus::basic::data_contract::{DataContractEmptySchemaErrorWasm, DataContractHaveNewUniqueIndexErrorWasm, DataContractImmutablePropertiesUpdateErrorWasm, DataContractInvalidIndexDefinitionUpdateErrorWasm, DataContractUniqueIndicesChangedErrorWasm, IncompatibleDataContractSchemaErrorWasm, InvalidDataContractIdErrorWasm, InvalidDocumentTypeNameErrorWasm};
use crate::errors::consensus::basic::data_contract::{DataContractEmptySchemaErrorWasm, DataContractErrorWasm, DataContractHaveNewUniqueIndexErrorWasm, DataContractImmutablePropertiesUpdateErrorWasm, DataContractInvalidIndexDefinitionUpdateErrorWasm, DataContractUniqueIndicesChangedErrorWasm, IncompatibleDataContractSchemaErrorWasm, InvalidDataContractIdErrorWasm, InvalidDocumentTypeNameErrorWasm};
use crate::errors::consensus::basic::document::{DocumentTransitionsAreAbsentErrorWasm, DuplicateDocumentTransitionsWithIdsErrorWasm, DuplicateDocumentTransitionsWithIndicesErrorWasm, IdentityContractNonceOutOfBoundsErrorWasm, InvalidDocumentTransitionActionErrorWasm, InvalidDocumentTransitionIdErrorWasm, MissingDataContractIdErrorWasm, MissingDocumentTypeErrorWasm};
use crate::errors::consensus::basic::state_transition::{
InvalidStateTransitionTypeErrorWasm, MissingStateTransitionTypeErrorWasm,
Expand Down Expand Up @@ -360,17 +359,7 @@ fn from_basic_error(basic_error: &BasicError) -> JsValue {
IncompatibleRe2PatternError(err) => IncompatibleRe2PatternErrorWasm::from(err).into(),
BasicError::VersionError(err) => generic_wasm_error!(VersionError, err).into(),
BasicError::ContractError(e) => {
match e {
DataContractError::DecodingContractError(err) |
DataContractError::DecodingDocumentError(err) => {
generic_wasm_error!(DecodingError, err).into()
},
DataContractError::InvalidDocumentTypeError(err) => {
InvalidDocumentTypeErrorWasm::from(err).into()
},
// Remaining errors do not have conversion to ConsensusError implemented
_ => todo!()
}
DataContractErrorWasm::from(e).into()
}
BasicError::UnknownSecurityLevelError(e) => {
generic_wasm_error!(UnknownSecurityLevelError, e).into()
Expand Down

0 comments on commit e52ab2d

Please sign in to comment.