🎨 Add More Contextual Information to Error Messages

This commit is contained in:
luoshuijs 2023-11-27 15:32:03 +08:00 committed by GitHub
parent 679566ce7f
commit 648dc1da37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 25 deletions

View File

@ -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<MonaArtifact> for PyArtifact {
fn try_into(self) -> Result<MonaArtifact, Self::Error> {
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<StatName, anyhow::Error> = 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<StatName, anyhow::Error> = 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),

View File

@ -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<MonaBuffInterface> for PyBuffInterface {
fn try_into(self) -> Result<MonaBuffInterface, Self::Error> {
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

View File

@ -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<MonaCharacterInterface> for PyCharacterInterface {
type Error = anyhow::Error;
fn try_into(self) -> Result<MonaCharacterInterface, Self::Error> {
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

View File

@ -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<MonaSkillInterface> 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

View File

@ -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<MonaWeaponInterface> for PyWeaponInterface {
fn try_into(self) -> Result<MonaWeaponInterface, Self::Error> {
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