Handle RLP to string UTF-8 decoding errors (#2217) (#2226)

This commit is contained in:
Tomasz Drwięga 2016-09-22 14:48:54 +02:00 committed by Gav Wood
parent d59e2ecbc6
commit f5d48cbf2a

View File

@ -174,6 +174,8 @@ pub enum FromBytesError {
DataIsTooLong,
/// Integer-representation is non-canonically prefixed with zero byte(s).
ZeroPrefixedInt,
/// String representation is not utf-8
InvalidUtf8,
}
impl StdError for FromBytesError {
@ -199,7 +201,7 @@ pub trait FromBytes: Sized {
impl FromBytes for String {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<String> {
Ok(::std::str::from_utf8(bytes).unwrap().to_owned())
::std::str::from_utf8(bytes).map(|s| s.to_owned()).map_err(|_| FromBytesError::InvalidUtf8)
}
}