Correctly encode/decode RPC errors (#1157)

This commit is contained in:
Age Manning
2020-05-18 18:13:03 +10:00
committed by GitHub
parent 2d8e2dd7f5
commit a4b07a833c
6 changed files with 27 additions and 37 deletions

View File

@@ -181,13 +181,13 @@ pub enum RPCCodedResponse<T: EthSpec> {
Success(RPCResponse<T>),
/// The response was invalid.
InvalidRequest(ErrorMessage),
InvalidRequest(String),
/// The response indicates a server error.
ServerError(ErrorMessage),
ServerError(String),
/// There was an unknown response.
Unknown(ErrorMessage),
Unknown(String),
/// Received a stream termination indicating which response is being terminated.
StreamTermination(ResponseTermination),
@@ -222,7 +222,7 @@ impl<T: EthSpec> RPCCodedResponse<T> {
}
/// Builds an RPCCodedResponse from a response code and an ErrorMessage
pub fn from_error(response_code: u8, err: ErrorMessage) -> Self {
pub fn from_error(response_code: u8, err: String) -> Self {
match response_code {
1 => RPCCodedResponse::InvalidRequest(err),
2 => RPCCodedResponse::ServerError(err),
@@ -268,18 +268,6 @@ impl<T: EthSpec> RPCCodedResponse<T> {
}
}
#[derive(Encode, Decode, Debug, Clone)]
pub struct ErrorMessage {
/// The UTF-8 encoded Error message string.
pub error_message: Vec<u8>,
}
impl std::string::ToString for ErrorMessage {
fn to_string(&self) -> String {
String::from_utf8(self.error_message.clone()).unwrap_or_else(|_| "".into())
}
}
impl std::fmt::Display for RPCResponseErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let repr = match self {