Skip to content

Commit

Permalink
Fix issue 1078, wallet panic cause by display formatter, should not r…
Browse files Browse the repository at this point in the history
…eturn error in Display::fmt impl (MystenLabs#1082)
  • Loading branch information
patrickkuo authored Mar 26, 2022
1 parent bca13f6 commit 5aa9228
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
11 changes: 11 additions & 0 deletions sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use move_core_types::identifier::Identifier;
use serde_json::{json, Value};
use tracing_test::traced_test;

use std::fmt::Write;
use sui::config::{
AccountConfig, AuthorityPrivateInfo, Config, GenesisConfig, NetworkConfig, ObjectConfig,
PersistedConfig, WalletConfig, AUTHORITIES_DB_NAME,
Expand Down Expand Up @@ -1027,3 +1028,13 @@ async fn test_native_transfer() -> Result<(), anyhow::Error> {
network.kill().await?;
Ok(())
}

#[test]
// Test for issue https://github.com/MystenLabs/sui/issues/1078
fn test_bug_1078() {
let read = WalletCommandResult::Object(ObjectRead::NotExists(ObjectID::random()));
let mut writer = String::new();
// fmt ObjectRead should not fail.
write!(writer, "{}", read).unwrap();
write!(writer, "{:?}", read).unwrap();
}
25 changes: 14 additions & 11 deletions sui/src/wallet_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use colored::Colorize;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::TypeTag;
use move_core_types::parser::parse_type_tag;
use serde::ser::Error;
use serde::Serialize;
use structopt::clap::AppSettings;
use structopt::StructOpt;
Expand Down Expand Up @@ -476,7 +475,7 @@ impl Display for WalletCommandResult {
write!(writer, "{}", response)?;
}
WalletCommandResult::Object(object_read) => {
let object = object_read.object().map_err(fmt::Error::custom)?;
let object = unwrap_err_to_string(|| Ok(object_read.object()?));
writeln!(writer, "{}", object)?;
}
WalletCommandResult::Call(cert, effects) => {
Expand Down Expand Up @@ -550,21 +549,25 @@ fn write_cert_and_effects(

impl Debug for WalletCommandResult {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = match self {
let s = unwrap_err_to_string(|| match self {
WalletCommandResult::Object(object_read) => {
let object = object_read.object().map_err(fmt::Error::custom)?;
let layout = object_read.layout().map_err(fmt::Error::custom)?;
object
.to_json(layout)
.map_err(fmt::Error::custom)?
.to_string()
let object = object_read.object()?;
let layout = object_read.layout()?;
Ok(object.to_json(layout)?.to_string())
}
_ => serde_json::to_string(self).map_err(fmt::Error::custom)?,
};
_ => Ok(serde_json::to_string(self)?),
});
write!(f, "{}", s)
}
}

fn unwrap_err_to_string<T: Display, F: FnOnce() -> Result<T, anyhow::Error>>(func: F) -> String {
match func() {
Ok(s) => format!("{}", s),
Err(err) => format!("{}", err).red().to_string(),
}
}

impl WalletCommandResult {
pub fn print(&self, pretty: bool) {
let line = if pretty {
Expand Down

0 comments on commit 5aa9228

Please sign in to comment.