Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rpc error: support escaped strings #578

Merged
merged 2 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions types/src/v2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// DEALINGS IN THE SOFTWARE.

use crate::v2::params::{Id, TwoPointZero};
use beef::Cow;
use serde::de::Deserializer;
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -64,7 +65,7 @@ pub struct ErrorObject<'a> {
/// Code
pub code: ErrorCode,
/// Message
pub message: &'a str,
pub message: Cow<'a, str>,
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
/// Optional data
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(borrow)]
Expand All @@ -74,13 +75,13 @@ pub struct ErrorObject<'a> {
impl<'a> ErrorObject<'a> {
/// Create a new `ErrorObject` with optional data.
pub fn new(code: ErrorCode, data: Option<&'a RawValue>) -> ErrorObject<'a> {
Self { code, message: code.message(), data }
Self { code, message: code.message().into(), data }
}
}

impl<'a> From<ErrorCode> for ErrorObject<'a> {
fn from(code: ErrorCode) -> Self {
Self { code, message: code.message(), data: None }
Self { code, message: code.message().into(), data: None }
}
}

Expand Down Expand Up @@ -242,7 +243,7 @@ mod tests {
let ser = r#"{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}"#;
let exp = RpcError {
jsonrpc: TwoPointZero,
error: ErrorObject { code: ErrorCode::ParseError, message: "Parse error", data: None },
error: ErrorObject { code: ErrorCode::ParseError, message: "Parse error".into(), data: None },
id: Id::Null,
};
let err: RpcError = serde_json::from_str(ser).unwrap();
Expand All @@ -255,19 +256,48 @@ mod tests {
let data = serde_json::value::to_raw_value(&"vegan").unwrap();
let exp = RpcError {
jsonrpc: TwoPointZero,
error: ErrorObject { code: ErrorCode::ParseError, message: "Parse error", data: Some(&*data) },
error: ErrorObject { code: ErrorCode::ParseError, message: "Parse error".into(), data: Some(&*data) },
id: Id::Null,
};
let err: RpcError = serde_json::from_str(ser).unwrap();
assert_eq!(exp, err);
}

#[test]
fn deserialized_error_with_quoted_str() {
let raw = r#"{
"error": {
"code": 1002,
"message": "desc: \"Could not decode `ChargeAssetTxPayment::asset_id`\" } })",
"data": "\\\"validate_transaction\\\""
},
"id": 7,
"jsonrpc": "2.0"
}"#;
let err: RpcError = serde_json::from_str(raw).unwrap();

let data = serde_json::value::to_raw_value(&"\\\"validate_transaction\\\"").unwrap();

assert_eq!(
err,
RpcError {
error: ErrorObject {
code: 1002.into(),
message: "desc: \"Could not decode `ChargeAssetTxPayment::asset_id`\" } })".into(),
data: Some(&*data),
},
id: Id::Number(7),
jsonrpc: TwoPointZero,
}
);
}

#[test]
fn serialize_works() {
let exp = r#"{"jsonrpc":"2.0","error":{"code":-32603,"message":"Internal error"},"id":1337}"#;
let err = RpcError {
jsonrpc: TwoPointZero,
error: ErrorObject { code: ErrorCode::InternalError, message: "Internal error", data: None },
error: ErrorObject { code: ErrorCode::InternalError, message: "Internal error".into(), data: None },
id: Id::Number(1337),
};
let ser = serde_json::to_string(&err).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions utils/src/server/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn send_response(id: Id, tx: &MethodSink, result: impl Serialize, max_respon
let data = to_json_raw_value(&format!("Exceeded max limit {}", max_response_size)).ok();
let err = ErrorObject {
code: ErrorCode::ServerError(OVERSIZED_RESPONSE_CODE),
message: OVERSIZED_RESPONSE_MSG,
message: OVERSIZED_RESPONSE_MSG.into(),
data: data.as_deref(),
};
return send_error(id, tx, err);
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn send_call_error(id: Id, tx: &MethodSink, err: Error) {
e => (ErrorCode::ServerError(UNKNOWN_ERROR_CODE), e.to_string(), None),
};

let err = ErrorObject { code, message: &message, data: data.as_deref() };
let err = ErrorObject { code, message: message.into(), data: data.as_deref() };

send_error(id, tx, err)
}
Expand Down