From 648dc1da37892ed7592e3e2fc6c4380c2ce913b8 Mon Sep 17 00:00:00 2001 From: luoshuijs Date: Mon, 27 Nov 2023 15:32:03 +0800 Subject: [PATCH] :art: Add More Contextual Information to Error Messages --- src/applications/input/artifact.rs | 48 +++++++++++++++++++++++------ src/applications/input/buff.rs | 25 ++++++++++++--- src/applications/input/character.rs | 17 +++++++--- src/applications/input/skill.rs | 7 +++-- src/applications/input/weapon.rs | 24 ++++++++++++--- 5 files changed, 96 insertions(+), 25 deletions(-) diff --git a/src/applications/input/artifact.rs b/src/applications/input/artifact.rs index 93a398e..4283ac1 100644 --- a/src/applications/input/artifact.rs +++ b/src/applications/input/artifact.rs @@ -1,10 +1,12 @@ use anyhow::anyhow; -use mona::artifacts::{Artifact as MonaArtifact, ArtifactSetName, ArtifactSlotName}; -use mona::common::StatName; + use pyo3::prelude::*; use pyo3::types::{PyDict, PyList, PyString}; use pythonize::depythonize; +use mona::artifacts::{Artifact as MonaArtifact, ArtifactSetName, ArtifactSlotName}; +use mona::common::StatName; + #[pyclass(name = "Artifact")] #[derive(Clone)] pub struct PyArtifact { @@ -87,27 +89,53 @@ impl TryInto for PyArtifact { fn try_into(self) -> Result { let name: ArtifactSetName = Python::with_gil(|py| { let _string: &PyString = self.set_name.as_ref(py); - depythonize(_string) - .map_err(|err| anyhow!("Failed to deserialize artifact set name: {}", err)) + depythonize(_string).map_err(|err| { + let serialized_data = format!("{:?}", _string); + anyhow!( + "Failed to deserialize name into mona::artifacts::ArtifactSetName: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })?; let slot: ArtifactSlotName = Python::with_gil(|py| { let _string: &PyString = self.slot.as_ref(py); - depythonize(_string) - .map_err(|err| anyhow!("Failed to deserialize artifact slot name: {}", err)) + depythonize(_string).map_err(|err| { + let serialized_data = format!("{:?}", _string); + anyhow!( + "Failed to deserialize slot name into mona::artifacts::ArtifactSlotName: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })?; let main_stat_name: StatName = Python::with_gil(|py| { - depythonize(self.main_stat.0.as_ref(py)) - .map_err(|err| anyhow!("Failed to deserialize main stat name: {}", err)) + let main_stat = self.main_stat.0.as_ref(py); + depythonize(self.main_stat.0.as_ref(py)).map_err(|err| { + let serialized_data = format!("{:?}", main_stat); + anyhow!( + "Failed to deserialize main stat into mona::artifacts::StatName: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })?; let sub_stats = Python::with_gil(|py| { self.sub_stats .iter() .map(|s| { - let name: Result = depythonize(s.0.as_ref(py)) - .map_err(|err| anyhow!("Failed to deserialize sub stat name: {}", err)); + let sub_stats = s.0.as_ref(py); + let name: Result = depythonize(s.0.as_ref(py)).map_err(|err| { + let serialized_data = format!("{:?}", sub_stats); + anyhow!( + "Failed to deserialize sub stats into mona::artifacts::StatName: {}. Serialized data: \n{}", + err, + serialized_data + ) + }); match name { Ok(n) => Ok((n, s.1)), Err(e) => Err(e), diff --git a/src/applications/input/buff.rs b/src/applications/input/buff.rs index b7aa613..0fb0c77 100644 --- a/src/applications/input/buff.rs +++ b/src/applications/input/buff.rs @@ -1,10 +1,11 @@ use anyhow::anyhow; -use mona::buffs::buff_name::BuffName; -use mona::buffs::BuffConfig; + use pyo3::prelude::*; +use pyo3::types::{PyDict, PyString}; use pythonize::depythonize; -use pyo3::types::{PyDict, PyString}; +use mona::buffs::buff_name::BuffName; +use mona::buffs::BuffConfig; use mona_wasm::applications::common::BuffInterface as MonaBuffInterface; @@ -56,13 +57,27 @@ impl TryInto for PyBuffInterface { fn try_into(self) -> Result { let name: BuffName = Python::with_gil(|py| { let _string: &PyString = self.name.as_ref(py); - depythonize(_string).map_err(|err| anyhow!("Failed to deserialize name: {}", err)) + depythonize(_string).map_err(|err| { + let serialized_data = format!("{:?}", _string); + anyhow!( + "Failed to deserialize name into mona::buffs::buff_name::BuffName: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })?; let config: BuffConfig = if let Some(value) = self.config { Python::with_gil(|py| { let _dict: &PyDict = value.as_ref(py); - depythonize(_dict).map_err(|err| anyhow!("Failed to deserialize config: {}", err)) + depythonize(_dict).map_err(|err| { + let serialized_data = format!("{:?}", _dict); + anyhow!( + "Failed to deserialize config into mona::buffs::BuffConfig: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })? } else { BuffConfig::NoConfig diff --git a/src/applications/input/character.rs b/src/applications/input/character.rs index c5dd5a7..fdfd27e 100644 --- a/src/applications/input/character.rs +++ b/src/applications/input/character.rs @@ -1,8 +1,9 @@ -use anyhow::Context; +use anyhow::{anyhow, Context}; +use std::str::FromStr; + use pyo3::prelude::*; use pyo3::types::PyDict; use pythonize::depythonize; -use std::str::FromStr; use mona::character::{CharacterConfig, CharacterName}; use mona_wasm::applications::common::CharacterInterface as MonaCharacterInterface; @@ -90,11 +91,19 @@ impl TryInto for PyCharacterInterface { type Error = anyhow::Error; fn try_into(self) -> Result { - let name = CharacterName::from_str(&self.name).context("Failed to deserialize json")?; + let name = CharacterName::from_str(&self.name) + .context("Failed to name params into mona::character::CharacterName")?; let params: CharacterConfig = if let Some(value) = self.params { Python::with_gil(|py| { let _dict: &PyDict = value.as_ref(py); - depythonize(_dict).context("Failed to convert PyDict to CharacterConfig") + depythonize(_dict).map_err(|err| { + let serialized_data = format!("{:?}", _dict); + anyhow!( + "Failed to deserialize params into mona::character::CharacterConfig: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })? } else { CharacterConfig::NoConfig diff --git a/src/applications/input/skill.rs b/src/applications/input/skill.rs index 9014d21..d7f9e3b 100644 --- a/src/applications/input/skill.rs +++ b/src/applications/input/skill.rs @@ -1,4 +1,4 @@ -use anyhow::Context; +use anyhow::anyhow; use pyo3::prelude::*; use pyo3::types::PyDict; use pythonize::depythonize; @@ -48,7 +48,10 @@ impl TryInto for PySkillInterface { let config: CharacterSkillConfig = if let Some(value) = self.config { Python::with_gil(|py| { let _dict: &PyDict = value.as_ref(py); - depythonize(_dict).context("Failed to convert PyDict to CharacterConfig") + depythonize(_dict).map_err(|err| { + let serialized_data = format!("{:?}", _dict); + anyhow!("Failed to deserialize config into mona::character::skill_config::CharacterSkillConfig: {}. Serialized data: \n{}", err, serialized_data) + }) })? } else { CharacterSkillConfig::NoConfig diff --git a/src/applications/input/weapon.rs b/src/applications/input/weapon.rs index e900dfe..b558672 100644 --- a/src/applications/input/weapon.rs +++ b/src/applications/input/weapon.rs @@ -1,10 +1,12 @@ use anyhow::anyhow; -use mona::weapon::{WeaponConfig, WeaponName}; -use mona_wasm::applications::common::WeaponInterface as MonaWeaponInterface; + use pyo3::prelude::*; use pyo3::types::{PyDict, PyString}; use pythonize::depythonize; +use mona::weapon::{WeaponConfig, WeaponName}; +use mona_wasm::applications::common::WeaponInterface as MonaWeaponInterface; + #[pyclass(name = "WeaponInterface")] #[derive(Clone)] pub struct PyWeaponInterface { @@ -74,13 +76,27 @@ impl TryInto for PyWeaponInterface { fn try_into(self) -> Result { let name: WeaponName = Python::with_gil(|py| { let _string: &PyString = self.name.as_ref(py); - depythonize(_string).map_err(|err| anyhow!("Failed to deserialize name: {}", err)) + depythonize(_string).map_err(|err| { + let serialized_data = format!("{:?}", _string); + anyhow!( + "Failed to deserialize name into mona::weapon::WeaponName: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })?; let params: WeaponConfig = if let Some(value) = self.params { Python::with_gil(|py| { let _dict: &PyDict = value.as_ref(py); - depythonize(_dict).map_err(|err| anyhow!("Failed to deserialize params: {}", err)) + depythonize(_dict).map_err(|err| { + let serialized_data = format!("{:?}", _dict); + anyhow!( + "Failed to deserialize params into mona::weapon::WeaponConfig: {}. Serialized data: \n{}", + err, + serialized_data + ) + }) })? } else { WeaponConfig::NoConfig