evmbin: escape newlines in json errors (#9458)

This commit is contained in:
Andronik Ordian 2018-09-04 09:28:49 +03:00 committed by Wei Tang
parent c0eb30b604
commit 1f2426226b
3 changed files with 6 additions and 2 deletions

View File

@ -95,7 +95,7 @@ impl vm::Informant for Informant {
println!(
"{{\"error\":\"{error}\",\"gasUsed\":\"{gas:x}\",\"time\":{time}}}",
error = failure.error,
error = display::escape_newlines(&failure.error),
gas = failure.gas_used,
time = display::as_micros(&failure.time),
)

View File

@ -31,3 +31,7 @@ pub fn format_time(time: &Duration) -> String {
pub fn as_micros(time: &Duration) -> u64 {
time.as_secs() * 1_000_000 + time.subsec_nanos() as u64 / 1_000
}
fn escape_newlines<D: ::std::fmt::Display>(s: D) -> String {
format!("{}", s).replace("\r\n", "\n").replace('\n', "\\n")
}

View File

@ -129,7 +129,7 @@ impl<Trace: Writer, Out: Writer> vm::Informant for Informant<Trace, Out> {
writeln!(
&mut out_sink,
"{{\"error\":\"{error}\",\"gasUsed\":\"0x{gas:x}\",\"time\":{time}}}",
error = failure.error,
error = display::escape_newlines(&failure.error),
gas = failure.gas_used,
time = display::as_micros(&failure.time),
).expect("The sink must be writeable.");