Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Use jsonrpc-core data for improved and more consistent error handling flow #402

Merged
merged 1 commit into from
Jul 13, 2017

Conversation

Xanewok
Copy link
Member

@Xanewok Xanewok commented Jul 8, 2017

Laying foundations for #395, #376 and proper #360 - JSON-RPC conformance in general and tidying the server flow logic a bit.

  • Pulls in jsonrpc_core::Id for the number | string | null id type in JSON-RPC which we didn't support until recently
  • Introduces more error codes
  • Uses jsonrpc_core structs instead of our custom ones for failure/error message logic.

I have also some pending WIP changes planned for {Notification,Request}Message (here), but I still need to work on stronger typifying, will probably land with gluon-lang/lsp-types#19.

Moving completely to using jsonrpc-core means making a lot of internal changes, so I figured it'd be nice to move a small, self-contained portion and see if the approach is okay.

@Xanewok
Copy link
Member Author

Xanewok commented Jul 9, 2017

It seems that apart from the tests in #400, an upgrade now causes more tests to fail, most notably the spans are different and it's not specific to this PR. Could it have been caused by rls-analysis update?

EDIT: Tests issues described here: #404.

@Xanewok
Copy link
Member Author

Xanewok commented Jul 9, 2017

Rebased and force-pushed now

src/test/mod.rs Outdated
.expect("Couldn't parse json failure response");

const PARSE_ERROR: jsonrpc_core::ErrorCode = jsonrpc_core::ErrorCode::ParseError;
assert!(failure.error.code == PARSE_ERROR);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I look at it again I guess it could be just

assert!(failure.error.code == jsonrpc_core::ErrorCode::ParseError);

Copy link
Member

@nrc nrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this stuff is all improvement.

src/server.rs Outdated
jsonrpc::Failure {
jsonrpc: Some(version::Version::V2),
id: id,
error: error,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can just use id, error here.

src/server.rs Outdated
},
None => None
let id = ls_command.get("id").expect("no id").to_owned();
let id: jsonrpc::Id = serde_json::from_value(id).expect("Can't extract id");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should continue to be careful not to panic here. If we don't need an id, then we should not panic on an invalid id.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Missed that. Added handling missing id, but I now realized that according to JSON-RPC/LSP failure responses are only sent as a response to requests (and not notifications).

I'd say we should eventually change return value of parse_message to Result<ServerMessage, Option<jsonrpc::Failure>> and handle/output failures only for received invalid Requests, but with this we're not making the flow any worse wrt that and we can improve it the next iteration. Added few reminding comments. @nrc what do you think?

src/server.rs Outdated
code: jsonrpc::ErrorCode::MethodNotFound,
message: String::from("setTraceNotification"),
data: None
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to special case setTraceNotification rather than swallow it in the _ case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, just tried to retain the same semantics when rewriting it. Should I move it under _?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, may as well

src/server.rs Outdated
code: METHOD_NOT_FOUND,
message: message.to_owned(),
},
fn failure_simple<M: Into<String>>(&self, id: usize, code: jsonrpc::ErrorCode, msg: M) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about failure_message rather than failure_simple?

@Xanewok Xanewok force-pushed the json-rpc branch 2 times, most recently from 854c245 to d96891e Compare July 11, 2017 09:55
src/server.rs Outdated
serde_json::from_value(params.expect("no params").to_owned()).expect("Can't extract params");
let params = match params {
Some(value) => value,
None => return Err(Some(server_failure(Id::Null, jsonrpc::Error::invalid_request()))),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the id in these error messages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should, updated now

@nrc nrc merged commit d195862 into rust-lang:master Jul 13, 2017
@Xanewok Xanewok deleted the json-rpc branch August 3, 2017 07:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants