Improve some RPC error messages. (#6311)

This commit is contained in:
Tomasz Drwięga 2017-08-20 06:13:00 +02:00 committed by Gav Wood
parent 4ccc82be92
commit f79159a69c
8 changed files with 32 additions and 14 deletions

View File

@ -245,17 +245,27 @@ impl<C: Send + Sync + 'static> EthPubSub for EthPubSubClient<C> {
kind: pubsub::Kind,
params: Trailing<pubsub::Params>,
) {
match (kind, params.into()) {
let error = match (kind, params.into()) {
(pubsub::Kind::NewHeads, None) => {
self.heads_subscribers.write().push(subscriber)
self.heads_subscribers.write().push(subscriber);
return;
},
(pubsub::Kind::Logs, Some(pubsub::Params::Logs(filter))) => {
self.logs_subscribers.write().push(subscriber, filter.into());
return;
},
(pubsub::Kind::NewHeads, _) => {
errors::invalid_params("newHeads", "Expected no parameters.")
},
(pubsub::Kind::Logs, _) => {
errors::invalid_params("logs", "Expected a filter object.")
},
_ => {
let _ = subscriber.reject(errors::unimplemented(None));
errors::unimplemented(None)
},
}
};
let _ = subscriber.reject(error);
}
fn unsubscribe(&self, id: SubscriptionId) -> BoxFuture<bool, Error> {

View File

@ -214,7 +214,7 @@ impl<T: Serialize> Serialize for Rich<T> {
// and serialize
value.serialize(serializer)
} else {
Err(S::Error::custom("Unserializable structures."))
Err(S::Error::custom("Unserializable structures: expected objects"))
}
}
}

View File

@ -79,8 +79,12 @@ impl<'a> Visitor<'a> for BlockNumberVisitor {
"latest" => Ok(BlockNumber::Latest),
"earliest" => Ok(BlockNumber::Earliest),
"pending" => Ok(BlockNumber::Pending),
_ if value.starts_with("0x") => u64::from_str_radix(&value[2..], 16).map(BlockNumber::Num).map_err(|_| Error::custom("invalid block number")),
_ => value.parse::<u64>().map(BlockNumber::Num).map_err(|_| Error::custom("invalid block number"))
_ if value.starts_with("0x") => u64::from_str_radix(&value[2..], 16).map(BlockNumber::Num).map_err(|e| {
Error::custom(format!("Invalid block number: {}", e))
}),
_ => value.parse::<u64>().map(BlockNumber::Num).map_err(|e| {
Error::custom(format!("Invalid block number: {}", e))
}),
}
}

View File

@ -81,9 +81,9 @@ impl<'a> Visitor<'a> for BytesVisitor {
);
Ok(Bytes::new(Vec::new()))
} else if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 {
Ok(Bytes::new(FromHex::from_hex(&value[2..]).map_err(|_| Error::custom("invalid hex"))?))
Ok(Bytes::new(FromHex::from_hex(&value[2..]).map_err(|e| Error::custom(format!("Invalid hex: {}", e)))?))
} else {
Err(Error::custom("invalid format"))
Err(Error::custom("Invalid bytes format. Expected a 0x-prefixed hex string with even length"))
}
}

View File

@ -121,7 +121,7 @@ impl<'a> Visitor<'a> for DerivationTypeVisitor {
match value {
"soft" => Ok(DerivationType::Soft),
"hard" => Ok(DerivationType::Hard),
_ => Err(Error::custom("invalid derivation type")),
v => Err(Error::custom(format!("invalid derivation type: {:?}", v))),
}
}

View File

@ -43,7 +43,7 @@ impl<'a, T> Deserialize<'a> for VariadicValue<T> where T: DeserializeOwned {
from_value(v.clone()).map(VariadicValue::Single)
.or_else(|_| from_value(v).map(VariadicValue::Multiple))
.map_err(|_| D::Error::custom("Invalid type."))
.map_err(|err| D::Error::custom(format!("Invalid variadic value type: {}", err)))
}
}

View File

@ -47,8 +47,12 @@ impl<'a> Visitor<'a> for IndexVisitor {
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: Error {
match value {
_ if value.starts_with("0x") => usize::from_str_radix(&value[2..], 16).map(Index).map_err(|_| Error::custom("invalid index")),
_ => value.parse::<usize>().map(Index).map_err(|_| Error::custom("invalid index")),
_ if value.starts_with("0x") => usize::from_str_radix(&value[2..], 16).map(Index).map_err(|e| {
Error::custom(format!("Invalid index: {}", e))
}),
_ => value.parse::<usize>().map(Index).map_err(|e| {
Error::custom(format!("Invalid index: {}", e))
}),
}
}

View File

@ -84,7 +84,7 @@ impl<'a> Deserialize<'a> for Params {
}
from_value(v.clone()).map(Params::Logs)
.map_err(|_| D::Error::custom("Invalid type."))
.map_err(|e| D::Error::custom(format!("Invalid Pub-Sub parameters: {}", e)))
}
}